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

[Perf] Use SmallVec for some temporary allocations #3

Open
wants to merge 4 commits into
base: mainnet-staging
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion algorithms/src/crypto_hash/poseidon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ impl<F: PrimeField, const RATE: usize> PoseidonSponge<F, RATE, 1> {
};
let bits = self.get_bits(num_bits_per_nonnative * num_elements);

let mut lookup_table = Vec::<TargetField>::with_capacity(num_bits_per_nonnative);
let mut lookup_table: SmallVec<[TargetField; 256]> = SmallVec::new();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could it make sense to still use num_bits_per_nonnative?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sadly, it's not const, so we can't use it

let mut cur = TargetField::one();
for _ in 0..num_bits_per_nonnative {
lookup_table.push(cur);
Expand Down
5 changes: 5 additions & 0 deletions console/types/field/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ version = "=0.16.19"
path = "../boolean"
version = "=0.16.19"

[dependencies.smallvec]
version = "1.11"
features = [ "const_new" ]
default-features = false

[dependencies.zeroize]
version = "1"
features = [ "derive" ]
Expand Down
4 changes: 3 additions & 1 deletion console/types/field/src/from_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use super::*;

use smallvec::{smallvec, SmallVec};

impl<E: Environment> FromBits for Field<E> {
/// Initializes a new field from a list of **little-endian** bits.
/// - If `bits_le` is longer than `E::Field::size_in_bits()`, the excess bits are enforced to be `0`s.
Expand Down Expand Up @@ -46,7 +48,7 @@ impl<E: Environment> FromBits for Field<E> {
Ok(Field { field: E::Field::from_bigint(field).ok_or_else(|| anyhow!("Invalid field from bits"))? })
} else {
// Construct the sanitized list of bits padded with `false`
let mut sanitized_bits = vec![false; size_in_bits];
let mut sanitized_bits: SmallVec<[bool; 384]> = smallvec![false; size_in_bits];
vicsn marked this conversation as resolved.
Show resolved Hide resolved
// Note: This is safe, because we just checked that the length of bits isn't bigger
// than `size_in_data_bits` which is equal to `size_in_bits - 1`.
sanitized_bits[..num_bits].copy_from_slice(bits_le);
Expand Down
5 changes: 5 additions & 0 deletions curves/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ version = "1.0.188"
default-features = false
features = [ "derive" ]

[dependencies.smallvec]
version = "1.11"
features = [ "const_new" ]
default-features = false

[dependencies.thiserror]
version = "1.0"

Expand Down
11 changes: 8 additions & 3 deletions curves/src/bls12_377/g1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use crate::{
ProjectiveCurve,
};

use smallvec::SmallVec;
use std::ops::Neg;

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
Expand Down Expand Up @@ -145,8 +146,8 @@ impl ShortWeierstrassParameters for Bls12_377G1Parameters {
let d_mod_window_size = i64::try_from(d & MASK_FOR_MOD_TABLE_SIZE).unwrap();
if d_mod_window_size >= HALF_TABLE_SIZE { d_mod_window_size - TABLE_SIZE } else { d_mod_window_size }
};
let to_wnaf = |e: Self::ScalarField| -> Vec<i32> {
let mut naf = vec![];
let to_wnaf = |e: Self::ScalarField| -> SmallVec<[i32; 128]> {
let mut naf: SmallVec<[i32; 128]> = SmallVec::new();
vicsn marked this conversation as resolved.
Show resolved Hide resolved
let mut e = e.to_bigint();
while !e.is_zero() {
let next = if e.is_odd() {
Expand All @@ -167,7 +168,11 @@ impl ShortWeierstrassParameters for Bls12_377G1Parameters {
naf
};

let wnaf = |k1: Self::ScalarField, k2: Self::ScalarField, s1: bool, s2: bool| -> (Vec<i32>, Vec<i32>) {
let wnaf = |k1: Self::ScalarField,
k2: Self::ScalarField,
s1: bool,
s2: bool|
-> (SmallVec<[i32; 128]>, SmallVec<[i32; 128]>) {
let mut wnaf_1 = to_wnaf(k1);
let mut wnaf_2 = to_wnaf(k2);

Expand Down