Skip to content

perf: optimize transaction lookups, cache filtering, and fix resource leaks#332

Open
pannous wants to merge 2 commits into
Qortal:masterfrom
pannous:perf/scalability-optimizations
Open

perf: optimize transaction lookups, cache filtering, and fix resource leaks#332
pannous wants to merge 2 commits into
Qortal:masterfrom
pannous:perf/scalability-optimizations

Conversation

@pannous

@pannous pannous commented Mar 15, 2026

Copy link
Copy Markdown

Summary

Performance and scalability improvements targeting three hot paths identified through code analysis:

  • TransactionImporter: O(1) signature lookupincomingTransactionQueueContains() previously did a full O(n) linear stream scan with Arrays.equals on every call. Added a ByteArray HashSet index (incomingSignatureIndex) for constant-time contains checks. This method is called for every incoming transaction signature from every peer, so the improvement scales with both queue size and peer count.

  • OnlineAccountsManager: eliminate O(n) per-iteration lookups — The minting account loop was calling onlineAccounts.stream().anyMatch(Arrays.equals(...)) per iteration, creating O(nm) complexity. Pre-build a HashSet<ByteArray> of existing public keys before the loop for O(1) lookups. Also optimized the matching count calculation from O(nm) nested streams to O(n+m).

  • HSQLDBCacheUtils: bug fix + optimization + resource cleanup

    • Bug fix: Duplicate keyword filter was applied twice (copy-paste error on lines 227-254), causing incorrect filtering and wasted CPU
    • Optimization: Replaced two-pass metadata/status stripping (creating up to 3 list copies) with a single-pass stream.map() operation
    • Resource cleanup: Added try-with-resources for Statement and ResultSet in fillNamepMap(), getResources(), and getAccountBalances() to prevent JDBC resource leaks under load

Test plan

  • All 40 existing HSQLDBCacheUtilsTests pass (including metadata/status inclusion/exclusion tests)
  • All 10 ByteArrayTests pass
  • Full project compiles cleanly with mvn compile
  • No behavioral changes — all optimizations preserve existing semantics

Addresses #122

- TransactionImporter: add O(1) signature index (ByteArray HashSet) replacing
  O(n) linear stream scan in incomingTransactionQueueContains(), which is called
  on every incoming transaction signature from every peer

- OnlineAccountsManager: pre-build HashSet of existing public keys before
  minting account loop, replacing O(n) stream scan per iteration; also optimize
  matching count calculation from O(n*m) to O(n+m)

- HSQLDBCacheUtils: fix duplicate keyword filter bug (filter was applied twice),
  reduce two-pass metadata/status stripping to single-pass map operation,
  add try-with-resources for Statement/ResultSet in fillNamepMap, getResources,
  and getAccountBalances to prevent JDBC resource leaks
Copilot AI review requested due to automatic review settings March 15, 2026 13:18

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Performance optimizations targeting three hot paths: O(1) transaction signature lookups via ByteArray HashSet index, pre-built lookup sets for online account public keys, and single-pass metadata/status stripping. Also fixes a duplicate keyword filter bug and adds try-with-resources for JDBC resource cleanup.

Changes:

  • Replace O(n) linear scans with O(1) HashSet lookups in TransactionImporter and OnlineAccountsManager using ByteArray wrapper
  • Fix duplicate keyword filter block and simplify metadata/status stripping to single-pass in HSQLDBCacheUtils.filterList()
  • Add try-with-resources for Statement/ResultSet in three database methods to prevent JDBC resource leaks

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
HSQLDBCacheUtils.java Remove duplicate keyword filter, single-pass metadata/status stripping, try-with-resources for JDBC resources
TransactionImporter.java Add incomingSignatureIndex HashSet for O(1) signature contains-check
OnlineAccountsManager.java Pre-build HashSet of public keys to avoid O(n*m) nested stream scans

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

Comment on lines +730 to +792
@@ -841,6 +789,8 @@ private static List<ArbitraryResourceData> getResources( Repository repository)
resources.add( arbitraryResourceData );
} while (resultSet.next());

} // try-with-resources closes Statement and ResultSet
Align the code inside the try-with-resources block in getResources()
with the indentation style used in fillNamepMap() and getAccountBalances().
Also consolidated the two early-return null/empty checks into one and
removed the trailing comment on the closing brace.
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