From ccbfaf6a202cc7c5fe13f7c408c3039ef7fd998a Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Wed, 28 Apr 2021 14:21:45 +1200 Subject: [PATCH] Remove large temprary stack allocations from SqrtTables::new --- src/arithmetic/fields.rs | 43 ++++++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/src/arithmetic/fields.rs b/src/arithmetic/fields.rs index 59bd863..664aade 100644 --- a/src/arithmetic/fields.rs +++ b/src/arithmetic/fields.rs @@ -199,37 +199,42 @@ impl SqrtTables { marker: PhantomData, }; - let gtab: Vec> = (0..4) - .scan(F::ROOT_OF_UNITY, |gi, _| { - // gi == ROOT_OF_UNITY^(256^i) - let gtab_i: Vec = (0..256) - .scan(F::one(), |acc, _| { - let res = *acc; - *acc *= *gi; - Some(res) - }) - .collect(); - *gi = gtab_i[255] * *gi; - Some(gtab_i) - }) - .collect(); + let mut gtab = (0..4).scan(F::ROOT_OF_UNITY, |gi, _| { + // gi == ROOT_OF_UNITY^(256^i) + let gtab_i: Vec = (0..256) + .scan(F::one(), |acc, _| { + let res = *acc; + *acc *= *gi; + Some(res) + }) + .collect(); + *gi = gtab_i[255] * *gi; + Some(gtab_i) + }); + let gtab_0 = gtab.next().unwrap(); + let gtab_1 = gtab.next().unwrap(); + let gtab_2 = gtab.next().unwrap(); + let mut gtab_3 = gtab.next().unwrap(); + assert_eq!(gtab.next(), None); // Now invert gtab[3]. let mut inv: Vec = vec![1; hash_mod]; for j in 0..256 { - let hash = hasher.hash(>ab[3][j]); + let hash = hasher.hash(>ab_3[j]); // 1 is the last value to be assigned, so this ensures there are no collisions. assert!(inv[hash] == 1); inv[hash] = ((256 - j) & 0xFF) as u8; } + gtab_3.truncate(129); + SqrtTables:: { hasher, inv, - g0: Box::new(gtab[0][..].try_into().unwrap()), - g1: Box::new(gtab[1][..].try_into().unwrap()), - g2: Box::new(gtab[2][..].try_into().unwrap()), - g3: Box::new(gtab[3][0..129].try_into().unwrap()), + g0: gtab_0.into_boxed_slice().try_into().unwrap(), + g1: gtab_1.into_boxed_slice().try_into().unwrap(), + g2: gtab_2.into_boxed_slice().try_into().unwrap(), + g3: gtab_3.into_boxed_slice().try_into().unwrap(), } }