Skip to content

Commit c55d826

Browse files
perf: Don't allocate when hashing FRI table rows
1 parent d284df4 commit c55d826

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

triton-vm/src/table/master_table.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,36 @@ use crate::table::auxiliary_table::all_degrees_with_origin;
165165
use crate::table::degree_lowering::DegreeLoweringTable;
166166
use crate::table::processor::ClkJumpDiffs;
167167

168+
/// Helper trait to turn a slice of either [`BFieldElement`]s or
169+
/// [`XFieldElement`]s into a slice of [`BFieldElement`]s.
170+
///
171+
/// In particular, this is helpful when writing code that is generic over the
172+
/// two fields but should also perform well, i.e., where calling
173+
/// [`BFieldCodec::encode`] (which performs allocations) is not a good idea.
174+
pub(crate) trait BfeSlice: FiniteField {
175+
fn bfe_slice(slice: &[Self]) -> &[BFieldElement];
176+
}
177+
178+
impl BfeSlice for BFieldElement {
179+
fn bfe_slice(slice: &[Self]) -> &[BFieldElement] {
180+
slice
181+
}
182+
}
183+
184+
impl BfeSlice for XFieldElement {
185+
fn bfe_slice(slice: &[Self]) -> &[BFieldElement] {
186+
x_field_element::as_flat_slice(slice)
187+
}
188+
}
189+
168190
pub(crate) trait MasterTable: Sync
169191
where
170192
StandardUniform: Distribution<Self::Field>,
171193
XFieldElement: Add<Self::Field, Output = XFieldElement>
172194
// _no_ clue why this is necessary
173195
+ Add<XFieldElement, Output = XFieldElement>,
174196
{
175-
type Field: FiniteField
197+
type Field: BfeSlice
176198
+ Add<BFieldElement, Output = Self::Field>
177199
+ MulAssign<BFieldElement>
178200
+ From<BFieldElement>
@@ -423,7 +445,9 @@ where
423445
let all_digests = fri_domain_table
424446
.axis_iter(ROW_AXIS)
425447
.into_par_iter()
426-
.map(|row| Tip5::hash_varlen(&row.iter().flat_map(|e| e.encode()).collect_vec()))
448+
.map(|row| row.to_slice().unwrap())
449+
.map(Self::Field::bfe_slice)
450+
.map(Tip5::hash_varlen)
427451
.collect();
428452
profiler!(stop "hash rows");
429453

@@ -453,7 +477,9 @@ where
453477
sponge_states
454478
.par_iter_mut()
455479
.zip(codewords.axis_iter(ROW_AXIS))
456-
.for_each(|(sponge, row)| sponge.absorb(row.iter().flat_map(|e| e.encode())));
480+
.for_each(|(sponge, row)| {
481+
sponge.absorb(Self::Field::bfe_slice(row.to_slice().unwrap()))
482+
});
457483
profiler!(stop "hash rows");
458484
}
459485

0 commit comments

Comments
 (0)