perf: vector pre-allocation and associated tweaks

Signed-off-by: ljedrz <ljedrz@gmail.com>
This commit is contained in:
ljedrz 2020-10-06 17:04:52 +02:00
parent 98baae93c1
commit e1e22a2310
18 changed files with 38 additions and 49 deletions

View File

@ -53,7 +53,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
};
let circuit_identifier = circuit.circuit_name.clone();
let mut resolved_members = vec![];
let mut resolved_members = Vec::with_capacity(circuit.members.len());
for member in circuit.members.into_iter() {
match member {

View File

@ -37,7 +37,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
span: Span,
) -> Result<ConstrainedValue<F, G>, ExpressionError> {
// Get the value of each core function argument
let mut argument_values = vec![];
let mut argument_values = Vec::with_capacity(arguments.len());
for argument in arguments.into_iter() {
let argument_value =
self.enforce_expression(cs, file_scope.clone(), function_scope.clone(), None, argument)?;

View File

@ -52,7 +52,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
None => {}
}
let mut result = vec![];
let mut result = Vec::with_capacity(tuple.len());
for (i, expression) in tuple.into_iter().enumerate() {
let type_ = if expected_types.is_empty() {
None

View File

@ -62,14 +62,14 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
// Allocate each input variable as a circuit expression
let mut sections = vec![];
let mut sections = Vec::with_capacity(4);
sections.push((registers_name, registers_values));
sections.push((record_name, record_values));
sections.push((state_name, state_values));
sections.push((state_leaf_name, state_leaf_values));
let mut members = vec![];
let mut members = Vec::with_capacity(sections.len());
for (name, values) in sections {
let member_name = name.clone();

View File

@ -30,7 +30,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
identifier: Identifier,
section: HashMap<Parameter, Option<InputValue>>,
) -> Result<ConstrainedValue<F, G>, FunctionError> {
let mut members = vec![];
let mut members = Vec::with_capacity(section.len());
// Allocate each section definition as a circuit member value

View File

@ -42,7 +42,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
let registers = input.get_registers();
// Iterate over main function input variables and allocate new values
let mut input_variables = vec![];
let mut input_variables = Vec::with_capacity(function.input.len());
for input_model in function.input.clone().into_iter() {
let (identifier, value) = match input_model {
InputVariable::InputKeyword(identifier) => {

View File

@ -71,8 +71,7 @@ impl OutputBytes {
string.push_str(&format);
}
let mut bytes: Vec<u8> = vec![];
bytes.extend_from_slice(string.as_bytes());
let bytes = string.into_bytes();
Ok(Self(bytes))
}

View File

@ -34,15 +34,15 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
statements: Vec<Statement>,
return_type: Option<Type>,
) -> StatementResult<Vec<IndicatorAndConstrainedValue<F, G>>> {
let mut results = vec![];
let mut results = Vec::with_capacity(statements.len());
// Evaluate statements. Only allow a single return argument to be returned.
for statement in statements.iter() {
for statement in statements.into_iter() {
let mut value = self.enforce_statement(
cs,
file_scope.clone(),
function_scope.clone(),
indicator,
statement.clone(),
statement,
return_type.clone(),
"".to_owned(),
)?;

View File

@ -61,7 +61,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
};
let implicit_types = types.is_empty();
let mut expected_types = vec![];
let mut expected_types = Vec::with_capacity(expressions.len());
for ty in types.iter().take(expressions.len()) {
let expected_type = if implicit_types { None } else { Some(ty.clone()) };
@ -69,7 +69,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedProgram<F, G> {
expected_types.push(expected_type);
}
let mut values = vec![];
let mut values = Vec::with_capacity(expressions.len());
for (expression, expected_type) in expressions.into_iter().zip(expected_types.into_iter()) {
let value = self.enforce_expression(

View File

@ -122,7 +122,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> ConstrainedValue<F, G> {
Type::Array(Box::new(array_type), dimensions)
}
ConstrainedValue::Tuple(tuple) => {
let mut types = vec![];
let mut types = Vec::with_capacity(tuple.len());
for value in tuple {
let type_ = value.to_type(span.clone())?;
@ -448,7 +448,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> CondSelectGadget<F> for Constrained
ConstrainedValue::Integer(Integer::conditionally_select(cs, cond, num_1, num_2)?)
}
(ConstrainedValue::Array(arr_1), ConstrainedValue::Array(arr_2)) => {
let mut array = vec![];
let mut array = Vec::with_capacity(arr_1.len());
for (i, (first, second)) in arr_1.iter().zip(arr_2.iter()).enumerate() {
array.push(Self::conditionally_select(
@ -462,7 +462,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> CondSelectGadget<F> for Constrained
ConstrainedValue::Array(array)
}
(ConstrainedValue::Tuple(tuple_1), ConstrainedValue::Array(tuple_2)) => {
let mut array = vec![];
let mut array = Vec::with_capacity(tuple_1.len());
for (i, (first, second)) in tuple_1.iter().zip(tuple_2.iter()).enumerate() {
array.push(Self::conditionally_select(
@ -484,7 +484,7 @@ impl<F: Field + PrimeField, G: GroupType<F>> CondSelectGadget<F> for Constrained
ConstrainedValue::CircuitExpression(identifier, members_1),
ConstrainedValue::CircuitExpression(_identifier, members_2),
) => {
let mut members = vec![];
let mut members = Vec::with_capacity(members_1.len());
for (i, (first, second)) in members_1.iter().zip(members_2.iter()).enumerate() {
members.push(ConstrainedCircuitMember::conditionally_select(

View File

@ -158,7 +158,7 @@ fn check_array_bytes(value: Value, size: usize, span: Span) -> Result<Vec<UInt8>
return Err(CoreCircuitError::array_length(size, array_value.len(), span));
}
let mut array_bytes = vec![];
let mut array_bytes = Vec::with_capacity(array_value.len());
for value in array_value {
let byte = match value {

View File

@ -33,7 +33,7 @@ where
// Generic impl
impl<F: Field> RippleCarryAdder<F> for Vec<Boolean> {
fn add_bits<CS: ConstraintSystem<F>>(&self, mut cs: CS, other: &Self) -> Result<Vec<Boolean>, SynthesisError> {
let mut result = vec![];
let mut result = Vec::with_capacity(self.len() + 1);
let mut carry = Boolean::constant(false);
for (i, (a, b)) in self.iter().zip(other.iter()).enumerate() {
let (sum, next) = Boolean::add(cs.ns(|| format!("rpc {}", i)), a, b, &carry)?;

View File

@ -117,7 +117,7 @@ macro_rules! add_int_impl {
}
// Storage area for the resulting bits
let mut result_bits = vec![];
let mut result_bits = Vec::with_capacity(max_bits);
// Allocate each bit_gadget of the result
let mut coeff = F::one();

View File

@ -84,7 +84,7 @@ macro_rules! mul_int_impl {
a_shifted.truncate(size);
// conditionally add
let mut to_add = vec![];
let mut to_add = Vec::with_capacity(a_shifted.len());
for (j, a_bit) in a_shifted.iter().enumerate() {
let selected_bit = Boolean::conditionally_select(
&mut cs.ns(|| format!("select product bit {} {}", i, j)),
@ -175,7 +175,7 @@ macro_rules! mul_int_impl {
}
// Storage area for the resulting bits
let mut result_bits = vec![];
let mut result_bits = Vec::with_capacity(max_bits);
// Allocate each bit_gadget of the result
let mut coeff = F::one();

View File

@ -58,15 +58,7 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
// Serialize assignments
fn get_serialized_assignments<E: PairingEngine>(assignments: &[E::Fr]) -> Vec<SerializedField> {
let mut serialized = vec![];
for assignment in assignments {
let field = SerializedField::from(assignment);
serialized.push(field);
}
serialized
assignments.iter().map(SerializedField::from).collect()
}
let input_assignment = get_serialized_assignments::<E>(&synthesizer.input_assignment);
@ -76,7 +68,7 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
fn get_serialized_constraints<E: PairingEngine>(
constraints: &[(E::Fr, Index)],
) -> Vec<(SerializedField, SerializedIndex)> {
let mut serialized = vec![];
let mut serialized = Vec::with_capacity(constraints.len());
for &(ref coeff, index) in constraints {
let field = SerializedField::from(coeff);
@ -88,9 +80,9 @@ impl<E: PairingEngine> From<CircuitSynthesizer<E>> for SerializedCircuit {
serialized
}
let mut at = vec![];
let mut bt = vec![];
let mut ct = vec![];
let mut at = Vec::with_capacity(num_constraints);
let mut bt = Vec::with_capacity(num_constraints);
let mut ct = Vec::with_capacity(num_constraints);
for i in 0..num_constraints {
// Serialize at[i]
@ -130,7 +122,7 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
fn get_deserialized_assignments(
assignments: &[SerializedField],
) -> Result<Vec<<Bls12_377 as PairingEngine>::Fr>, FieldError> {
let mut deserialized = vec![];
let mut deserialized = Vec::with_capacity(assignments.len());
for serialized_assignment in assignments {
let field = <Bls12_377 as PairingEngine>::Fr::try_from(serialized_assignment)?;
@ -148,7 +140,7 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
fn get_deserialized_constraints(
constraints: &[(SerializedField, SerializedIndex)],
) -> Result<Vec<(<Bls12_377 as PairingEngine>::Fr, Index)>, FieldError> {
let mut deserialized = vec![];
let mut deserialized = Vec::with_capacity(constraints.len());
for &(ref serialized_coeff, ref serialized_index) in constraints {
let field = <Bls12_377 as PairingEngine>::Fr::try_from(serialized_coeff)?;
@ -160,9 +152,9 @@ impl TryFrom<SerializedCircuit> for CircuitSynthesizer<Bls12_377> {
Ok(deserialized)
}
let mut at = vec![];
let mut bt = vec![];
let mut ct = vec![];
let mut at = Vec::with_capacity(serialized.num_constraints);
let mut bt = Vec::with_capacity(serialized.num_constraints);
let mut ct = Vec::with_capacity(serialized.num_constraints);
for i in 0..serialized.num_constraints {
// Deserialize at[i]

View File

@ -49,7 +49,7 @@ pub fn input_to_u8_vec(input: InputValue) -> Result<Vec<u8>, InputValueError> {
value => return Err(InputValueError::ExpectedBytes(value.to_string())),
};
let mut result_vec = vec![];
let mut result_vec = Vec::with_capacity(input_array.len());
for input in input_array {
let integer_string = input_to_integer_string(input)?;
@ -67,7 +67,7 @@ pub fn input_to_nested_u8_vec(input: InputValue) -> Result<Vec<Vec<u8>>, InputVa
value => return Err(InputValueError::ExpectedBytes(value.to_string())),
};
let mut result_vec = vec![];
let mut result_vec = Vec::with_capacity(inner_arrays.len());
for input_array in inner_arrays {
let array = input_to_u8_vec(input_array)?;

View File

@ -123,7 +123,7 @@ impl InputValue {
Type::Array(array_type)
};
let mut elements = vec![];
let mut elements = Vec::with_capacity(inline.expressions.len());
for expression in inline.expressions.into_iter() {
let element = InputValue::from_expression(inner_array_type.clone(), expression)?;
@ -229,7 +229,7 @@ impl InputValue {
));
}
let mut values = vec![];
let mut values = Vec::with_capacity(tuple_type.types_.len());
for (type_, value) in tuple_type.types_.into_iter().zip(tuple.expressions.into_iter()) {
let value = InputValue::from_expression(type_, value)?;

View File

@ -82,8 +82,7 @@ impl Type {
let type_ = self.clone();
if dimensions.len() > 1 {
let mut next = vec![];
next.extend_from_slice(&dimensions[1..]);
let next = dimensions[1..].to_vec();
return Type::Array(Box::new(type_), next);
}
@ -95,8 +94,7 @@ impl Type {
let type_ = self.clone();
if dimensions.len() > 1 {
let mut next = vec![];
next.extend_from_slice(&dimensions[..dimensions.len() - 1]);
let next = dimensions[..dimensions.len() - 1].to_vec();
return Type::Array(Box::new(type_), next);
}