halo2
halo2 copied to clipboard
Make Expression copiable using singleton arena
NOTE: Relevant files to review are halo2_frontend/src/plonk/circuit/{expession.rs, arena.rs}
This an experiment about making expression copiable. To implement this we go with this strategy:
- Create a type called
ExprRef<F>that contains the index (usize) of an expression stored in an specific arena. - Modify the struct
Expressionto useExprRefinstead the a recursiveExpressionthat makes it non-copiable. - In order to access to the Expression arenas, we create a new trait that allows to access to the expression arena for an specific field, called
FieldFront. Mainly we change all frontend fromFieldtoFieldFront, making arena accessible everywhere.
pub trait FieldFront: Field {
// Since base trait is not referenciable, we need a way to access it.
// This is necessary to provide a way to transform fields from/to backend.
type Field: Field;
fn into_field(self) -> Self::Field;
fn into_fieldfront(f: Self::Field) -> Self;
// Allocate a new expression
fn alloc(expr: Expression<Self>) -> ExprRef<Self>;
// Get an expression
fn get(ref_: &ExprRef<Self>) -> Expression<Self>;
// Replace an expression
fn replace(expr: Expression<Self>, ref_: &ExprRef<Self>);
}
This means:
- Library users needs to change
<Field>for<FieldFront>and automatically all theirExpressionsare automatically copiable. This is thread-safe. - Arenas are not dropped and lives until the whole program is finished.
- Arenas are field-especific and cannot be defined outside the halo2_crate ( this is due that is not possible to implement external traits on external types ).
Codecov Report
Attention: Patch coverage is 81.97065% with 86 lines in your changes missing coverage. Please review.
Project coverage is 81.81%. Comparing base (
32599e8) to head (c8c7ff9). Report is 1 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #354 +/- ##
==========================================
- Coverage 82.09% 81.81% -0.29%
==========================================
Files 83 83
Lines 17228 17452 +224
==========================================
+ Hits 14143 14278 +135
- Misses 3085 3174 +89
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.