Skip to content

perf: track open cores in a flat table keyed by discovery key bytes - #189

Draft
danielAsaboro wants to merge 2 commits into
holepunchto:mainfrom
danielAsaboro:perf/core-table
Draft

danielAsaboro wants to merge 2 commits into
holepunchto:mainfrom
danielAsaboro:perf/core-table

Conversation

@danielAsaboro

Copy link
Copy Markdown

CoreTracker keeps open cores in a Map keyed by the hex discovery key, so every lookup (get, resume, opened, and each ondiscoverykey from a peer) first turns the 32-byte key into a 64-character string. This replaces the map with a flat table keyed by the key bytes:

  • cores: a dense array of the open cores. Each core stores its own index (core.trackerIndex), so removal is a swap with the last entry.
  • slots: a Uint32Array with open addressing and linear probing. Each slot holds an 8-bit tag from the hash in the upper bits and index + 1 in the lower 24 bits, so most mismatches are rejected without loading the core. Deletion is a backward shift, so there are no tombstones.
  • The hash mixes the first 8 key bytes with a per-process random seed, so a peer can't pick keys that all land in one probe chain.

The public surface is the same: size, iteration, get (now a hex string or a buffer), resume/opened (the internal callers pass the buffer) and watch. cores.map is gone. Its only reader was an assertion in test/groups.js that could never fail, because it looked up a key in a map keyed by discovery key. That assertion now checks cores.get(a.discoveryKey) === null.

Capacity is 2^24 open cores.

validation

test/cores.js runs 20k random set, replace and gc operations against a reference Map, using ids that share their first 8 bytes so the probe chains are long. It fails if deletion, the swap-remove order or the replace path is broken. Node and Bare 55/55, npm run lint clean.

numbers (main → this branch, 1000 open cores)

benchmark main branch ratio wins
cores.get(dk) 615 ns 83 ns 0.14 11/11
iterate all cores 46.7 µs 22.1 µs 0.47 11/11
get({ key }) on an open core 1,986 ns 1,409 ns 0.71 10/11
session get + ready + close 2,685 ns 2,260 ns 0.84 9/11

Method: both checkouts are loaded into one process with 1000 open named cores and 64 replication streams each, and 11 timed runs alternate between them. "wins" counts the paired runs where this branch was faster. Separate-process runs were too noisy on this machine to use; unchanged paths show ±15–25% in this harness, so only the first two rows are clear wins.

This branch has not been deployed

No deployments
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