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: add kzg.UnsafeToBytes and kzg.UnsafeFromBytes methods #499

Merged
merged 4 commits into from
Apr 30, 2024

Conversation

gbotrel
Copy link
Collaborator

@gbotrel gbotrel commented Apr 30, 2024

Description

TLDR; adds a method (no breaking changes) to kzg.SRS object that quickly dumps a binary representation without doing any checks, and optionally, enables to truncate the SRS at writing / reading time. UnsafeFromBytes is 6x time faster than UnsafeReadFrom

godoc:

// UnsafeToBytes returns the binary encoding of the entire SRS memory representation
// It is meant to be use to achieve fast serialization/deserialization and
// is not compatible with WriteTo / ReadFrom. It does not do any validation
// and doesn't encode points in a canonical form.
// @unstable: the format may change in the future
// If maxPkPoints is provided, the number of points in the ProvingKey will be limited to maxPkPoints
func (srs *SRS) UnsafeToBytes(maxPkPoints ...int) ([]byte, error) {

benchmark, 1 <<24 points bls12377

BenchmarkSerializeSRS/WriteTo-10                       1        1183526917 ns/op        2147545088 B/op      550 allocs/op
BenchmarkSerializeSRS/WriteRawTo-10                    1        1249923583 ns/op        4295028640 B/op      542 allocs/op
BenchmarkSerializeSRS/UnsafeToBytes-10                 4         287003656 ns/op        1610821252 B/op      522 allocs/op
BenchmarkDeserializeSRS/UnsafeReadFrom-10                      1        2677718292 ns/op        5922632568 B/op     1597 allocs/op
BenchmarkDeserializeSRS/UnsafeFromBytes-10                     3         419717778 ns/op        2147761242 B/op     1205 allocs/op

Checklist:

  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added tests that prove my fix is effective or that my feature works
  • I did not modify files generated from templates
  • New and existing unit tests pass locally with my changes

@gbotrel gbotrel requested review from ivokub and removed request for ThomasPiellard April 30, 2024 02:29
Copy link
Collaborator

@ivokub ivokub left a comment

Choose a reason for hiding this comment

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

Small comments, but otherwise looks great.

internal/generator/kzg/template/marshal.go.tmpl Outdated Show resolved Hide resolved
internal/generator/kzg/template/kzg.test.go.tmpl Outdated Show resolved Hide resolved
internal/generator/kzg/template/kzg.test.go.tmpl Outdated Show resolved Hide resolved
@gbotrel gbotrel merged commit 516f4df into master Apr 30, 2024
7 checks passed
@gbotrel gbotrel deleted the perf/kzg_serialization branch April 30, 2024 14:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants