diff options
author | Maxime Chevalier-Boisvert <[email protected]> | 2024-07-11 15:01:05 -0400 |
---|---|---|
committer | GitHub <[email protected]> | 2024-07-11 19:01:05 +0000 |
commit | 3fbf9df39ad9c9338a2f9b94b4dd87c1c446ec95 (patch) | |
tree | f9e36af88c70599e10ce30a245abcca053d3b24e /yjit/src | |
parent | 1f6aeadc824ad597aa7ad8ab2ddb554f448147ac (diff) |
YJIT: increase context cache size to 1024 redux (#11140)
* YJIT: increase context cache size to 1024 redux
* Move context hashing code outside of unsafe block
* Avoid allocating large table on the stack, which would cause a stack overflow
Co-authored by Alan Wu @XrXr
Diffstat (limited to 'yjit/src')
-rw-r--r-- | yjit/src/core.rs | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/yjit/src/core.rs b/yjit/src/core.rs index 7ddb65fd90..699fcae840 100644 --- a/yjit/src/core.rs +++ b/yjit/src/core.rs @@ -842,7 +842,7 @@ enum CtxOp { } // Number of entries in the context cache -const CTX_CACHE_SIZE: usize = 512; +const CTX_CACHE_SIZE: usize = 1024; // Cache of the last contexts encoded // Empirically this saves a few percent of memory @@ -906,18 +906,20 @@ impl Context { // Store an entry in a cache of recently encoded/decoded contexts fn cache_set(ctx: &Context, idx: u32) { + // Compute the hash for this context + let mut hasher = DefaultHasher::new(); + ctx.hash(&mut hasher); + let ctx_hash = hasher.finish() as usize; + unsafe { // Lazily initialize the context cache if CTX_CACHE == None { - let empty_tbl = [(Context::default(), 0); CTX_CACHE_SIZE]; - CTX_CACHE = Some(Box::new(empty_tbl)); + // Here we use the vec syntax to avoid allocating the large table on the stack, + // as this can cause a stack overflow + let tbl = vec![(Context::default(), 0); CTX_CACHE_SIZE].into_boxed_slice().try_into().unwrap(); + CTX_CACHE = Some(tbl); } - // Compute the hash for this context - let mut hasher = DefaultHasher::new(); - ctx.hash(&mut hasher); - let ctx_hash = hasher.finish() as usize; - // Write a cache entry for this context let cache = CTX_CACHE.as_mut().unwrap(); cache[ctx_hash % CTX_CACHE_SIZE] = (*ctx, idx); @@ -927,6 +929,11 @@ impl Context { // Lookup the context in a cache of recently encoded/decoded contexts fn cache_get(ctx: &Context) -> Option<u32> { + // Compute the hash for this context + let mut hasher = DefaultHasher::new(); + ctx.hash(&mut hasher); + let ctx_hash = hasher.finish() as usize; + unsafe { if CTX_CACHE == None { return None; @@ -934,11 +941,6 @@ impl Context { let cache = CTX_CACHE.as_mut().unwrap(); - // Compute the hash for this context - let mut hasher = DefaultHasher::new(); - ctx.hash(&mut hasher); - let ctx_hash = hasher.finish() as usize; - // Check that the context for this cache entry mmatches let cache_entry = &cache[ctx_hash % CTX_CACHE_SIZE]; if cache_entry.0 == *ctx { |