Skip to content

Commit

Permalink
perf: reduce format!-related allocations and move them to closures wh…
Browse files Browse the repository at this point in the history
…ere possible

Signed-off-by: ljedrz <ljedrz@gmail.com>
  • Loading branch information
ljedrz committed Oct 22, 2020
1 parent 63f80c0 commit 4ccc9c3
Show file tree
Hide file tree
Showing 14 changed files with 121 additions and 127 deletions.
16 changes: 9 additions & 7 deletions compiler/src/expression/function/function.rs
Expand Up @@ -65,15 +65,17 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {

let (outer_scope, function_call) = function_value.extract_function(file_scope, &span)?;

let name_unique = format!(
"function call {} {}:{}",
function_call.get_name(),
span.line,
span.start,
);
let name_unique = || {
format!(
"function call {} {}:{}",
function_call.get_name(),
span.line,
span.start,
)
};

self.enforce_function(
&mut cs.ns(|| name_unique),
&mut cs.ns(name_unique),
&outer_scope,
function_scope,
function_call,
Expand Down
9 changes: 6 additions & 3 deletions compiler/src/expression/logical/and.rs
Expand Up @@ -33,9 +33,12 @@ pub fn enforce_and<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<
let name = format!("{} && {}", left, right);

if let (ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) = (left, right) {
let name_unique = format!("{} {}:{}", name, span.line, span.start);
let result = Boolean::and(cs.ns(|| name_unique), &left_bool, &right_bool)
.map_err(|e| BooleanError::cannot_enforce("&&".to_string(), e, span.to_owned()))?;
let result = Boolean::and(
cs.ns(|| format!("{} {}:{}", name, span.line, span.start)),
&left_bool,
&right_bool,
)
.map_err(|e| BooleanError::cannot_enforce("&&".to_string(), e, span.to_owned()))?;

return Ok(ConstrainedValue::Boolean(result));
}
Expand Down
9 changes: 6 additions & 3 deletions compiler/src/expression/logical/or.rs
Expand Up @@ -33,9 +33,12 @@ pub fn enforce_or<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F
let name = format!("{} || {}", left, right);

if let (ConstrainedValue::Boolean(left_bool), ConstrainedValue::Boolean(right_bool)) = (left, right) {
let name_unique = format!("{} {}:{}", name, span.line, span.start);
let result = Boolean::or(cs.ns(|| name_unique), &left_bool, &right_bool)
.map_err(|e| BooleanError::cannot_enforce("||".to_string(), e, span.to_owned()))?;
let result = Boolean::or(
cs.ns(|| format!("{} {}:{}", name, span.line, span.start)),
&left_bool,
&right_bool,
)
.map_err(|e| BooleanError::cannot_enforce("||".to_string(), e, span.to_owned()))?;

return Ok(ConstrainedValue::Boolean(result));
}
Expand Down
13 changes: 7 additions & 6 deletions compiler/src/function/result/result.rs
Expand Up @@ -61,12 +61,13 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
}

let condition = indicator.unwrap_or(Boolean::Constant(true));
let name_unique = format!("select {} {}:{}", result, span.line, span.start);
let selected_value =
ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &result, return_value)
.map_err(|_| {
StatementError::select_fail(result.to_string(), return_value.to_string(), span.to_owned())
})?;
let selected_value = ConstrainedValue::conditionally_select(
cs.ns(|| format!("select {} {}:{}", result, span.line, span.start)),
&condition,
&result,
return_value,
)
.map_err(|_| StatementError::select_fail(result.to_string(), return_value.to_string(), span.to_owned()))?;

*return_value = selected_value;
}
Expand Down
18 changes: 10 additions & 8 deletions compiler/src/statement/assign/array.rs
Expand Up @@ -52,9 +52,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
ConstrainedValue::Array(old) => {
new_value.resolve_type(Some(old[index].to_type(&span)?), &span)?;

let name_unique = format!("select {} {}:{}", new_value, span.line, span.start);
let selected_value = ConstrainedValue::conditionally_select(
cs.ns(|| name_unique),
cs.ns(|| format!("select {} {}:{}", new_value, span.line, span.start)),
&condition,
&new_value,
&old[index],
Expand Down Expand Up @@ -89,12 +88,15 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
}
_ => return Err(StatementError::array_assign_range(span.to_owned())),
};
let name_unique = format!("select {} {}:{}", new_array, span.line, span.start);
let selected_array =
ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &new_array, old_array)
.map_err(|_| {
StatementError::select_fail(new_array.to_string(), old_array.to_string(), span.to_owned())
})?;
let selected_array = ConstrainedValue::conditionally_select(
cs.ns(|| format!("select {} {}:{}", new_array, span.line, span.start)),
&condition,
&new_array,
old_array,
)
.map_err(|_| {
StatementError::select_fail(new_array.to_string(), old_array.to_string(), span.to_owned())
})?;

*old_array = selected_array;
}
Expand Down
15 changes: 9 additions & 6 deletions compiler/src/statement/assign/assign.rs
Expand Up @@ -61,12 +61,15 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {

new_value.resolve_type(Some(old_value.to_type(&span)?), &span)?;

let name_unique = format!("select {} {}:{}", new_value, span.line, span.start);
let selected_value =
ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &new_value, old_value)
.map_err(|_| {
StatementError::select_fail(new_value.to_string(), old_value.to_string(), span.to_owned())
})?;
let selected_value = ConstrainedValue::conditionally_select(
cs.ns(|| format!("select {} {}:{}", new_value, span.line, span.start)),
&condition,
&new_value,
old_value,
)
.map_err(|_| {
StatementError::select_fail(new_value.to_string(), old_value.to_string(), span.to_owned())
})?;

*old_value = selected_value;

Expand Down
3 changes: 1 addition & 2 deletions compiler/src/statement/assign/circuit_variable.rs
Expand Up @@ -68,9 +68,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
new_value.resolve_type(Some(value.to_type(span)?), span)?;

// Conditionally select the value if this branch is executed.
let name_unique = format!("select {} {}:{}", new_value, span.line, span.start);
let mut selected_value = ConstrainedValue::conditionally_select(
cs.ns(|| name_unique),
cs.ns(|| format!("select {} {}:{}", new_value, span.line, span.start)),
&condition,
&new_value,
&member.1,
Expand Down
15 changes: 9 additions & 6 deletions compiler/src/statement/assign/tuple.rs
Expand Up @@ -44,12 +44,15 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
ConstrainedValue::Tuple(old) => {
new_value.resolve_type(Some(old[index].to_type(&span)?), &span)?;

let name_unique = format!("select {} {}:{}", new_value, span.line, span.start);
let selected_value =
ConstrainedValue::conditionally_select(cs.ns(|| name_unique), &condition, &new_value, &old[index])
.map_err(|_| {
StatementError::select_fail(new_value.to_string(), old[index].to_string(), span.to_owned())
})?;
let selected_value = ConstrainedValue::conditionally_select(
cs.ns(|| format!("select {} {}:{}", new_value, span.line, span.start)),
&condition,
&new_value,
&old[index],
)
.map_err(|_| {
StatementError::select_fail(new_value.to_string(), old[index].to_string(), span.to_owned())
})?;

old[index] = selected_value;
}
Expand Down
3 changes: 1 addition & 2 deletions compiler/src/statement/iteration/iteration.rs
Expand Up @@ -67,9 +67,8 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
);

// Evaluate statements and possibly return early
let name_unique = format!("for loop iteration {} {}:{}", i, span.line, span.start);
let mut result = self.evaluate_branch(
&mut cs.ns(|| name_unique),
&mut cs.ns(|| format!("for loop iteration {} {}:{}", i, span.line, span.start)),
file_scope,
function_scope,
indicator,
Expand Down
10 changes: 4 additions & 6 deletions compiler/src/value/address/address.rs
Expand Up @@ -81,12 +81,10 @@ impl Address {
None => None,
};

let address_name = format!("{}: address", name);
let address_namespace = format!("`{}` {}:{}", address_name, span.line, span.start);

let address = Address::alloc(cs.ns(|| address_namespace), || {
address_value.ok_or(SynthesisError::AssignmentMissing)
})
let address = Address::alloc(
cs.ns(|| format!("`{}: address` {}:{}", name, span.line, span.start)),
|| address_value.ok_or(SynthesisError::AssignmentMissing),
)
.map_err(|_| AddressError::missing_address(span.to_owned()))?;

Ok(ConstrainedValue::Address(address))
Expand Down
12 changes: 5 additions & 7 deletions compiler/src/value/boolean/input.rs
Expand Up @@ -42,13 +42,11 @@ pub(crate) fn allocate_bool<F: Field + PrimeField, CS: ConstraintSystem<F>>(
option: Option<bool>,
span: &Span,
) -> Result<Boolean, BooleanError> {
let boolean_name = format!("{}: bool", name);
let boolean_name_unique = format!("`{}` {}:{}", boolean_name, span.line, span.start);

Boolean::alloc(cs.ns(|| boolean_name_unique), || {
option.ok_or(SynthesisError::AssignmentMissing)
})
.map_err(|_| BooleanError::missing_boolean(boolean_name.to_owned(), span.to_owned()))
Boolean::alloc(
cs.ns(|| format!("`{}: bool` {}:{}", name, span.line, span.start)),
|| option.ok_or(SynthesisError::AssignmentMissing),
)
.map_err(|_| BooleanError::missing_boolean(format!("{}: bool", name), span.to_owned()))
}

pub(crate) fn bool_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
Expand Down
12 changes: 5 additions & 7 deletions compiler/src/value/field/input.rs
Expand Up @@ -31,13 +31,11 @@ pub(crate) fn allocate_field<F: Field + PrimeField, CS: ConstraintSystem<F>>(
option: Option<String>,
span: &Span,
) -> Result<FieldType<F>, FieldError> {
let field_name = format!("{}: field", name);
let field_name_unique = format!("`{}` {}:{}", field_name, span.line, span.start);

FieldType::alloc(cs.ns(|| field_name_unique), || {
option.ok_or(SynthesisError::AssignmentMissing)
})
.map_err(|_| FieldError::missing_field(field_name, span.to_owned()))
FieldType::alloc(
cs.ns(|| format!("`{}: field` {}:{}", name, span.line, span.start)),
|| option.ok_or(SynthesisError::AssignmentMissing),
)
.map_err(|_| FieldError::missing_field(format!("{}: field", name), span.to_owned()))
}

pub(crate) fn field_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
Expand Down
12 changes: 5 additions & 7 deletions compiler/src/value/group/input.rs
Expand Up @@ -31,13 +31,11 @@ pub(crate) fn allocate_group<F: Field + PrimeField, G: GroupType<F>, CS: Constra
option: Option<GroupValue>,
span: &Span,
) -> Result<G, GroupError> {
let group_name = format!("{}: group", name);
let group_name_unique = format!("`{}` {}:{}", group_name, span.line, span.start);

G::alloc(cs.ns(|| group_name_unique), || {
option.ok_or(SynthesisError::AssignmentMissing)
})
.map_err(|_| GroupError::missing_group(group_name, span.to_owned()))
G::alloc(
cs.ns(|| format!("`{}: group` {}:{}", name, span.line, span.start)),
|| option.ok_or(SynthesisError::AssignmentMissing),
)
.map_err(|_| GroupError::missing_group(format!("{}: group", name), span.to_owned()))
}

pub(crate) fn group_from_input<F: Field + PrimeField, G: GroupType<F>, CS: ConstraintSystem<F>>(
Expand Down

0 comments on commit 4ccc9c3

Please sign in to comment.