mini_racer_rs 1.5.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bf41190f924dfa0445ada46e1b47ed422f25b4d88de74d4c4f0397f4190282c9
4
- data.tar.gz: 330258ac6f3a92e31e23e8123dedeae52befe9eecb0a2409a10da574a9cf7893
3
+ metadata.gz: ac6808f9735e55f734d5279ac4a61df43c360de9fd62475417fb3a86eed95419
4
+ data.tar.gz: 775a078c2e03d715bce13eca7deac20a9df74be55adec4d1942717042ffdf29d
5
5
  SHA512:
6
- metadata.gz: 2fc22cbc008d1db4a8b8bf22e8d79177e20cd8ae97da99ab5f1962f601097dc8f37580ed4882ed5178c35c8be47bfb0d9a6b533fcf5b0ca281f928dfc2428c4b
7
- data.tar.gz: 288df9055b1beb4231c3701e523a2176ac2907ae7762c8282c6534ca6119197c7f1c46a3ff1f534364c30c29825877637433a6144636e52ffd0d0d22da10e3e2
6
+ metadata.gz: 9b4a2ee6af6bf08c29484eaab6228ae78b05c17e9c57457b12c339d013047855371b600b1e1f870e62a02d2f04bf75f69e779aab07f094c73179fea3f132634d
7
+ data.tar.gz: 2662edd8f467161d2f4d7c93a29e3033feddac65411ce5e5118590aa8c216c09b36f52b259d0aa88285b6876839a13df59bc4d80b48df9133a4203beb337c0d5
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --markup markdown
2
+ --exclude lib/mini_racer/truffleruby.rb
3
+ --exclude lib/mini_racer/shared.rb
4
+ lib/**/*.rb
5
+ -
6
+ COMPATIBILITY.md
7
+ CHANGELOG.md
data/CHANGELOG.md CHANGED
@@ -1,5 +1,27 @@
1
1
  # Changelog
2
2
 
3
+ ## [1.5.2](https://github.com/andymai/mini_racer_rs/compare/v1.5.1...v1.5.2) (2026-10-24)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **ext:** keep set_flags! values rooted and stop dropping interrupts in waits ([#152](https://github.com/andymai/mini_racer_rs/issues/152)) ([b32e5cd](https://github.com/andymai/mini_racer_rs/commit/b32e5cd22a1f0f36cc4fa1e7b516c8867829b70f))
9
+ * **pool:** build replacements outside the timeout scope and root shutdown's queues ([#151](https://github.com/andymai/mini_racer_rs/issues/151)) ([60b292d](https://github.com/andymai/mini_racer_rs/commit/60b292df11e7802b9b8429848788176698b463e9))
10
+
11
+
12
+ ### Performance
13
+
14
+ * **engine:** decode a shared UTF-8 source in place ([#155](https://github.com/andymai/mini_racer_rs/issues/155)) ([f37538f](https://github.com/andymai/mini_racer_rs/commit/f37538fa2931438c4cd5a40d7647d2bbfc92d356))
15
+ * **engine:** remember 4,096 sightings of large scripts ([#156](https://github.com/andymai/mini_racer_rs/issues/156)) ([656b60f](https://github.com/andymai/mini_racer_rs/commit/656b60fe62c976ae06786460bb24004c127dccd8))
16
+
17
+ ## [1.5.1](https://github.com/andymai/mini_racer_rs/compare/v1.5.0...v1.5.1) (2026-10-23)
18
+
19
+
20
+ ### Bug Fixes
21
+
22
+ * free disposed context memory before timeout is due ([#141](https://github.com/andymai/mini_racer_rs/issues/141)) ([1b8fd96](https://github.com/andymai/mini_racer_rs/commit/1b8fd96057d09ebb4a7e4b30eaa71c0d436c5694))
23
+ * free snapshot blob bytes after contexts are gone ([#142](https://github.com/andymai/mini_racer_rs/issues/142)) ([72f4a2d](https://github.com/andymai/mini_racer_rs/commit/72f4a2d5aa287e065ffa9a1402a5255a5ea7db99))
24
+
3
25
  ## [1.5.0](https://github.com/andymai/mini_racer_rs/compare/v1.4.0...v1.5.0) (2026-10-23)
4
26
 
5
27
 
data/Cargo.lock CHANGED
@@ -555,9 +555,9 @@ dependencies = [
555
555
 
556
556
  [[package]]
557
557
  name = "magnus"
558
- version = "0.9.2"
558
+ version = "0.9.3"
559
559
  source = "registry+https://github.com/rust-lang/crates.io-index"
560
- checksum = "b9068710e00d21be762edca59b9c4c8946dbdb29820d4f76ad559afea07982c9"
560
+ checksum = "40ef424404d166cbb71c8b8d19c9c589c748c4547c1afa1928c5504299f6c713"
561
561
  dependencies = [
562
562
  "magnus-macros",
563
563
  "rb-sys",
@@ -593,7 +593,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
593
593
 
594
594
  [[package]]
595
595
  name = "mini_racer_engine"
596
- version = "1.5.0"
596
+ version = "1.5.2"
597
597
  dependencies = [
598
598
  "deno_core_icudata",
599
599
  "libc",
@@ -606,7 +606,7 @@ dependencies = [
606
606
 
607
607
  [[package]]
608
608
  name = "mini_racer_rs"
609
- version = "1.5.0"
609
+ version = "1.5.2"
610
610
  dependencies = [
611
611
  "magnus",
612
612
  "mini_racer_engine",
@@ -617,7 +617,7 @@ dependencies = [
617
617
 
618
618
  [[package]]
619
619
  name = "mini_racer_sourcemap"
620
- version = "1.5.0"
620
+ version = "1.5.2"
621
621
  dependencies = [
622
622
  "memchr",
623
623
  "serde",
@@ -627,7 +627,7 @@ dependencies = [
627
627
 
628
628
  [[package]]
629
629
  name = "mini_racer_sync"
630
- version = "1.5.0"
630
+ version = "1.5.2"
631
631
  dependencies = [
632
632
  "loom",
633
633
  "thiserror",
@@ -635,7 +635,7 @@ dependencies = [
635
635
 
636
636
  [[package]]
637
637
  name = "mini_racer_wire"
638
- version = "1.5.0"
638
+ version = "1.5.2"
639
639
  dependencies = [
640
640
  "criterion",
641
641
  "proptest",
data/Cargo.toml CHANGED
@@ -15,14 +15,14 @@ license = "MIT"
15
15
  publish = false
16
16
  repository = "https://github.com/andymai/mini_racer_rs"
17
17
  rust-version = "1.91"
18
- version = "1.5.0"
18
+ version = "1.5.2"
19
19
 
20
20
  [workspace.dependencies]
21
21
  criterion = {version = "0.8", default-features = false, features = ["cargo_bench_support"]}
22
22
  deno_core_icudata = "=0.78.0"
23
23
  libc = "0.2"
24
24
  loom = "0.7.2"
25
- magnus = {version = "=0.9.2", features = ["rb-sys"]}
25
+ magnus = {version = "=0.9.3", features = ["rb-sys"]}
26
26
  memchr = "2"
27
27
  mini_racer_engine = {path = "crates/mini_racer_engine"}
28
28
  mini_racer_sourcemap = {path = "crates/mini_racer_sourcemap"}
data/README.md CHANGED
@@ -21,9 +21,9 @@ mini_racer_rs implements the `MiniRacer` API from mini_racer 0.22.2. It defines
21
21
 
22
22
  ## Highlights
23
23
 
24
- - **Round trips:** 5 to 9 times faster (0.75 µs vs 6.45 µs per call).
25
- - **Babel through ExecJS:** 1.97x faster (5.11 ms vs 10.06 ms).
26
- - **Bundle load:** 4.6x faster for a 44 MB production server rendering bundle (215 ms vs 983 ms).
24
+ - **Round trips:** 5 to 9 times faster (0.75 µs vs 6.50 µs per call).
25
+ - **Babel through ExecJS:** 1.93x faster (5.09 ms vs 9.85 ms).
26
+ - **Bundle load:** 9.2x faster for a 44 MB production server rendering bundle (105 ms vs 965 ms).
27
27
  - **Memory:** A context holding the MUI bundle uses 43 MB vs 110 MB.
28
28
  - **Forks:** After loading and rendering React: 20 of 20 clean mini_racer_rs exits vs 20 of 20 mini_racer aborts.
29
29
  - **V8:** V8 15.2.124.1 vs V8 13.6.233.17 in mini_racer 0.22.2.
@@ -33,7 +33,7 @@ mini_racer_rs implements the `MiniRacer` API from mini_racer 0.22.2. It defines
33
33
  - **Callback jumps:** A `throw` out of an attached callback ends the script with `Error: terminated` and continues after `eval`; mini_racer 0.22.2 crashes with a segmentation fault.
34
34
  - **Checks:** Script-enforced crate boundaries, four fenced `unsafe` modules, loom, wire-codec fuzzing, nightly sanitizers and mini_racer differential tests.
35
35
 
36
- These numbers compare with mini_racer 0.22.1. See [Benchmarks](#benchmarks) for the method.
36
+ The round trip, Babel, memory, fiber scheduler, and bundle load numbers compare with mini_racer 0.22.2, while the fork numbers compare with mini_racer 0.22.1. See [Benchmarks](#benchmarks) for the method.
37
37
 
38
38
  ## Install
39
39
 
@@ -212,39 +212,39 @@ Before forking, evaluate each bundle of 1 MiB or more twice in the master, then
212
212
 
213
213
  For Ruby's `fork`, `IO.popen("-")`, or `Process.daemon`, mini_racer_rs waits for context runners, pauses its worker pool and waits for running tasks. An async `WebAssembly.compile` in a new default-mode child completes on the child's own pool. On Ubuntu 24.04 with glibc 2.39, 20 of 20 mini_racer 0.22.1 children aborted with signal 6 after React rendering; all mini_racer_rs children exited cleanly. mini_racer 0.22.2 has the same fork handling. On Fedora, `test/stress/fork_busy.rb` aborted with mini_racer 0.22.2 in 3 of 3 runs, after 5, 14 and 19 forks, when an evaluating parent thread raised `TypeError`. mini_racer_rs completed 300 forks per run on Fedora and Ubuntu.
214
214
 
215
- With `mini_racer` 0.22.1, each worker compiles the bundle from source on its first load because it has no code cache.
215
+ With `mini_racer` 0.22.2, each worker compiles the bundle from source on its first load because it has no code cache.
216
216
 
217
217
  ## Benchmarks
218
218
 
219
- | Workload | mini_racer 0.22.1 | mini_racer_rs | Ratio |
219
+ | Workload | mini_racer 0.22.2 | mini_racer_rs | Ratio |
220
220
  |---|---:|---:|---:|
221
- | Babel transform of a 3 KB JSX component, through ExecJS | 10.1 ms | 5.1 ms | 2.0x |
222
- | Terser minify of a 78 KB library, through ExecJS | 51.4 ms | 50.9 ms | 1.0x |
223
- | React server render of 1,000 items (17 KB of HTML), through ExecJS | 192 µs | 182 µs | 1.1x |
221
+ | Babel transform of a 3 KB JSX component, through ExecJS | 9.8 ms | 5.1 ms | 1.9x |
222
+ | Terser minify of a 78 KB library, through ExecJS | 51.3 ms | 49.9 ms | 1.0x |
223
+ | React server render of 1,000 items (17 KB of HTML), through ExecJS | 192 µs | 175 µs | 1.1x |
224
224
  | React in a new context: load the bundle and render 1,000 items, through ExecJS | 11.8 ms | 11.8 ms | 1.0x |
225
- | Handlebars render of 50 rows, through `Context#call` | 49.0 µs | 34.7 µs | 1.4x |
226
- | Autoprefixer on Bootstrap's 280 KB stylesheet, through ExecJS | 26.2 ms | 23.8 ms | 1.1x |
227
- | MUI dashboard server render of 200 rows and 299 icons (469 KB of HTML), through ExecJS | 30.3 ms | 29.2 ms | 1.0x |
228
- | MUI in a new context: load the 8.2 MB bundle and render once, through ExecJS | 166 ms | 104 ms | 1.6x |
229
- | Load a 44 MB production server rendering bundle into a new context, through ExecJS | 983 ms | 215 ms | 4.6x |
225
+ | Handlebars render of 50 rows, through `Context#call` | 50.0 µs | 35.0 µs | 1.4x |
226
+ | Autoprefixer on Bootstrap's 280 KB stylesheet, through ExecJS | 26.0 ms | 24.0 ms | 1.1x |
227
+ | MUI dashboard server render of 200 rows and 299 icons (469 KB of HTML), through ExecJS | 31.4 ms | 29.2 ms | 1.1x |
228
+ | MUI in a new context: load the 8.2 MB bundle and render once, through ExecJS | 164 ms | 104 ms | 1.6x |
229
+ | Load a 44 MB production server rendering bundle into a new context, through ExecJS | 965 ms | 105 ms | 9.2x |
230
230
 
231
- Results are medians of 5 runs. Every workload produced identical output in both gems. The eight library workloads use one run of the Ruby 3.4 extension from the published mini_racer_rs 1.4.0 `x86_64-linux-gnu` gem. The 44 MB production bundle load uses commit `2513a48`. Benchmarks ran on Ruby 3.4.2 and Linux with an AMD Ryzen 9 7950X3D. The governor was `performance`. Both gems were pinned to CPUs 0 through 7.
231
+ Results are medians of 5 runs. Every workload produced identical output in both gems. The eight library workloads use one run of the Ruby 3.4 extension from the published mini_racer_rs 1.5.1 `x86_64-linux-gnu` gem against mini_racer 0.22.2. The 44 MB production bundle load uses a second run of the same gem against mini_racer 0.22.2. Benchmarks ran on Ruby 3.4.2 and Linux with an AMD Ryzen 9 7950X3D. The governor was `performance`. Both gems were pinned to CPUs 0 through 7.
232
232
 
233
- The per-call and memory figures below come from the same run of that extension on the same machine. One watchdog thread serves the whole process, making a call with `timeout:` fast. Scripts of 1 MiB or more use a process-wide V8 code cache. Contexts loading the same large script share one source copy.
233
+ The per-call and memory figures below come from the first run of that extension on the same machine, except the production bundle row, which comes from the second run. One watchdog thread serves the whole process, making a call with `timeout:` fast. Scripts of 1 MiB or more use a process-wide V8 code cache. Contexts loading the same large script share one source copy.
234
234
 
235
- | Measure | mini_racer 0.22.1 | mini_racer_rs |
235
+ | Measure | mini_racer 0.22.2 | mini_racer_rs |
236
236
  |---|---:|---:|
237
- | eval round trip | 6.76 µs | 1.14 µs |
238
- | call round trip | 6.45 µs | 0.75 µs |
239
- | callback round trip | 6.06 µs | 1.11 µs |
240
- | call on a context with `timeout:` | 20.50 µs | 0.78 µs |
241
- | call inside `MiniRacerRs.timeout` (mini_racer: on a context with `timeout:`) | 20.61 µs | 0.85 µs |
237
+ | eval round trip | 6.58 µs | 1.13 µs |
238
+ | call round trip | 6.50 µs | 0.75 µs |
239
+ | callback round trip | 6.13 µs | 1.11 µs |
240
+ | call on a context with `timeout:` | 20.42 µs | 0.78 µs |
241
+ | call inside `MiniRacerRs.timeout` (mini_racer: on a context with `timeout:`) | 20.22 µs | 0.87 µs |
242
242
  | Babel inside V8 | 10.3 ms | 5.3 ms |
243
- | startup plus Babel | 44.3 ms | 13.2 ms |
243
+ | startup plus Babel | 44.8 ms | 13.3 ms |
244
244
  | context holding Babel | 28.5 MB | 15.0 MB |
245
245
  | context holding the 8.2 MB MUI bundle | 110 MB | 43 MB |
246
- | context holding the 44 MB production bundle, at commit `2513a48` before sources were shared | 301 MB | 136 MB |
247
- | peak resident memory with 20,000 contexts left to Ruby's garbage collector | 2,910 MB | 106 MB |
246
+ | context holding the 44 MB production bundle | 301 MB | 44 MB |
247
+ | peak resident memory with 20,000 contexts left to Ruby's garbage collector | 2,918 MB | 108 MB |
248
248
 
249
249
  ```sh
250
250
  bundle exec ruby bench/run.rb --cpus 0-7
@@ -869,3 +869,57 @@ fn channel(error: ChannelError) -> EngineError {
869
869
  error => protocol(error),
870
870
  }
871
871
  }
872
+
873
+ #[cfg(test)]
874
+ mod tests {
875
+ use std::sync::Arc;
876
+ use std::time::{Duration, Instant};
877
+
878
+ use mini_racer_sync::Phase;
879
+
880
+ use super::{ContextHandle, Held, Host, WaitHandle, WaitOutcome};
881
+ use crate::control::ContextConfig;
882
+
883
+ struct Parking;
884
+
885
+ impl Host for Parking {
886
+ fn wait(&mut self, wait: &WaitHandle<'_>) -> WaitOutcome {
887
+ self.wait_blocking(wait);
888
+ WaitOutcome::Ready
889
+ }
890
+
891
+ fn wait_blocking(&mut self, wait: &WaitHandle<'_>) {
892
+ while !wait.ready() {
893
+ wait.park();
894
+ }
895
+ }
896
+ }
897
+
898
+ // A weak reference keeps the block's allocation, which a thread that made
899
+ // the context and exited leaves in a malloc arena other threads take over.
900
+ #[test]
901
+ fn the_watchdog_lets_go_of_a_disposed_context_before_its_timeout() {
902
+ let context = ContextHandle::spawn(
903
+ ContextConfig {
904
+ timeout: Some(Duration::from_secs(600)),
905
+ ..ContextConfig::default()
906
+ },
907
+ &mut Parking,
908
+ )
909
+ .expect("spawn");
910
+ let Held::Block(block) = context.held() else {
911
+ panic!("a new context holds its block");
912
+ };
913
+ let block = Arc::clone(block);
914
+ let untimed = Arc::weak_count(&block);
915
+ context.eval("<eval>", "1", &mut Parking).expect("eval");
916
+ context.begin_dispose();
917
+ context.dispose(&mut Parking).expect("dispose");
918
+ let deadline = Instant::now() + Duration::from_secs(30);
919
+ while block.phase.get() != Phase::Exited {
920
+ assert!(Instant::now() < deadline, "the runner did not exit");
921
+ std::thread::sleep(Duration::from_millis(1));
922
+ }
923
+ assert_eq!(Arc::weak_count(&block), untimed);
924
+ }
925
+ }
@@ -9,7 +9,11 @@ use crate::control::JsText;
9
9
 
10
10
  const MAX_ENTRIES: usize = 8;
11
11
  const MAX_BYTES: usize = 256 << 20;
12
- const MAX_SIGHTINGS: usize = 64;
12
+ // 32 KiB of digests, so a bundle loaded again after thousands of large scripts
13
+ // evaluated once is still seen a second time: a list of 64 forgot one after 70
14
+ // such scripts, so only its fourth load read a cache. Each large eval scans
15
+ // it, in about 2 microseconds when full.
16
+ const MAX_SIGHTINGS: usize = 4096;
13
17
  const MAX_REFUSED: usize = 8;
14
18
  // Units read from each end of a source, and a smaller window from each of a
15
19
  // few points between, to tell one sighting from another without reading it all.
@@ -292,7 +296,7 @@ pub(crate) fn enabled() -> bool {
292
296
  #[cfg(test)]
293
297
  #[allow(clippy::panic, reason = "a test fails by panicking")]
294
298
  mod tests {
295
- use super::{CodeCache, Plan};
299
+ use super::{CodeCache, MAX_SIGHTINGS, Plan};
296
300
  use crate::control::JsText;
297
301
 
298
302
  fn cache() -> &'static CodeCache {
@@ -397,6 +401,29 @@ mod tests {
397
401
  ));
398
402
  }
399
403
 
404
+ // The list holds as many sightings as it keeps and no more: a script is
405
+ // seen again after one fewer others, and forgotten after that many more.
406
+ #[test]
407
+ fn a_sighting_is_forgotten_once_the_list_has_had_its_fill_of_others() {
408
+ let cache = cache();
409
+ let script = |seed: usize| JsText::Latin1(seed.to_le_bytes().to_vec());
410
+ for seed in 0..MAX_SIGHTINGS {
411
+ let _ = cache.plan(&name(), &script(seed), None, false);
412
+ }
413
+ assert!(matches!(
414
+ cache.plan(&name(), &script(0), None, false),
415
+ Plan::Make(..)
416
+ ));
417
+ for seed in MAX_SIGHTINGS..2 * MAX_SIGHTINGS {
418
+ let _ = cache.plan(&name(), &script(seed), None, false);
419
+ }
420
+ assert_eq!(mini_racer_sync::lock(&cache.sightings).len(), MAX_SIGHTINGS);
421
+ assert!(matches!(
422
+ cache.plan(&name(), &script(0), None, false),
423
+ Plan::Compile(None)
424
+ ));
425
+ }
426
+
400
427
  #[test]
401
428
  fn a_script_being_built_is_built_once() {
402
429
  let cache = cache();
@@ -456,4 +483,21 @@ mod tests {
456
483
  Plan::Consume(..)
457
484
  ));
458
485
  }
486
+
487
+ #[test]
488
+ fn caches_past_the_size_limit_drop_the_least_recently_used() {
489
+ let cache = cache();
490
+ let half = || vec![0; super::MAX_BYTES / 2 + 1].into_boxed_slice();
491
+ cached(cache, &source(10), half());
492
+ cached(cache, &source(11), half());
493
+ assert_eq!(cache.stored(), 2);
494
+ assert!(matches!(
495
+ cache.plan(&name(), &source(11), None, true),
496
+ Plan::Consume(..)
497
+ ));
498
+ assert!(matches!(
499
+ cache.plan(&name(), &source(10), None, true),
500
+ Plan::Make(..)
501
+ ));
502
+ }
459
503
  }
@@ -5,6 +5,7 @@ use mini_racer_sync::lock;
5
5
 
6
6
  use crate::control::JsText;
7
7
  use crate::process::code_cache::Key;
8
+ use crate::text;
8
9
 
9
10
  const MAX_SOURCES: usize = 8;
10
11
  const MAX_BYTES: usize = 256 << 20;
@@ -26,16 +27,7 @@ impl Units {
26
27
  Some(match text {
27
28
  JsText::Latin1(bytes) => Self::OneByte(Arc::from(bytes.as_slice())),
28
29
  JsText::Utf8(bytes) if bytes.is_ascii() => Self::OneByte(Arc::from(bytes.as_slice())),
29
- JsText::Utf8(bytes) => {
30
- let text = std::str::from_utf8(bytes).ok()?;
31
- text.chars()
32
- .map(u8::try_from)
33
- .collect::<Result<_, _>>()
34
- .map_or_else(
35
- |_| Self::TwoByte(text.encode_utf16().collect()),
36
- Self::OneByte,
37
- )
38
- }
30
+ JsText::Utf8(bytes) => text::units(bytes)?,
39
31
  JsText::Utf16(units) => units
40
32
  .iter()
41
33
  .map(|&unit| u8::try_from(unit))
@@ -1,5 +1,5 @@
1
1
  use std::cmp::{Ordering, Reverse};
2
- use std::collections::BinaryHeap;
2
+ use std::collections::{BinaryHeap, HashMap};
3
3
  use std::sync::{Arc, Condvar, Mutex, Weak};
4
4
  use std::time::{Duration, Instant};
5
5
 
@@ -7,11 +7,13 @@ use mini_racer_sync::{DispatchId, Interrupt, lock, wait, wait_timeout};
7
7
 
8
8
  use crate::EngineError;
9
9
  use crate::control::{ControlBlock, Timer};
10
+ use crate::process::fork::Generation;
10
11
 
11
12
  struct Entry {
12
13
  at: Instant,
13
14
  seq: u64,
14
- block: Weak<ControlBlock>,
15
+ // The context's key in `Timers::blocks`.
16
+ block: usize,
15
17
  }
16
18
 
17
19
  impl PartialEq for Entry {
@@ -37,6 +39,15 @@ impl Ord for Entry {
37
39
  #[derive(Default)]
38
40
  struct Timers {
39
41
  heap: BinaryHeap<Reverse<Entry>>,
42
+ // The contexts the entries are for, by address, each from its first
43
+ // timed script until its runner exits (`forget`). Entries outlive their
44
+ // dispatches and a weak reference keeps a context's allocation, so an
45
+ // entry names its context rather than holds it: held to its deadline, a
46
+ // disposed context's allocation fragments the malloc arena of a thread
47
+ // that made it and exited. An address is mapped only while its context's
48
+ // allocation is, and an entry left after `forget` at most wakes the
49
+ // watchdog to reread a slot.
50
+ blocks: HashMap<usize, Weak<ControlBlock>>,
40
51
  seq: u64,
41
52
  }
42
53
 
@@ -46,16 +57,31 @@ pub struct Watchdog {
46
57
  }
47
58
 
48
59
  impl Watchdog {
49
- fn push(&self, at: Instant, block: Weak<ControlBlock>) {
60
+ fn push(&self, at: Instant, block: &Arc<ControlBlock>) {
61
+ let key = address(block);
50
62
  let mut timers = lock(&self.timers);
51
63
  timers.seq = timers.seq.wrapping_add(1);
52
64
  let seq = timers.seq;
53
- timers.heap.push(Reverse(Entry { at, seq, block }));
65
+ // A live reference at this address can only be this context's; a dead
66
+ // one is a context gone without `forget`, whose place this one took.
67
+ let slot = timers.blocks.entry(key).or_default();
68
+ if slot.strong_count() == 0 {
69
+ *slot = Arc::downgrade(block);
70
+ }
71
+ timers.heap.push(Reverse(Entry {
72
+ at,
73
+ seq,
74
+ block: key,
75
+ }));
54
76
  drop(timers);
55
77
  self.changed.notify_one();
56
78
  }
57
79
  }
58
80
 
81
+ fn address(block: &ControlBlock) -> usize {
82
+ std::ptr::from_ref(block).addr()
83
+ }
84
+
59
85
  // One thread per process serves every context's timeouts. It is kept per
60
86
  // fork generation: a forked child has the parent's watchdog state but not its
61
87
  // thread, and a runner of the parent may have held its lock at the fork.
@@ -118,7 +144,7 @@ fn arm(
118
144
  drop(timer);
119
145
  // A wake entry the watchdog no longer needs is harmless: it rereads the slot.
120
146
  if wake {
121
- watchdog.push(at, Arc::downgrade(block));
147
+ watchdog.push(at, block);
122
148
  }
123
149
  Ok(())
124
150
  }
@@ -166,6 +192,15 @@ pub fn clear_nested(block: &ControlBlock, id: DispatchId) {
166
192
  lock(&block.timer).nested.retain(|(_, due)| *due != id);
167
193
  }
168
194
 
195
+ /// Drops the watchdog's reference to a context whose runner has exited, so
196
+ /// a deadline still pending keeps none of it.
197
+ pub fn forget(block: &ControlBlock, generation: &Generation) {
198
+ let watchdog = lock(generation.watchdog()).clone();
199
+ if let Some(watchdog) = watchdog {
200
+ lock(&watchdog.timers).blocks.remove(&address(block));
201
+ }
202
+ }
203
+
169
204
  fn run(watchdog: &Watchdog) {
170
205
  loop {
171
206
  let mut timers = lock(&watchdog.timers);
@@ -186,8 +221,14 @@ fn run(watchdog: &Watchdog) {
186
221
  .peek()
187
222
  .is_some_and(|Reverse(entry)| entry.at <= now)
188
223
  {
189
- if let Some(Reverse(entry)) = timers.heap.pop() {
190
- due.push(entry.block);
224
+ if let Some(Reverse(entry)) = timers.heap.pop()
225
+ && let Some(block) = timers.blocks.get(&entry.block)
226
+ {
227
+ if block.strong_count() == 0 {
228
+ timers.blocks.remove(&entry.block);
229
+ } else {
230
+ due.push(Weak::clone(block));
231
+ }
191
232
  }
192
233
  }
193
234
  // Lock order: the heap is released before any context's timer
@@ -229,6 +270,6 @@ fn fire(watchdog: &Watchdog, block: &Arc<ControlBlock>, now: Instant) {
229
270
  block.term.request(Interrupt::Timeout(id));
230
271
  }
231
272
  if let Some(at) = next {
232
- watchdog.push(at, Arc::downgrade(block));
273
+ watchdog.push(at, block);
233
274
  }
234
275
  }
@@ -298,6 +298,7 @@ pub fn run(block: &Arc<ControlBlock>, generation: &'static Generation, start: St
298
298
  let _ = block.channel.respond(Ok(Vec::new()));
299
299
  }
300
300
  drop(closing);
301
+ watchdog::forget(block, generation);
301
302
  block.phase.set(Phase::Exited);
302
303
  }
303
304
 
@@ -77,8 +77,11 @@ const INTL_REFUSED: &str = "Intl is not supported in Snapshot.new scripts";
77
77
  /// V8 reads the blob for as long as an isolate made from it lives, whenever it
78
78
  /// makes a context from it or checks a code cache against it. Empty, it stands
79
79
  /// for no snapshot, as it does in `mini_racer`.
80
+ // The bytes have an allocation of their own, freed with the last strong
81
+ // reference: a `Weak` keeps its `Arc`'s allocation, and with it bytes kept
82
+ // inline there.
80
83
  #[derive(Clone, Default, PartialEq, Eq)]
81
- pub struct SnapshotBlob(Arc<[u8]>);
84
+ pub struct SnapshotBlob(Arc<Box<[u8]>>);
82
85
 
83
86
  impl SnapshotBlob {
84
87
  /// The word V8 opens each section of a blob with, which its deserializer
@@ -89,7 +92,7 @@ impl SnapshotBlob {
89
92
 
90
93
  pub fn new(bytes: &[u8]) -> Result<Self, InvalidSnapshot> {
91
94
  Self::check(bytes)?;
92
- Ok(Self(Arc::from(bytes)))
95
+ Ok(Self(Arc::new(Box::from(bytes))))
93
96
  }
94
97
 
95
98
  /// Refuses a blob as `mini_racer` does, in its order: one too long for
@@ -137,7 +140,7 @@ impl SnapshotBlob {
137
140
 
138
141
  /// A `SnapshotBlob` that lives only while a context made from it does.
139
142
  #[derive(Debug, Clone)]
140
- pub struct WeakSnapshotBlob(Weak<[u8]>);
143
+ pub struct WeakSnapshotBlob(Weak<Box<[u8]>>);
141
144
 
142
145
  impl WeakSnapshotBlob {
143
146
  #[must_use]
@@ -1,4 +1,5 @@
1
1
  use std::ffi::{c_char, c_void};
2
+ use std::mem::MaybeUninit;
2
3
  use std::ptr::NonNull;
3
4
  use std::sync::Arc;
4
5
  use std::time::Duration;
@@ -158,6 +159,47 @@ pub fn utf8_to_utf16(text: &[u8], out: &mut [u16]) -> Option<usize> {
158
159
  converted.is_ok().then_some(converted.count)
159
160
  }
160
161
 
162
+ /// A shared slice of a length fixed when it is made, written in place from its
163
+ /// first element on, a run at a time, and handed out once every one is
164
+ /// written.
165
+ pub struct Filling<T> {
166
+ slots: Arc<[MaybeUninit<T>]>,
167
+ written: usize,
168
+ }
169
+
170
+ impl<T: Copy> Filling<T> {
171
+ #[must_use]
172
+ pub fn new(len: usize) -> Self {
173
+ Self {
174
+ slots: Arc::new_uninit_slice(len),
175
+ written: 0,
176
+ }
177
+ }
178
+
179
+ /// Writes `run` after the elements already written; `None`, writing
180
+ /// nothing, when it does not fit.
181
+ pub fn append(&mut self, run: &[T]) -> Option<()> {
182
+ let end = self.written.checked_add(run.len())?;
183
+ let slots = Arc::get_mut(&mut self.slots)?.get_mut(self.written..end)?;
184
+ for (slot, &unit) in slots.iter_mut().zip(run) {
185
+ slot.write(unit);
186
+ }
187
+ self.written = end;
188
+ Some(())
189
+ }
190
+
191
+ /// The slice, once every element is written.
192
+ #[must_use]
193
+ pub fn filled(self) -> Option<Arc<[T]>> {
194
+ if self.written != self.slots.len() {
195
+ return None;
196
+ }
197
+ // SAFETY: `append` writes the elements in order from the first and
198
+ // counts only those it wrote, so every one is initialized.
199
+ Some(unsafe { self.slots.assume_init() })
200
+ }
201
+ }
202
+
161
203
  /// The address V8 names `isolate` by when it posts the isolate a task.
162
204
  #[must_use]
163
205
  pub fn isolate_key(isolate: &v8::Isolate) -> usize {