Skip to content

Perf pure go cairo0 class hash - #3648

Merged
rodrodros merged 12 commits into
mainfrom
perf-pure-go-cairo0-class-hash
May 20, 2026
Merged

Perf pure go cairo0 class hash#3648
rodrodros merged 12 commits into
mainfrom
perf-pure-go-cairo0-class-hash

Conversation

@ongyimeng

@ongyimeng ongyimeng commented May 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Replace Cairo0 class hashing with a pure-Go implementation and optimize the path.

Changes

  • reimplement legacy Cairo0 hinted-hash canonicalization in Go
  • remove the old Rust/FFI Cairo0 hashing path
  • optimize Pedersen hashing and table initialization
  • parallelize independent work in deprecated Cairo class hashing
  • add stronger regression and parity tests

Results

These results are from the benchmark, BenchmarkClassV0Hash.

Runtime improved compared to the latest upstream main Rust/FFI path:

  • Smaller fixture: ~7.52 ms/op -> ~6.08 ms/op, about 19.2% faster.
  • Larger fixture: ~79.67 ms/op -> ~59.46 ms/op, about 25.4% faster.

Go memory metrics use the initial pure-Go baseline since Go benchmarks do not count Rust-side FFI allocations.

Go memory metrics improved compared to the initial pure-Go baseline:

  • Smaller fixture:
    • ~541,656 B/op -> ~458,824 B/op, about 15.3% lower.
    • 3,025 allocs/op -> 1,881 allocs/op, about 37.8% fewer.
  • Larger fixture:
    • ~3.40 MB/op -> ~2.49 MB/op, about 26.8% lower.
    • ~19.39k allocs/op -> 12,042 allocs/op, about 37.9% fewer.

Memory tradeoff:

  • Pedersen 8-bit precomputed tables retain about 1 MiB.
  • Rough import-time process RSS delta is about 6.1 MiB. (measured by comparing an empty process against one that imports core/crypto).

Benchmarks were run on macOS, Intel i5, with GOMAXPROCS=1

Motivation

Issue #2084

@codecov

codecov Bot commented May 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.43544% with 97 lines in your changes missing coverage. Please review.
✅ Project coverage is 76.21%. Comparing base (bfe781f) to head (a25522f).

Files with missing lines Patch % Lines
core/deprecated_cairo_program.go 84.64% 52 Missing and 26 partials ⚠️
core/crypto/pedersen_hash.go 83.50% 11 Missing and 5 partials ⚠️
core/class_hash.go 96.15% 1 Missing and 1 partial ⚠️
core/crypto/keccak.go 88.88% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3648      +/-   ##
==========================================
+ Coverage   75.96%   76.21%   +0.24%     
==========================================
  Files         395      396       +1     
  Lines       35780    36424     +644     
==========================================
+ Hits        27182    27759     +577     
- Misses       6635     6688      +53     
- Partials     1963     1977      +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ongyimeng
ongyimeng force-pushed the perf-pure-go-cairo0-class-hash branch from 574ac2f to 3933b97 Compare May 18, 2026 11:59
@ongyimeng
ongyimeng marked this pull request as ready for review May 18, 2026 12:22
Comment thread core/crypto/pedersen_hash.go
Comment thread core/deprecated_cairo_program.go Outdated
Comment thread core/deprecated_cairo_program.go Outdated

@RafaelGranza RafaelGranza left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Really solid work. The test setup with starknet-rs fixtures and the snapshot are great touches.

Could you walk through the comments before merging? Most are nits or future direction ideas, no blocking items.

Comment thread core/deprecated_cairo_program_test.go
Comment thread core/crypto/pedersen_hash_test.go
Comment thread core/class_hash.go
Comment thread core/class_hash.go
Comment thread core/deprecated_cairo_program.go
@RafaelGranza

RafaelGranza commented May 18, 2026

Copy link
Copy Markdown
Contributor

Sorry, misclicked and updated your branch with a merge instead of a rebase.
Heads up in case you want to clean it up.

@rodrodros rodrodros left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All looking good so far, blocking until I get a good grasp of the code myself

@ongyimeng
ongyimeng force-pushed the perf-pure-go-cairo0-class-hash branch from 95c9515 to 841e84e Compare May 19, 2026 09:03
Comment thread core/deprecated_cairo_program_test.go Outdated
Comment thread core/deprecated_cairo_program_test.go Outdated

@rodrodros rodrodros left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@ongyimeng looks good, can you add the benchmark tests to the diff as well 🙏

Comment thread core/class_hash.go
@ongyimeng
ongyimeng force-pushed the perf-pure-go-cairo0-class-hash branch from 841e84e to 1dd8b46 Compare May 19, 2026 15:14
Instead of storing in a buffer all the bytes of a Cairo 0 Class, chunk
them in a local 4kb buffer that every time it gets filled, it streams it
into the Starknet Keccak Digest. Avoiding memory spikes and reducing
allocations.

@rodrodros rodrodros left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Good work!

@rodrodros
rodrodros merged commit c5570cb into main May 20, 2026
32 checks passed
@rodrodros
rodrodros deleted the perf-pure-go-cairo0-class-hash branch May 20, 2026 10:25
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.

3 participants