Skip to content

Conversation

@qiweiii
Copy link
Contributor

@qiweiii qiweiii commented Oct 31, 2025

some trace have lots of state items

improve in-mem backend a bit, state trie is more difficult to improve

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

The change from SortedArray to Dictionary improves performance for direct key lookups and updates, but introduces a significant performance regression for prefix/range queries in readAll and createIterator. These functions now iterate and sort the entire dataset, which will be slow for large state sets.

Comment on lines 30 to 41
// Filter and sort entries
let filtered = store
.filter { $0.key.starts(with: prefix) }
.filter { $0.key.lexicographicallyPrecedes(startKey) == false }
.sorted { $0.key.lexicographicallyPrecedes($1.key) }

// Apply limit if specified
if let limit {
return Array(filtered.prefix(Int(limit)))
}
return resp

return filtered

Choose a reason for hiding this comment

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

The implementation of readAll has a performance regression. It filters and sorts the entire store dictionary, which is an O(N) operation to filter plus a sort on the filtered results. The previous SortedArray-based implementation was more efficient for this, as it could find the starting key in O(log N) and iterate from there. For a large store, this change will be very slow and may negate the performance gains in other functions. A similar issue exists for createIterator on lines 90-96.

If this trade-off is acceptable, the two separate .filter calls on lines 32 and 33 should be combined for slightly better performance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

should be fine, readAll is not used anywhere, read is frequently used, and single read and write achieve O(1) is very meaningful for now

@codecov
Copy link

codecov bot commented Oct 31, 2025

Codecov Report

❌ Patch coverage is 11.53846% with 23 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.66%. Comparing base (2595ce8) to head (11d8edc).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
...ain/Sources/Blockchain/State/InMemoryBackend.swift 11.53% 23 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #372      +/-   ##
==========================================
- Coverage   81.68%   81.66%   -0.03%     
==========================================
  Files         379      379              
  Lines       25919    25903      -16     
==========================================
- Hits        21172    21153      -19     
- Misses       4747     4750       +3     

☔ 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.

@qiweiii qiweiii requested a review from xlc October 31, 2025 07:18
@qiweiii qiweiii merged commit 411dfd4 into master Oct 31, 2025
7 of 10 checks passed
@qiweiii qiweiii deleted the improve-perf-many-state-items branch October 31, 2025 09:52
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