Skip to content

LSMTreeIndexCursor.fetchNext() allocates an ArrayList, a LinkedHashMap and a HashSet per key group, i.e. per row on a unique-index scan #6944

Closed

Description

@ruispereira

Summary

LSMTreeIndexCursor.fetchNext() allocates three collections per key group. On a unique-index scan a key group is one row, so a full range scan allocates an ArrayList, a LinkedHashMap and a HashSet per row, on the hottest read path in the engine.

Where (HEAD c1785ced6)

engine/src/main/java/com/arcadedb/index/lsm/LSMTreeIndexCursor.java:540-579, per iteration of the key-group loop:

final List<Object> values = new ArrayList<>();          // the group's raw values
final Map<RID, Boolean> ridState = new LinkedHashMap<>(); // rid -> deleted?
...
final Set<RID> mergedRIDs = new HashSet<>();

The number of live RIDs per key is 1 for a unique index and small for most non-unique ones, so all three are sized far above what they hold, and all three die immediately.

Cost

Three allocations plus their internal arrays (ArrayList's Object[], LinkedHashMap's Node[] and one Node per put, HashSet's backing HashMap) per emitted row, i.e. roughly 6-8 objects per row where the useful output is one IndexCursorEntry. On a 1M-row indexed range scan that is on the order of 7M short-lived objects, all in the young generation, against a codebase whose own CLAUDE.md states "always bear in mind PERFORMANCE ... prefer using arrays of primitives to List of Objects".

Suggested fix

Hoist the three containers to fields and clear() them per group rather than reallocating - the cursor is single-threaded and each group's contents are consumed before the next group starts. For the common single-RID case, a fast path that skips the maps entirely (when the group holds one value and it is not a tombstone, emit directly) would remove the allocations altogether for unique-index scans.

I would treat the fields-and-clear version as the safe change and the fast path as the optional follow-up.

Scope / confidence

Medium-high on the cost argument, which is a straight read of the allocation sites; the actual throughput gain depends on how much of the scan cost is page decoding versus allocation, so it is worth a JMH check before and after. Note this is the same method as the correctness issue filed separately about the transaction overlay's REMOVE entries being ignored - if both are addressed, doing the correctness fix first is the right order, since it changes what state the loop has to keep.

Activity

  1. added
    engine
    on Aug 31, 2026
  2. added this to the 26.9.1 milestone on Aug 31, 2026
  3. added
    in progress
    on Aug 31, 2026
  4. self-assigned this
    on Aug 31, 2026
  5. added 2 commits that reference this issue on Aug 31, 2026
  6. lvca commented on Sep 1, 2026

    @lvca
    Member

    Fixed in #6963 (merged).

    Unique-index range scans no longer allocate extra collections per row - the scan reuses the same working buffers instead of creating new ones for every key, reducing garbage-collector pressure on large scans.

    \n---\n❤️ Thanks for helping make ArcadeDB better! ArcadeDB is free and open source, sustained by its community. If it's useful to you or your company, please consider becoming a sponsor to keep development going.

  7. removed
    in progress
    on Sep 1, 2026
  8. ruispereira commented on Sep 1, 2026

    @ruispereira
    ContributorAuthor

    Re-validated against main at 8cfac2923: confirmed fixed, including every part this issue listed. No follow-up needed.

  9. added a commit that references this issue on Sep 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

engineperformance

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions