From 539037a468a5a26041c61819289d52ad6c770c5d Mon Sep 17 00:00:00 2001 From: DavePearce Date: Tue, 5 Nov 2024 15:27:32 +1300 Subject: [PATCH] Remove some occurrences of *fr.Element This is an extremely minor optimisation which attempts to reduce pressure on the heap. --- pkg/binfile/constraint_set.go | 2 +- pkg/hir/parser.go | 2 +- pkg/schema/type.go | 8 ++++---- pkg/util/arrays.go | 16 ---------------- 4 files changed, 6 insertions(+), 22 deletions(-) diff --git a/pkg/binfile/constraint_set.go b/pkg/binfile/constraint_set.go index bbee210..a17f04c 100644 --- a/pkg/binfile/constraint_set.go +++ b/pkg/binfile/constraint_set.go @@ -176,7 +176,7 @@ func allocateRegisters(cs *constraintSet, schema *hir.Schema) map[uint]uint { // Check whether a type constraint required or not. if c.MustProve && col_type.AsUint() != nil { bound := col_type.AsUint().Bound() - schema.AddRangeConstraint(c.Handle, ctx, &hir.ColumnAccess{Column: cid, Shift: 0}, *bound) + schema.AddRangeConstraint(c.Handle, ctx, &hir.ColumnAccess{Column: cid, Shift: 0}, bound) } } } diff --git a/pkg/hir/parser.go b/pkg/hir/parser.go index 74713eb..37eb363 100644 --- a/pkg/hir/parser.go +++ b/pkg/hir/parser.go @@ -181,7 +181,7 @@ func (p *hirParser) parseColumnDeclaration(e sexp.SExp) error { // Apply type constraint (if applicable) if columnType.AsUint() != nil { bound := columnType.AsUint().Bound() - p.env.schema.AddRangeConstraint(columnName, p.module, &ColumnAccess{cid, 0}, *bound) + p.env.schema.AddRangeConstraint(columnName, p.module, &ColumnAccess{cid, 0}, bound) } // return nil diff --git a/pkg/schema/type.go b/pkg/schema/type.go index 7aac5dc..1801fd6 100644 --- a/pkg/schema/type.go +++ b/pkg/schema/type.go @@ -33,7 +33,7 @@ type UintType struct { // The number of bits this type represents (e.g. 8 for u8, etc). nbits uint // The numeric bound of all values in this type (e.g. 2^8 for u8, etc). - bound *fr.Element + bound fr.Element } // NewUintType constructs a new integer type for a given bit width. @@ -45,7 +45,7 @@ func NewUintType(nbits uint) *UintType { bound := new(fr.Element) bound.SetBigInt(&maxBigInt) - return &UintType{nbits, bound} + return &UintType{nbits, *bound} } // AsUint accesses this type assuming it is a Uint. Since this is the case, @@ -76,7 +76,7 @@ func (p *UintType) ByteWidth() uint { // Accept determines whether a given value is an element of this type. For // example, 123 is an element of the type u8 whilst 256 is not. func (p *UintType) Accept(val fr.Element) bool { - return val.Cmp(p.bound) < 0 + return val.Cmp(&p.bound) < 0 } // BitWidth returns the bitwidth of this type. For example, the @@ -94,7 +94,7 @@ func (p *UintType) HasBound(bound uint) bool { } // Bound determines the actual bound for all values which are in this type. -func (p *UintType) Bound() *fr.Element { +func (p *UintType) Bound() fr.Element { return p.bound } diff --git a/pkg/util/arrays.go b/pkg/util/arrays.go index da8f683..bf671d1 100644 --- a/pkg/util/arrays.go +++ b/pkg/util/arrays.go @@ -48,22 +48,6 @@ func RemoveMatching[T any](items []T, predicate Predicate[T]) []T { return items } -// Equals returns true if both arrays contain equivalent elements. -func Equals(lhs []*fr.Element, rhs []*fr.Element) bool { - if len(lhs) != len(rhs) { - return false - } - - for i := 0; i < len(lhs); i++ { - // Check lengths match - if lhs[i].Cmp(rhs[i]) != 0 { - return false - } - } - // - return true -} - // Equals2d returns true if two 2D arrays are equal. func Equals2d(lhs [][]fr.Element, rhs [][]fr.Element) bool { if len(lhs) != len(rhs) {