Skip to content

Fix (#1598): Optimize VersionedLDBAVLStorage methods to reduce allocations#2276

Open
dimension-drifter wants to merge 1 commit intoergoplatform:masterfrom
dimension-drifter:fix/issue-1598
Open

Fix (#1598): Optimize VersionedLDBAVLStorage methods to reduce allocations#2276
dimension-drifter wants to merge 1 commit intoergoplatform:masterfrom
dimension-drifter:fix/issue-1598

Conversation

@dimension-drifter
Copy link

This PR optimizes the VersionedLDBAVLStorage class and ProverNodeSerializer class to significantly improve performance by reducing unnecessary allocations and using more efficient iteration patterns.

Changes Made

  1. Optimized serializedVisitedNodes method in VersionedLDBAVLStorage
  • Before: Used recursive approach with Array concatenation (++) which creates many intermediate arrays
  • After: Uses ArrayBuilder with stack-based iterative traversal and cfor loops
  • Impact: Eliminates O(n²) array concatenation overhead, reduces GC pressure significantly
  1. Optimized update method in VersionedLDBAVLStorage
  • Before: Used Seq concatenation and .map operations on collections
  • After: Uses ArrayBuilder pre-sized with sizeHint and cfor loops for iteration
  • Impact: Reduces allocations by building arrays once with known capacity
  1. Optimized toBytes method in ProverNodeSerializer
  • Before: Used generic Writer with ByteArrayBuilder through inherited method
  • After: Implements specialized toBytes directly using ArrayBuilder with exact size pre-calculation
  • Impact: Eliminates intermediate writer object allocation, uses exact buffer size

Performance Improvements
All optimizations follow the [SigmaState Performance Style Guide]:

  • Replaced Seq with Array where appropriate
  • Used ArrayBuilder with sizeHint for efficient buffer management
  • Used cfor for performance-critical loops (eliminates closure allocation)
  • Avoided array concatenation operations (++)
  • Pre-calculated sizes to avoid buffer resizing

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.

1 participant