Repository navigation
Delete from HashMap by backward shift, free the entry in console.timeEnd and countReset #44280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
73cf394
Add test probes for the console tables and for HashMap lookups
robobun d8689a0
collections: delete from HashMap by backward shift, leave no tombstone
robobun c85065a
console: timeEnd and countReset remove their table entry
robobun bbdbbaa
hash map probe: coerce the arguments, count in u64, skip the churn wi…
robobun c488d7c
Shorten the comments of the map delete and of the test probes
robobun ea0f7db
map tests and probes: pin the insert-only order across two grows, bou…
robobun 199e8a3
Merge main: port the test probes to bindgen schemas, keep retain exact
robobun e90e8e6
bun-types test: list the two new bindgen schemas
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| //! Test-only bridge exposing `bun_collections::HashMap` to | ||
| //! `bun:internal-for-testing` (see `src/js/internal-for-testing.ts`). | ||
| //! | ||
| //! The map has no JS-visible surface of its own, and the cost of a lookup | ||
| //! shows from JS only as time. This bridge counts it instead: its hash context | ||
| //! gives every key the same fingerprint, so the map calls `ctx_eql` once for | ||
| //! each stored entry a lookup walks past, and the context counts those calls. | ||
| //! | ||
| //! Lives in `bun_runtime` (not `bun_collections`) because it needs the JSC | ||
| //! types. | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
|
|
||
| use core::cell::Cell; | ||
|
|
||
| use bun_collections::{HashContext, HashMap}; | ||
| use bun_jsc::{CallFrame, JSGlobalObject, JSValue, JsResult}; | ||
|
|
||
| thread_local! { | ||
| static COMPARISONS: Cell<u32> = const { Cell::new(0) }; | ||
| } | ||
|
|
||
| struct CountingContext; | ||
|
|
||
| impl HashContext<u64> for CountingContext { | ||
| fn ctx_hash(key: &u64) -> u64 { | ||
| // The fingerprint is the top 7 bits of the hash: clear them. | ||
| bun_wyhash::auto_hash(key) >> 7 | ||
| } | ||
| fn ctx_eql(a: &u64, b: &u64) -> bool { | ||
| COMPARISONS.set(COMPARISONS.get() + 1); | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| a == b | ||
| } | ||
| } | ||
|
|
||
| const MISSES: u64 = 1000; | ||
|
|
||
| /// `hashMapChurnProbe(live, cycles)`: inserts the keys `0..live`, then | ||
| /// `cycles` times removes the oldest key and inserts the next integer, so the | ||
| /// map always holds `live` entries. Then looks up 1,000 keys that were never | ||
| /// inserted. Returns `{ capacity, length, maxComparisons }`, where | ||
| /// `maxComparisons` is the largest number of stored entries that one of those | ||
| /// lookups was compared against. | ||
| pub(crate) fn churn_probe(global: &JSGlobalObject, frame: &CallFrame) -> JsResult<JSValue> { | ||
| let live = frame.argument(0).to_int32().clamp(0, 1 << 20) as u64; | ||
| let cycles = frame.argument(1).to_int32().clamp(0, 1 << 24) as u64; | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
|
|
||
| let mut map: HashMap<u64, (), CountingContext> = HashMap::new(); | ||
| for key in 0..live { | ||
| map.insert(key, ()); | ||
| } | ||
| for cycle in 0..cycles { | ||
| map.remove(&cycle); | ||
| map.insert(live + cycle, ()); | ||
|
robobun marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| let mut max_comparisons = 0; | ||
| for miss in 0..MISSES { | ||
| COMPARISONS.set(0); | ||
| let found = map.contains_key(&(u64::MAX - miss)); | ||
| debug_assert!(!found); | ||
| max_comparisons = max_comparisons.max(COMPARISONS.get()); | ||
| } | ||
|
|
||
| let result = JSValue::create_empty_object(global, 3); | ||
| result.put( | ||
| global, | ||
| b"capacity", | ||
| JSValue::js_number(map.capacity() as f64), | ||
| ); | ||
| result.put(global, b"length", JSValue::js_number(map.len() as f64)); | ||
| result.put( | ||
| global, | ||
| b"maxComparisons", | ||
| JSValue::js_number(f64::from(max_comparisons)), | ||
| ); | ||
| Ok(result) | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| /** | ||
| * `bun_collections::HashMap` (src/collections/zig_hash_map.rs) is an internal | ||
| * Rust map with no JS-visible surface of its own. From JS the length of a | ||
| * lookup shows only as time, so these tests count it: `hashMapChurnProbe` | ||
| * (src/runtime/hash_map_testing.rs, exposed via `bun:internal-for-testing`) | ||
| * returns how many stored entries a lookup of an absent key is compared against. | ||
| */ | ||
| import { hashMapChurnProbe } from "bun:internal-for-testing"; | ||
| import { expect, test } from "bun:test"; | ||
|
|
||
| test("a lookup in a map that never removed an entry walks one short run", () => { | ||
| expect(hashMapChurnProbe(256, 0)).toEqual({ capacity: 512, length: 256, maxComparisons: 19 }); | ||
| }); |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.