Repository navigation
JsonValue: an object of 16 members or more finds a member through a table - #97
Merged
Merged
Conversation
…able Every get, has and set looked through all the names, so reading an object of n members cost n * n: JSON.parse of one object of 32,000 keys took 1072 ms. It now takes 17 ms (8,000 keys: 62 ms to 5 ms). The table is an open-addressed int[] of name positions inside JsonValue, hashed with HashHelpers.hashCodeString. It is made when an object reaches JSON_INDEX_FROM (16) members and remade, twice the size, when half full, in set and in clone, so a lookup only reads it. Smaller objects are looked through as before. Not a Map: one built into the library would be built again into a program that uses the same Map. lib.d.ts declares the new private field, as it does the others. Cost: every JsonValue carries one more field, and parsing many small objects is about 10% slower (80,000 four-key objects: 184 ms to 202 ms). Test: json_many_keys.ts. Tested in release on Windows x64 for gc, rc, none and own, compiled and under the JIT; debug, Linux, x86 and wasm were not built locally. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #96, which left this one out.
What
Every
get,hasandseton aJsonValueobject looked through all its names, so reading an object of n members cost n * n. An object of 16 members or more now finds a member through a hash table.JSON.parseJSON.parseJSON.parsegetcallsMeasured under the release JIT (gc, Windows x64).
How
JsonValuegetsprivate slots: int[]: an open-addressed table of name positions, hashed withHashHelpers.hashCodeString.JSON_INDEX_FROM(16) members and remade, twice the size, when half full. That happens insetand inclone, so a lookup only reads it.Map: one built into the library would be built again into a program that uses the sameMaptype.lib.d.tsdeclares the new private field, since a program reads the class layout from there.For the reviewer
JsonValuecarries one more field, and parsing many small objects is about 10% slower (80,000 four-key objects: 184 ms before, 202 ms after). Objects of 24 keys came out about level.JsonValuegrows by one field, so the library andlib.d.tsmust be updated together.Testing
tests/json_many_keys.ts: sizes around the 16-member threshold and up to 5,000 keys, duplicate keys,seton existing and new keys, key order,clone, and a stringify round trip.scripts/check_declarations.ps1passes.🤖 Generated with Claude Code