Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FieldValue cheaper to clone by Arc-ing internally. #400

Merged
merged 1 commit into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions pytrustfall/src/shim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn interpret_query(
})?;
let owned_iter: Box<dyn Iterator<Item = BTreeMap<String, Py<PyAny>>>> =
Box::new(execution.map(|res| {
res.into_iter()
res.iter()
.map(|(k, v)| {
Python::with_gil(|py| {
let python_value = make_python_value(py, v);
Expand Down Expand Up @@ -143,7 +143,7 @@ impl AdapterShim {
}
}

fn make_python_value(py: Python, value: FieldValue) -> Py<PyAny> {
fn make_python_value(py: Python, value: &FieldValue) -> Py<PyAny> {
match value {
FieldValue::Null => Option::<i64>::None.into_py(py),
FieldValue::Uint64(x) => x.into_py(py),
Expand All @@ -153,7 +153,7 @@ fn make_python_value(py: Python, value: FieldValue) -> Py<PyAny> {
FieldValue::Boolean(x) => x.into_py(py),
FieldValue::Enum(_) => todo!(),
FieldValue::List(x) => x
.into_iter()
.iter()
.map(|v| make_python_value(py, v))
.collect::<Vec<_>>()
.into_py(py),
Expand All @@ -171,7 +171,7 @@ fn make_field_value_from_ref(value: &PyAny) -> Result<FieldValue, ()> {
} else if let Ok(inner) = value.extract::<f64>() {
Ok(FieldValue::Float64(inner))
} else if let Ok(inner) = value.extract::<String>() {
Ok(FieldValue::String(inner))
Ok(FieldValue::String(inner.into()))
} else if let Ok(inner) = value.extract::<Vec<&PyAny>>() {
let converted_values = inner
.iter()
Expand All @@ -187,7 +187,7 @@ fn make_field_value_from_ref(value: &PyAny) -> Result<FieldValue, ()> {
});

if let Some(inner_values) = converted_values {
Ok(FieldValue::List(inner_values))
Ok(FieldValue::List(inner_values.into()))
} else {
Err(())
}
Expand Down Expand Up @@ -244,7 +244,7 @@ impl Adapter<'static> for AdapterShim {
Python::with_gil(|py| {
let parameter_data: BTreeMap<String, Py<PyAny>> = parameters
.iter()
.map(|(k, v)| (k.to_string(), make_python_value(py, v.to_owned())))
.map(|(k, v)| (k.to_string(), make_python_value(py, v)))
.collect();

let py_iterable = self
Expand Down Expand Up @@ -297,7 +297,7 @@ impl Adapter<'static> for AdapterShim {
Python::with_gil(|py| {
let parameter_data: BTreeMap<String, Py<PyAny>> = parameters
.iter()
.map(|(k, v)| (k.to_string(), make_python_value(py, v.to_owned())))
.map(|(k, v)| (k.to_string(), make_python_value(py, v)))
.collect();

let py_iterable = self
Expand Down
4 changes: 2 additions & 2 deletions trustfall/tests/helper_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,10 @@ type Vertex {

assert_eq!(
res[0].get("name").unwrap().to_owned(),
trustfall::FieldValue::String(String::from("Berit"))
trustfall::FieldValue::String("Berit".into())
);
assert_eq!(
res[0].get("initial").unwrap().to_owned(),
trustfall::FieldValue::String(String::from("B"))
trustfall::FieldValue::String("B".into())
);
}
10 changes: 5 additions & 5 deletions trustfall_core/src/filesystem_interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,15 +289,15 @@ impl<'a> Adapter<'a> for FilesystemInterpreter {
"name" => Box::new(contexts.map(|context| match context.active_vertex() {
None => (context, FieldValue::Null),
Some(FilesystemVertex::Directory(ref x)) => {
let value = FieldValue::String(x.name.clone());
let value = FieldValue::String(x.name.clone().into());
(context, value)
}
_ => unreachable!(),
})),
"path" => Box::new(contexts.map(|context| match context.active_vertex() {
None => (context, FieldValue::Null),
Some(FilesystemVertex::Directory(ref x)) => {
let value = FieldValue::String(x.path.clone());
let value = FieldValue::String(x.path.clone().into());
(context, value)
}
_ => unreachable!(),
Expand All @@ -312,15 +312,15 @@ impl<'a> Adapter<'a> for FilesystemInterpreter {
"name" => Box::new(contexts.map(|context| match context.active_vertex() {
None => (context, FieldValue::Null),
Some(FilesystemVertex::File(ref x)) => {
let value = FieldValue::String(x.name.clone());
let value = FieldValue::String(x.name.clone().into());
(context, value)
}
_ => unreachable!(),
})),
"path" => Box::new(contexts.map(|context| match context.active_vertex() {
None => (context, FieldValue::Null),
Some(FilesystemVertex::File(ref x)) => {
let value = FieldValue::String(x.path.clone());
let value = FieldValue::String(x.path.clone().into());
(context, value)
}
_ => unreachable!(),
Expand All @@ -331,7 +331,7 @@ impl<'a> Adapter<'a> for FilesystemInterpreter {
let value = x
.extension
.clone()
.map(FieldValue::String)
.map(Into::into)
.unwrap_or(FieldValue::Null);
(context, value)
}
Expand Down
38 changes: 19 additions & 19 deletions trustfall_core/src/interpreter/filtering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ make_comparison_op_func!(less_than_or_equal, <=, slow_path_less_than_or_equal);
#[inline(always)]
pub(super) fn has_substring(left: &FieldValue, right: &FieldValue) -> bool {
match (left, right) {
(FieldValue::String(l), FieldValue::String(r)) => l.contains(r),
(FieldValue::String(l), FieldValue::String(r)) => l.contains(r.as_ref()),
(FieldValue::Null, FieldValue::String(_))
| (FieldValue::String(_), FieldValue::Null)
| (FieldValue::Null, FieldValue::Null) => false,
Expand All @@ -152,7 +152,7 @@ pub(super) fn has_substring(left: &FieldValue, right: &FieldValue) -> bool {
#[inline(always)]
pub(super) fn has_prefix(left: &FieldValue, right: &FieldValue) -> bool {
match (left, right) {
(FieldValue::String(l), FieldValue::String(r)) => l.starts_with(r),
(FieldValue::String(l), FieldValue::String(r)) => l.starts_with(r.as_ref()),
(FieldValue::Null, FieldValue::String(_))
| (FieldValue::String(_), FieldValue::Null)
| (FieldValue::Null, FieldValue::Null) => false,
Expand All @@ -163,7 +163,7 @@ pub(super) fn has_prefix(left: &FieldValue, right: &FieldValue) -> bool {
#[inline(always)]
pub(super) fn has_suffix(left: &FieldValue, right: &FieldValue) -> bool {
match (left, right) {
(FieldValue::String(l), FieldValue::String(r)) => l.ends_with(r),
(FieldValue::String(l), FieldValue::String(r)) => l.ends_with(r.as_ref()),
(FieldValue::Null, FieldValue::String(_))
| (FieldValue::String(_), FieldValue::Null)
| (FieldValue::Null, FieldValue::Null) => false,
Expand Down Expand Up @@ -760,43 +760,43 @@ mod tests {
fn test_mixed_list_equality_comparison() {
let test_data = vec![
(
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)]),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)]),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)].into()),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)].into()),
true,
),
(
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)]),
FieldValue::List(vec![FieldValue::Int64(0), FieldValue::Uint64(0)]),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)].into()),
FieldValue::List(vec![FieldValue::Int64(0), FieldValue::Uint64(0)].into()),
true,
),
(
FieldValue::List(vec![FieldValue::Int64(0), FieldValue::Uint64(0)]),
FieldValue::List(vec![FieldValue::Int64(0), FieldValue::Uint64(0)]),
FieldValue::List(vec![FieldValue::Int64(0), FieldValue::Uint64(0)].into()),
FieldValue::List(vec![FieldValue::Int64(0), FieldValue::Uint64(0)].into()),
true,
),
(
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(-2)]),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(-2)]),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(-2)].into()),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(-2)].into()),
true,
),
(
FieldValue::List(vec![FieldValue::Int64(-1), FieldValue::Uint64(2)]),
FieldValue::List(vec![FieldValue::Int64(-1), FieldValue::Uint64(2)]),
FieldValue::List(vec![FieldValue::Int64(-1), FieldValue::Uint64(2)].into()),
FieldValue::List(vec![FieldValue::Int64(-1), FieldValue::Uint64(2)].into()),
true,
),
(
FieldValue::List(vec![FieldValue::Int64(-1), FieldValue::Uint64(2)]),
FieldValue::List(vec![FieldValue::Uint64(2), FieldValue::Int64(-1)]),
FieldValue::List(vec![FieldValue::Int64(-1), FieldValue::Uint64(2)].into()),
FieldValue::List(vec![FieldValue::Uint64(2), FieldValue::Int64(-1)].into()),
false,
),
(
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)]),
FieldValue::List(vec![FieldValue::Int64(0)]),
FieldValue::List(vec![FieldValue::Uint64(0), FieldValue::Int64(0)].into()),
FieldValue::List(vec![FieldValue::Int64(0)].into()),
false,
),
(
FieldValue::List(vec![FieldValue::Uint64(0)]),
FieldValue::List(vec![]),
FieldValue::List(vec![FieldValue::Uint64(0)].into()),
FieldValue::List(vec![].into()),
false,
),
];
Expand Down
4 changes: 2 additions & 2 deletions trustfall_core/src/interpreter/hints/vertex_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,8 +402,8 @@ mod tests {
second => FieldValue::Int64(2),
third => FieldValue::Int64(3),
null => FieldValue::Null,
list => FieldValue::List(vec![FieldValue::Int64(1), FieldValue::Int64(2)]),
longer_list => FieldValue::List(vec![FieldValue::Int64(1), FieldValue::Int64(2), FieldValue::Int64(3)]),
list => FieldValue::List(vec![FieldValue::Int64(1), FieldValue::Int64(2)].into()),
longer_list => FieldValue::List(vec![FieldValue::Int64(1), FieldValue::Int64(2), FieldValue::Int64(3)].into()),
};

let test_data = [
Expand Down
15 changes: 9 additions & 6 deletions trustfall_core/src/ir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ mod tests {

#[test]
fn serialize_then_deserialize_enum() {
let value = FieldValue::Enum("foo".to_string());
let value = FieldValue::Enum("foo".into());
let deserialized: FieldValue = serialize_then_deserialize(&value);
assert_eq!(
value,
Expand All @@ -889,11 +889,14 @@ mod tests {

#[test]
fn serialize_then_deserialize_list() {
let value = FieldValue::List(vec![
FieldValue::Int64(1),
FieldValue::Int64(2),
FieldValue::String("foo".into()),
]);
let value = FieldValue::List(
vec![
FieldValue::Int64(1),
FieldValue::Int64(2),
FieldValue::String("foo".into()),
]
.into(),
);
let deserialized: FieldValue = serialize_then_deserialize(&value);
assert_eq!(
value,
Expand Down
46 changes: 25 additions & 21 deletions trustfall_core/src/ir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,8 @@ mod tests {
Type::new("[[String!]!]").unwrap(),
];
let values = vec![
FieldValue::String("".to_string()), // empty string is not the same value as null
FieldValue::String("test string".to_string()),
FieldValue::String("".into()), // empty string is not the same value as null
FieldValue::String("test string".into()),
];

for value in &values {
Expand Down Expand Up @@ -386,27 +386,31 @@ mod tests {
Type::new("[[String!]!]").unwrap(),
];
let non_nullable_values = vec![
FieldValue::List((1..3).map(FieldValue::Int64).collect_vec()),
FieldValue::List((1..3).map(FieldValue::Uint64).collect_vec()),
FieldValue::List(vec![
// Integer-typed but non-homogeneous FieldValue entries are okay.
FieldValue::Int64(-42),
FieldValue::Uint64(64),
]),
FieldValue::List((1..3).map(FieldValue::Int64).collect_vec().into()),
FieldValue::List((1..3).map(FieldValue::Uint64).collect_vec().into()),
FieldValue::List(
vec![
// Integer-typed but non-homogeneous FieldValue entries are okay.
FieldValue::Int64(-42),
FieldValue::Uint64(64),
]
.into(),
),
];
let nullable_values = vec![
FieldValue::List(vec![
FieldValue::Int64(1),
FieldValue::Null,
FieldValue::Int64(2),
]),
FieldValue::List(vec![FieldValue::Null, FieldValue::Uint64(42)]),
FieldValue::List(vec![
// Integer-typed but non-homogeneous FieldValue entries are okay.
FieldValue::Int64(-1),
FieldValue::Uint64(1),
FieldValue::Null,
]),
FieldValue::List(
vec![FieldValue::Int64(1), FieldValue::Null, FieldValue::Int64(2)].into(),
),
FieldValue::List(vec![FieldValue::Null, FieldValue::Uint64(42)].into()),
FieldValue::List(
vec![
// Integer-typed but non-homogeneous FieldValue entries are okay.
FieldValue::Int64(-1),
FieldValue::Uint64(1),
FieldValue::Null,
]
.into(),
),
];

for value in &non_nullable_values {
Expand Down
Loading