Skip to content

Optimize handles clearing on session / token closing#851

Open
bukka wants to merge 1 commit intosofthsm:mainfrom
bukka:handle-clearing-optimization
Open

Optimize handles clearing on session / token closing#851
bukka wants to merge 1 commit intosofthsm:mainfrom
bukka:handle-clearing-optimization

Conversation

@bukka
Copy link
Member

@bukka bukka commented Mar 21, 2026

Currently the full map scan is done on each clearing. This can result in significant CPU usage when large number of objects are used. The main problem is that full scan is done when any session is closed so if application has large number in one session, they get scanned even though there is no need for that.

This PR introduces additional maps that track object per session / slot as well as slot session count.

Summary by CodeRabbit

  • Refactor
    • Improved performance of handle manager operations by optimizing internal data structures to reduce lookup times during session and slot cleanup operations.

@bukka bukka requested a review from a team as a code owner March 21, 2026 14:01
@coderabbitai
Copy link

coderabbitai bot commented Mar 21, 2026

📝 Walkthrough

Walkthrough

The handle manager introduces three secondary indexes—sessionObjectHandles, slotHandles, and slotSessionCount—to optimize cleanup and removal operations. These indexes track handles by session and slot, eliminating full map scans during handle destruction, session closure, and slot cleanup.

Changes

Cohort / File(s) Summary
Handle Manager Indexing
src/lib/handle_mgr/HandleManager.h
Added three private member variables: sessionObjectHandles (maps sessions to object handles), slotHandles (maps slots to all handles), and slotSessionCount (tracks open sessions per slot) to enable efficient targeted cleanup.
Handle Manager Implementation
src/lib/handle_mgr/HandleManager.cpp
Updated handle creation, destruction, session closure, slot cleanup, and token logout logic to populate and utilize secondary indexes, replacing full map iterations with targeted lookups and removals.

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~50 minutes

Poem

🐰 Our warren grows swift with clever new maps,
No more digging through tunnels to close all the gaps!
Sessions and slots now dance in their place,
Finding their handles at optimal pace. ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and accurately describes the main optimization focus: avoiding full handle map scans during session and token closing by implementing per-session and per-slot indexing.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

CodeRabbit can generate a title for your PR based on the changes with custom instructions.

Set the reviews.auto_title_instructions setting to generate a title for your PR based on the changes in the PR with custom instructions.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/lib/handle_mgr/HandleManager.cpp (1)

161-165: Avoid operator[] in cleanup-only accesses to the secondary indexes.

These sites silently create empty buckets when the index is already out of sync. The risky case is Line 202: a missing slotSessionCount entry becomes 0, and the zero-path below clears the whole slot. Prefer find()/erase() here so an invariant miss stays visible instead of mutating state during cleanup.

Also applies to: 182-182, 194-194, 202-204, 257-257

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/lib/handle_mgr/HandleManager.cpp` around lines 161 - 165, The cleanup
code uses operator[] on secondary-index maps (e.g., sessionObjectHandles,
slotHandles, slotSessionCount) which can silently create empty buckets; change
these accesses to use find() and conditional erase only when the key exists (use
iterator from find(), then call erase(it) or
container.erase(iterator->second.find(hObject)) as appropriate) so missing
entries do not get created during cleanup—update all occurrences including the
removal from sessionObjectHandles/slotHandles and the slotSessionCount
decrement/clear paths to use find() checks before modifying or erasing.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/lib/handle_mgr/HandleManager.cpp`:
- Around line 161-165: The cleanup code uses operator[] on secondary-index maps
(e.g., sessionObjectHandles, slotHandles, slotSessionCount) which can silently
create empty buckets; change these accesses to use find() and conditional erase
only when the key exists (use iterator from find(), then call erase(it) or
container.erase(iterator->second.find(hObject)) as appropriate) so missing
entries do not get created during cleanup—update all occurrences including the
removal from sessionObjectHandles/slotHandles and the slotSessionCount
decrement/clear paths to use find() checks before modifying or erasing.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 9b1d9522-e39c-4c70-ba1f-5ed1e9178c3b

📥 Commits

Reviewing files that changed from the base of the PR and between 62c20ed and 5552e94.

📒 Files selected for processing (2)
  • src/lib/handle_mgr/HandleManager.cpp
  • src/lib/handle_mgr/HandleManager.h

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