Skip to content

⚡ Optimize client lookup with secondary index#5

Open
bmendonca3 wants to merge 1 commit intomainfrom
perf-client-lookup-optimization-14337241076733970662
Open

⚡ Optimize client lookup with secondary index#5
bmendonca3 wants to merge 1 commit intomainfrom
perf-client-lookup-optimization-14337241076733970662

Conversation

@bmendonca3
Copy link
Owner

This PR optimizes the client lookup mechanism in the WebSocket server.

Previously, finding a client by clientId required a linear scan of all connected sockets (Array.from(clients.values()).find(...)), which is O(N).

This change introduces a secondary index clientIdMap that maps clientId to a Set of socketIds. Lookups are now O(1).

The optimization preserves the existing behavior where the first connected socket for a given clientId is targeted.

A micro-benchmark confirmed significant performance gains (from ~2.4ms to ~0.0015ms for 50k clients). Existing tests pass.


PR created automatically by Jules for task 14337241076733970662 started by @bmendonca3

- Introduce `clientIdMap` (Map<string, Set<string>>) in `server/index.ts` to index socket IDs by client ID.
- Maintain the index on `connection` and `disconnect` events.
- Replace O(N) linear scan in `message:ack` handler with O(1) index lookup.
- Preserve existing "first one wins" behavior for handling multiple connections with the same client ID.
- Fix minor unused variable warning in server shutdown loop.

Measured improvement: ~1650x speedup for 50k connected clients in micro-benchmark.

Co-authored-by: bmendonca3 <208517100+bmendonca3@users.noreply.github.com>
@google-labs-jules
Copy link

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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