Skip to content

[Perf][grounded-shop-assistant]: session eviction sorts all 5000 sessions on every new session once full #494

Open
smaramwbc/statewave-grounded-shop-assistant
#7

Description

@jy7lsna

Affected Component

statewave-grounded-shop-assistant — server/src/sessionStore.ts

Description

Once the store is full, every new session copies and sorts the entire map just to find one entry to evict:

// server/src/sessionStore.ts:32-35
function evictOldestIfFull(): void {
  if (sessions.size < MAX_SESSIONS) return;
  const oldest = [...sessions.values()].sort((a, b) => a.lastSeen - b.lastSeen)[0];
  if (oldest) sessions.delete(oldest.id);
}

At MAX_SESSIONS = 5000, that's an array allocation plus an O(n log n) sort on the request path of every first message, which is exactly when the server is busiest (the map is full).

A JS Map iterates in insertion order. If a session is moved to the end whenever it's touched (delete then set), the first key is always the least-recently-used one, and eviction becomes O(1):

// on get(existing) and save():
sessions.delete(session.id);
sessions.set(session.id, session);

// evict:
const oldestId = sessions.keys().next().value;
if (oldestId !== undefined) sessions.delete(oldestId);

That also makes lastSeen only needed for the TTL sweep.

Expected Behavior

Evicting a session is O(1).

Suggested fix

As above. If the session-store tests from #493 land first, they'll confirm eviction order doesn't change.

Environment

main @ d4f0ac0.

Severity

Low — performance cleanup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions