mini_racer_rs 1.5.3 → 1.6.0
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 +4 -4
- data/CHANGELOG.md +12 -0
- data/COMPATIBILITY.md +1 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/crates/mini_racer_engine/src/process/code_cache.rs +132 -6
- data/crates/mini_racer_engine/src/process/sources.rs +56 -1
- data/ext/mini_racer_rs/src/context.rs +49 -13
- data/ext/mini_racer_rs/src/counters.rs +25 -0
- data/ext/mini_racer_rs/src/rb_raw.rs +43 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 83f21cde14bd9acd57ee2a35e4536767bbd5c7dd6d4d4fe497c3e95043c9a6fc
|
|
4
|
+
data.tar.gz: f755934e6ee5581273b9b2b77c311890a160e313b06206da81284f746117fef9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c8a1fcacea4a5020de89ad8b7005a3692382c73115618ada1d941543a709afe5564637239ec9965ba5bcaf10083383c1dc87ba43116bfc9c2aff20911f6ab0eb
|
|
7
|
+
data.tar.gz: 6765a38eb4439310ac203693c7ba3e2350955e0bf2ce317d61865d4b2d4140e4fcbf86bd3db0e85c8b30bd6e4bd44c2fb23e0d4816955f5ce9b3e3342984bbb6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.6.0](https://github.com/andymai/mini_racer_rs/compare/v1.5.3...v1.6.0) (2026-10-24)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Features
|
|
7
|
+
|
|
8
|
+
* add MiniRacerRs.code_cache ([#164](https://github.com/andymai/mini_racer_rs/issues/164)) ([9238e61](https://github.com/andymai/mini_racer_rs/commit/9238e61e4ae9ab4ae16d50c0035ee7bf611ea258))
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Performance
|
|
12
|
+
|
|
13
|
+
* **ext:** copy a large eval source without the GVL ([#163](https://github.com/andymai/mini_racer_rs/issues/163)) ([25fa05e](https://github.com/andymai/mini_racer_rs/commit/25fa05e009086eca64641a699fb5190fb876c69c))
|
|
14
|
+
|
|
3
15
|
## [1.5.3](https://github.com/andymai/mini_racer_rs/compare/v1.5.2...v1.5.3) (2026-10-24)
|
|
4
16
|
|
|
5
17
|
|
data/COMPATIBILITY.md
CHANGED
|
@@ -103,6 +103,7 @@ What mini_racer_rs adds under `MiniRacerRs`. An internal id is not for callers.
|
|
|
103
103
|
| `imethod:MiniRacerRs::Pool#with` | done | n/a |
|
|
104
104
|
| `module:MiniRacerRs` | done | done |
|
|
105
105
|
| `module:MiniRacerRs::ForkHook` | internal | n/a |
|
|
106
|
+
| `smethod:MiniRacerRs.code_cache` | done | n/a |
|
|
106
107
|
| `smethod:MiniRacerRs.counters` | done | n/a |
|
|
107
108
|
| `smethod:MiniRacerRs.heap_limit` | done | n/a |
|
|
108
109
|
| `smethod:MiniRacerRs.heap_limit=` | done | n/a |
|
data/Cargo.lock
CHANGED
|
@@ -593,7 +593,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
|
593
593
|
|
|
594
594
|
[[package]]
|
|
595
595
|
name = "mini_racer_engine"
|
|
596
|
-
version = "1.
|
|
596
|
+
version = "1.6.0"
|
|
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.
|
|
609
|
+
version = "1.6.0"
|
|
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.
|
|
620
|
+
version = "1.6.0"
|
|
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.
|
|
630
|
+
version = "1.6.0"
|
|
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.
|
|
638
|
+
version = "1.6.0"
|
|
639
639
|
dependencies = [
|
|
640
640
|
"criterion",
|
|
641
641
|
"proptest",
|
data/Cargo.toml
CHANGED
|
@@ -15,7 +15,7 @@ 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.
|
|
18
|
+
version = "1.6.0"
|
|
19
19
|
|
|
20
20
|
[workspace.dependencies]
|
|
21
21
|
criterion = {version = "0.8", default-features = false, features = ["cargo_bench_support"]}
|
|
@@ -60,6 +60,11 @@ pub struct CodeCache {
|
|
|
60
60
|
hits: AtomicU64,
|
|
61
61
|
stored: AtomicU64,
|
|
62
62
|
rebuilt: AtomicU64,
|
|
63
|
+
refusals: AtomicU64,
|
|
64
|
+
// What `entries` holds, set under its lock, so a reader never waits on
|
|
65
|
+
// it and a forked child never finds it held.
|
|
66
|
+
kept: AtomicU64,
|
|
67
|
+
kept_bytes: AtomicU64,
|
|
63
68
|
}
|
|
64
69
|
|
|
65
70
|
/// What a cached script is found by: its file name, the startup snapshot its
|
|
@@ -243,19 +248,25 @@ impl CodeCache {
|
|
|
243
248
|
drop(building);
|
|
244
249
|
if data.len() > MAX_BYTES {
|
|
245
250
|
touch(&self.refused, stored.key, MAX_REFUSED);
|
|
251
|
+
self.refusals.fetch_add(1, Ordering::Relaxed);
|
|
246
252
|
return None;
|
|
247
253
|
}
|
|
248
254
|
let mut entries = lock(&self.entries);
|
|
249
255
|
if entries.iter().any(|entry| entry.key == stored.key) {
|
|
250
256
|
return None;
|
|
251
257
|
}
|
|
252
|
-
let dropped = room(&entries, data.len(), stored.first)
|
|
258
|
+
let Some(dropped) = room(&entries, data.len(), stored.first) else {
|
|
259
|
+
drop(entries);
|
|
260
|
+
self.refusals.fetch_add(1, Ordering::Relaxed);
|
|
261
|
+
return None;
|
|
262
|
+
};
|
|
253
263
|
entries.drain(..dropped);
|
|
254
264
|
entries.push(Arc::new(Entry {
|
|
255
265
|
key: stored.key.clone(),
|
|
256
266
|
data,
|
|
257
267
|
read: AtomicBool::new(false),
|
|
258
268
|
}));
|
|
269
|
+
self.publish(&entries);
|
|
259
270
|
drop(entries);
|
|
260
271
|
self.stored.fetch_add(1, Ordering::Relaxed);
|
|
261
272
|
Some(stored)
|
|
@@ -267,6 +278,7 @@ impl CodeCache {
|
|
|
267
278
|
/// leave no room for keeps what it held.
|
|
268
279
|
pub(crate) fn replace(&self, stored: &Stored, data: Box<[u8]>) {
|
|
269
280
|
if data.len() > MAX_BYTES {
|
|
281
|
+
self.refusals.fetch_add(1, Ordering::Relaxed);
|
|
270
282
|
return;
|
|
271
283
|
}
|
|
272
284
|
let mut entries = lock(&self.entries);
|
|
@@ -277,6 +289,8 @@ impl CodeCache {
|
|
|
277
289
|
let read = held.read.load(Ordering::Relaxed);
|
|
278
290
|
let Some(dropped) = room(&entries, data.len(), stored.first && !read) else {
|
|
279
291
|
entries.insert(at, held);
|
|
292
|
+
drop(entries);
|
|
293
|
+
self.refusals.fetch_add(1, Ordering::Relaxed);
|
|
280
294
|
return;
|
|
281
295
|
};
|
|
282
296
|
entries.drain(..dropped);
|
|
@@ -285,6 +299,7 @@ impl CodeCache {
|
|
|
285
299
|
data,
|
|
286
300
|
read: AtomicBool::new(read),
|
|
287
301
|
}));
|
|
302
|
+
self.publish(&entries);
|
|
288
303
|
drop(entries);
|
|
289
304
|
drop(held);
|
|
290
305
|
self.rebuilt.fetch_add(1, Ordering::Relaxed);
|
|
@@ -292,14 +307,26 @@ impl CodeCache {
|
|
|
292
307
|
|
|
293
308
|
/// Drops an entry V8 refused, so no later context tries it again.
|
|
294
309
|
pub(crate) fn forget(&self, entry: &Arc<Entry>) {
|
|
295
|
-
lock(&self.entries)
|
|
310
|
+
let mut entries = lock(&self.entries);
|
|
311
|
+
entries.retain(|kept| !Arc::ptr_eq(kept, entry));
|
|
312
|
+
self.publish(&entries);
|
|
313
|
+
drop(entries);
|
|
314
|
+
self.refusals.fetch_add(1, Ordering::Relaxed);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
fn publish(&self, entries: &[Arc<Entry>]) {
|
|
318
|
+
let (count, bytes) = totals(entries);
|
|
319
|
+
self.kept.store(count, Ordering::Relaxed);
|
|
320
|
+
self.kept_bytes.store(bytes, Ordering::Relaxed);
|
|
296
321
|
}
|
|
297
322
|
|
|
298
323
|
/// A forked child's cache: what its parent kept, had seen and refused,
|
|
299
324
|
/// unless a thread of the parent held it at the fork, under the same key.
|
|
300
325
|
pub(crate) fn inherited(&self) -> Self {
|
|
326
|
+
let entries = copied(&self.entries);
|
|
327
|
+
let (count, bytes) = totals(&entries);
|
|
301
328
|
Self {
|
|
302
|
-
entries: Mutex::new(
|
|
329
|
+
entries: Mutex::new(entries),
|
|
303
330
|
sightings: Mutex::new(copied(&self.sightings)),
|
|
304
331
|
building: Mutex::default(),
|
|
305
332
|
refused: Mutex::new(copied(&self.refused)),
|
|
@@ -307,6 +334,9 @@ impl CodeCache {
|
|
|
307
334
|
hits: AtomicU64::new(0),
|
|
308
335
|
stored: AtomicU64::new(0),
|
|
309
336
|
rebuilt: AtomicU64::new(0),
|
|
337
|
+
refusals: AtomicU64::new(0),
|
|
338
|
+
kept: AtomicU64::new(count),
|
|
339
|
+
kept_bytes: AtomicU64::new(bytes),
|
|
310
340
|
}
|
|
311
341
|
}
|
|
312
342
|
|
|
@@ -328,6 +358,26 @@ impl CodeCache {
|
|
|
328
358
|
self.rebuilt.load(Ordering::Relaxed)
|
|
329
359
|
}
|
|
330
360
|
|
|
361
|
+
/// Caches V8 rejected in this process, and caches it made or made again
|
|
362
|
+
/// and did not keep: past the size limit, or a first sighting's that
|
|
363
|
+
/// would have dropped an entry a context has read.
|
|
364
|
+
#[must_use]
|
|
365
|
+
pub fn refusals(&self) -> u64 {
|
|
366
|
+
self.refusals.load(Ordering::Relaxed)
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/// Caches kept now, a forked child's inherited ones included.
|
|
370
|
+
#[must_use]
|
|
371
|
+
pub fn entries(&self) -> u64 {
|
|
372
|
+
self.kept.load(Ordering::Relaxed)
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/// The bytes of the caches kept now.
|
|
376
|
+
#[must_use]
|
|
377
|
+
pub fn bytes(&self) -> u64 {
|
|
378
|
+
self.kept_bytes.load(Ordering::Relaxed)
|
|
379
|
+
}
|
|
380
|
+
|
|
331
381
|
fn sighting(&self, filename: &JsText, source: &JsText, snapshot: Option<u64>) -> u64 {
|
|
332
382
|
let mut hasher = self.hasher.build_hasher();
|
|
333
383
|
filename.hash(&mut hasher);
|
|
@@ -387,6 +437,15 @@ fn touch<T: PartialEq>(list: &Mutex<Vec<T>>, item: T, max: usize) -> bool {
|
|
|
387
437
|
found.is_some()
|
|
388
438
|
}
|
|
389
439
|
|
|
440
|
+
fn totals(entries: &[Arc<Entry>]) -> (u64, u64) {
|
|
441
|
+
let bytes = entries.iter().map(|entry| entry.data.len()).sum::<usize>();
|
|
442
|
+
(to_u64(entries.len()), to_u64(bytes))
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
fn to_u64(n: usize) -> u64 {
|
|
446
|
+
u64::try_from(n).unwrap_or(u64::MAX)
|
|
447
|
+
}
|
|
448
|
+
|
|
390
449
|
fn copied<T: Clone>(list: &Mutex<Vec<T>>) -> Vec<T> {
|
|
391
450
|
list.try_lock().map(|list| list.clone()).unwrap_or_default()
|
|
392
451
|
}
|
|
@@ -582,11 +641,14 @@ mod tests {
|
|
|
582
641
|
));
|
|
583
642
|
}
|
|
584
643
|
|
|
644
|
+
// Its entries, and counts of its own work alone.
|
|
585
645
|
#[test]
|
|
586
646
|
fn a_child_starts_from_its_parents_entries() {
|
|
587
647
|
let cache = cache();
|
|
588
648
|
cached(cache, &source(4), Box::from(&b"compiled"[..]));
|
|
589
649
|
let child: &'static CodeCache = Box::leak(Box::new(cache.inherited()));
|
|
650
|
+
assert_eq!((child.entries(), child.bytes()), (1, 8));
|
|
651
|
+
assert_eq!((child.stored(), child.hits()), (0, 0));
|
|
590
652
|
assert!(matches!(
|
|
591
653
|
child.plan(&name(), &source(4), None, true),
|
|
592
654
|
Plan::Consume(..)
|
|
@@ -600,12 +662,75 @@ mod tests {
|
|
|
600
662
|
let held = cache.entries.lock();
|
|
601
663
|
let child: &'static CodeCache = Box::leak(Box::new(cache.inherited()));
|
|
602
664
|
drop(held);
|
|
665
|
+
assert_eq!((child.entries(), child.bytes()), (0, 0));
|
|
603
666
|
assert!(matches!(
|
|
604
667
|
child.plan(&name(), &source(5), None, true),
|
|
605
668
|
Plan::Make(..)
|
|
606
669
|
));
|
|
607
670
|
}
|
|
608
671
|
|
|
672
|
+
#[test]
|
|
673
|
+
fn counts_the_entries_it_keeps_and_their_bytes() {
|
|
674
|
+
let cache = cache();
|
|
675
|
+
assert_eq!((cache.entries(), cache.bytes()), (0, 0));
|
|
676
|
+
let stored = kept(cache, &tiny(0), Box::from(&b"loaded"[..]));
|
|
677
|
+
cached(cache, &tiny(1), Box::from(&b"compiled"[..]));
|
|
678
|
+
assert_eq!((cache.entries(), cache.bytes()), (2, 14));
|
|
679
|
+
cache.replace(&stored, Box::from(&b"called again"[..]));
|
|
680
|
+
assert_eq!((cache.entries(), cache.bytes()), (2, 20));
|
|
681
|
+
let Plan::Consume(entry, _) = cache.plan(&name(), &tiny(1), None, true) else {
|
|
682
|
+
panic!("a stored cache is found");
|
|
683
|
+
};
|
|
684
|
+
cache.forget(&entry);
|
|
685
|
+
assert_eq!((cache.entries(), cache.bytes()), (1, 12));
|
|
686
|
+
// One past the count drops the least recently used, `tiny(0)`'s.
|
|
687
|
+
for seed in 2..MAX_ENTRIES + 2 {
|
|
688
|
+
cached(cache, &tiny(seed), Box::from(&b"compiled"[..]));
|
|
689
|
+
}
|
|
690
|
+
let full = u64::try_from(MAX_ENTRIES).unwrap_or(u64::MAX);
|
|
691
|
+
assert_eq!((cache.entries(), cache.bytes()), (full, full * 8));
|
|
692
|
+
}
|
|
693
|
+
|
|
694
|
+
// V8's rejections, and caches built or built again but not kept; an
|
|
695
|
+
// entry gone before its builder built it again refuses nothing.
|
|
696
|
+
#[test]
|
|
697
|
+
fn counts_each_cache_rejected_or_refused() {
|
|
698
|
+
let cache = cache();
|
|
699
|
+
let too_large = || vec![0; super::MAX_BYTES + 1].into_boxed_slice();
|
|
700
|
+
cached(cache, &tiny(0), too_large());
|
|
701
|
+
assert_eq!(cache.refusals(), 1);
|
|
702
|
+
let stored = kept(cache, &tiny(1), Box::from(&b"loaded"[..]));
|
|
703
|
+
cache.replace(&stored, too_large());
|
|
704
|
+
assert_eq!(cache.refusals(), 2);
|
|
705
|
+
let Plan::Consume(entry, _) = cache.plan(&name(), &tiny(1), None, true) else {
|
|
706
|
+
panic!("a stored cache is found");
|
|
707
|
+
};
|
|
708
|
+
cache.forget(&entry);
|
|
709
|
+
assert_eq!(cache.refusals(), 3);
|
|
710
|
+
cache.replace(&stored, Box::from(&b"called"[..]));
|
|
711
|
+
assert_eq!((cache.refusals(), cache.entries()), (3, 0));
|
|
712
|
+
assert_eq!((cache.stored(), cache.rebuilt()), (1, 0));
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// A forked child may find the lock held by a thread of its parent, so
|
|
716
|
+
// the counts are read without it.
|
|
717
|
+
#[test]
|
|
718
|
+
fn its_counts_are_read_without_its_lock() {
|
|
719
|
+
let cache = cache();
|
|
720
|
+
cached(cache, &tiny(0), Box::from(&b"compiled"[..]));
|
|
721
|
+
let held = cache.entries.lock();
|
|
722
|
+
assert_eq!(
|
|
723
|
+
(
|
|
724
|
+
cache.entries(),
|
|
725
|
+
cache.bytes(),
|
|
726
|
+
cache.stored(),
|
|
727
|
+
cache.refusals()
|
|
728
|
+
),
|
|
729
|
+
(1, 8, 1, 0)
|
|
730
|
+
);
|
|
731
|
+
drop(held);
|
|
732
|
+
}
|
|
733
|
+
|
|
609
734
|
#[test]
|
|
610
735
|
fn a_cache_larger_than_the_limit_is_refused_and_not_rebuilt() {
|
|
611
736
|
let cache = cache();
|
|
@@ -754,8 +879,9 @@ mod tests {
|
|
|
754
879
|
consumed(cache, &tiny(0));
|
|
755
880
|
consumed(cache, &roomy);
|
|
756
881
|
let crowded = bundle(7, FIRST_SIGHTING);
|
|
882
|
+
assert_eq!(cache.refusals(), 0);
|
|
757
883
|
first(cache, &crowded, Box::from(&b"compiled"[..]));
|
|
758
|
-
assert_eq!(cache.stored(), 65);
|
|
884
|
+
assert_eq!((cache.stored(), cache.refusals()), (65, 1));
|
|
759
885
|
consumed(cache, &tiny(2));
|
|
760
886
|
let Plan::Make(building) = cache.plan(&name(), &crowded, None, true) else {
|
|
761
887
|
panic!("a second sighting makes a cache");
|
|
@@ -775,7 +901,7 @@ mod tests {
|
|
|
775
901
|
cached(cache, &tiny(1), half());
|
|
776
902
|
consumed(cache, &tiny(1));
|
|
777
903
|
first(cache, &bundle(8, FIRST_SIGHTING), half());
|
|
778
|
-
assert_eq!(cache.stored(), 1);
|
|
904
|
+
assert_eq!((cache.stored(), cache.refusals()), (1, 1));
|
|
779
905
|
cached(cache, &tiny(2), half());
|
|
780
906
|
assert_eq!(cache.stored(), 2);
|
|
781
907
|
first(cache, &bundle(9, FIRST_SIGHTING), half());
|
|
@@ -858,7 +984,7 @@ mod tests {
|
|
|
858
984
|
.store(building, Box::from(&b"loaded"[..]))
|
|
859
985
|
.unwrap_or_else(|| panic!("a cache with room is kept"));
|
|
860
986
|
cache.replace(&stored, half());
|
|
861
|
-
assert_eq!(cache.rebuilt(), 0);
|
|
987
|
+
assert_eq!((cache.rebuilt(), cache.refusals()), (0, 1));
|
|
862
988
|
assert_eq!(data_of(cache, &text), b"loaded");
|
|
863
989
|
// Read now, it is built again as any other entry.
|
|
864
990
|
consumed(cache, &text);
|
|
@@ -71,6 +71,10 @@ pub struct Sources {
|
|
|
71
71
|
made: AtomicU64,
|
|
72
72
|
reads: AtomicU64,
|
|
73
73
|
conversions: AtomicU64,
|
|
74
|
+
// What the list keeps, set under its lock, so a reader never waits on it
|
|
75
|
+
// and a forked child never finds it held.
|
|
76
|
+
kept: AtomicU64,
|
|
77
|
+
kept_bytes: AtomicU64,
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
impl Default for Sources {
|
|
@@ -179,6 +183,19 @@ impl List {
|
|
|
179
183
|
.find(|(refused, _)| refused == key)
|
|
180
184
|
.map_or_else(|| least_bytes(text), |&(_, bytes)| bytes)
|
|
181
185
|
}
|
|
186
|
+
|
|
187
|
+
fn totals(&self) -> (u64, u64) {
|
|
188
|
+
let bytes = self
|
|
189
|
+
.kept
|
|
190
|
+
.iter()
|
|
191
|
+
.map(|kept| kept.units.bytes())
|
|
192
|
+
.sum::<usize>();
|
|
193
|
+
(to_u64(self.kept.len()), to_u64(bytes))
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
fn to_u64(n: usize) -> u64 {
|
|
198
|
+
u64::try_from(n).unwrap_or(u64::MAX)
|
|
182
199
|
}
|
|
183
200
|
|
|
184
201
|
// The fewest bytes `text` can take as units, so a source that cannot fit is
|
|
@@ -201,9 +218,17 @@ impl Sources {
|
|
|
201
218
|
made: AtomicU64::new(0),
|
|
202
219
|
reads: AtomicU64::new(0),
|
|
203
220
|
conversions: AtomicU64::new(0),
|
|
221
|
+
kept: AtomicU64::new(0),
|
|
222
|
+
kept_bytes: AtomicU64::new(0),
|
|
204
223
|
}
|
|
205
224
|
}
|
|
206
225
|
|
|
226
|
+
fn publish(&self, list: &List) {
|
|
227
|
+
let (count, bytes) = list.totals();
|
|
228
|
+
self.kept.store(count, Ordering::Relaxed);
|
|
229
|
+
self.kept_bytes.store(bytes, Ordering::Relaxed);
|
|
230
|
+
}
|
|
231
|
+
|
|
207
232
|
/// A number for a new context to read shared sources as, told apart from
|
|
208
233
|
/// every other context of this process.
|
|
209
234
|
pub(crate) fn reader(&self) -> u64 {
|
|
@@ -223,6 +248,7 @@ impl Sources {
|
|
|
223
248
|
// unread is read again, and kept, rather than copied again.
|
|
224
249
|
let found = list.find(key, reader);
|
|
225
250
|
let spent = list.sweep();
|
|
251
|
+
self.publish(&list);
|
|
226
252
|
let room = found.is_none() && list.room(list.least_bytes(key, text), self.bounds);
|
|
227
253
|
drop(list);
|
|
228
254
|
drop(spent);
|
|
@@ -271,6 +297,7 @@ impl Sources {
|
|
|
271
297
|
held: 1,
|
|
272
298
|
readers,
|
|
273
299
|
});
|
|
300
|
+
self.publish(&list);
|
|
274
301
|
drop(list);
|
|
275
302
|
// Freed once the lock is released, as a large source takes a while,
|
|
276
303
|
// like the spent copies above.
|
|
@@ -305,9 +332,11 @@ impl Sources {
|
|
|
305
332
|
swept: list.swept.clone(),
|
|
306
333
|
})
|
|
307
334
|
.unwrap_or_default();
|
|
335
|
+
let inherited = Self::bounded(self.bounds.0, self.bounds.1);
|
|
336
|
+
inherited.publish(&list);
|
|
308
337
|
Self {
|
|
309
338
|
list: Mutex::new(list),
|
|
310
|
-
..
|
|
339
|
+
..inherited
|
|
311
340
|
}
|
|
312
341
|
}
|
|
313
342
|
|
|
@@ -329,6 +358,18 @@ impl Sources {
|
|
|
329
358
|
self.conversions.load(Ordering::Relaxed)
|
|
330
359
|
}
|
|
331
360
|
|
|
361
|
+
/// Sources kept now, a forked child's inherited ones included.
|
|
362
|
+
#[must_use]
|
|
363
|
+
pub fn kept(&self) -> u64 {
|
|
364
|
+
self.kept.load(Ordering::Relaxed)
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
/// The bytes of the sources kept now, one a unit or two, as V8 reads them.
|
|
368
|
+
#[must_use]
|
|
369
|
+
pub fn kept_bytes(&self) -> u64 {
|
|
370
|
+
self.kept_bytes.load(Ordering::Relaxed)
|
|
371
|
+
}
|
|
372
|
+
|
|
332
373
|
/// Strings alive now that read a kept source.
|
|
333
374
|
#[must_use]
|
|
334
375
|
pub fn reading(&self) -> usize {
|
|
@@ -412,10 +453,12 @@ mod tests {
|
|
|
412
453
|
let sources = Sources::default();
|
|
413
454
|
let text = script(1);
|
|
414
455
|
let key = key(&text);
|
|
456
|
+
assert_eq!((sources.kept(), sources.kept_bytes()), (0, 0));
|
|
415
457
|
let first = one_byte(sources.units(&key, &text, sources.reader()));
|
|
416
458
|
let second = one_byte(sources.units(&key, &text, sources.reader()));
|
|
417
459
|
assert!(Arc::ptr_eq(&first, &second));
|
|
418
460
|
assert_eq!(sources.made(), 1);
|
|
461
|
+
assert_eq!((sources.kept(), sources.kept_bytes()), (1, 2 << 20));
|
|
419
462
|
assert_eq!(sources.reading(), 2);
|
|
420
463
|
drop((first, second));
|
|
421
464
|
assert_eq!(sources.reading(), 0);
|
|
@@ -491,6 +534,7 @@ mod tests {
|
|
|
491
534
|
assert!(copy.upgrade().is_some());
|
|
492
535
|
drop(sources.units(&other_key, &other, sources.reader()));
|
|
493
536
|
assert!(copy.upgrade().is_none());
|
|
537
|
+
assert_eq!((sources.kept(), sources.kept_bytes()), (1, 2 << 20));
|
|
494
538
|
drop(sources.units(&key, &text, sources.reader()));
|
|
495
539
|
assert_eq!(sources.made(), 3);
|
|
496
540
|
}
|
|
@@ -611,6 +655,12 @@ mod tests {
|
|
|
611
655
|
drop(still_read);
|
|
612
656
|
assert!(two_byte(sources.units(&astral_key, &astral, sources.reader())).len() > 2 << 20);
|
|
613
657
|
assert_eq!((sources.conversions(), sources.made()), (3, 2));
|
|
658
|
+
// The copy one context read is swept, and the two-byte one counts two
|
|
659
|
+
// bytes a unit.
|
|
660
|
+
assert_eq!(
|
|
661
|
+
(sources.kept(), sources.kept_bytes()),
|
|
662
|
+
(1, 2 * ((2 << 20) + 2))
|
|
663
|
+
);
|
|
614
664
|
}
|
|
615
665
|
|
|
616
666
|
#[test]
|
|
@@ -637,6 +687,8 @@ mod tests {
|
|
|
637
687
|
let key = key(&text);
|
|
638
688
|
let shared = parent.units(&key, &text, parent.reader());
|
|
639
689
|
let child = parent.inherited();
|
|
690
|
+
assert_eq!((child.kept(), child.kept_bytes()), (1, 2 << 20));
|
|
691
|
+
assert_eq!((child.made(), child.reads()), (0, 0));
|
|
640
692
|
let in_child = child.units(&key, &text, child.reader());
|
|
641
693
|
assert!(same(shared.as_ref(), in_child.as_ref()));
|
|
642
694
|
assert_eq!(child.reading(), 2);
|
|
@@ -714,8 +766,11 @@ mod tests {
|
|
|
714
766
|
let key = key(&text);
|
|
715
767
|
let shared = one_byte(parent.units(&key, &text, parent.reader()));
|
|
716
768
|
let held = parent.list.lock();
|
|
769
|
+
// Read without the lock, which a child may find held.
|
|
770
|
+
assert_eq!((parent.kept(), parent.kept_bytes()), (1, 2 << 20));
|
|
717
771
|
let child = parent.inherited();
|
|
718
772
|
drop(held);
|
|
773
|
+
assert_eq!((child.kept(), child.kept_bytes()), (0, 0));
|
|
719
774
|
assert!(!Arc::ptr_eq(
|
|
720
775
|
&shared,
|
|
721
776
|
&one_byte(child.units(&key, &text, child.reader()))
|
|
@@ -27,6 +27,7 @@ const ISOLATE_BYTES: usize = 2 << 20;
|
|
|
27
27
|
const POLL: std::time::Duration = std::time::Duration::from_millis(1);
|
|
28
28
|
// mini_racer warns of every fiftieth level of js->ruby->js nesting.
|
|
29
29
|
const DEEP_NESTING: u32 = 50;
|
|
30
|
+
const SOURCE_WITHOUT_GVL: usize = 1 << 20;
|
|
30
31
|
|
|
31
32
|
static OWNED: LazyId = LazyId::new("owned?");
|
|
32
33
|
|
|
@@ -119,19 +120,54 @@ impl Drop for Locked {
|
|
|
119
120
|
|
|
120
121
|
// mini_racer sends ISO-8859-1 and UTF-16LE strings in their own encoding and
|
|
121
122
|
// every other string's bytes as UTF-8.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
123
|
+
#[derive(Clone, Copy)]
|
|
124
|
+
enum Sent {
|
|
125
|
+
Utf8,
|
|
126
|
+
Latin1,
|
|
127
|
+
Utf16,
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
impl Sent {
|
|
131
|
+
fn of(string: RString) -> Self {
|
|
132
|
+
match RbEncoding::from(string.enc_get()).name() {
|
|
133
|
+
"ISO-8859-1" => Self::Latin1,
|
|
134
|
+
"UTF-16LE" => Self::Utf16,
|
|
135
|
+
_ => Self::Utf8,
|
|
132
136
|
}
|
|
133
|
-
|
|
134
|
-
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
fn text(self, bytes: &[u8]) -> Option<JsText> {
|
|
140
|
+
Some(match self {
|
|
141
|
+
Self::Utf8 => JsText::Utf8(bytes.to_vec()),
|
|
142
|
+
Self::Latin1 => JsText::Latin1(bytes.to_vec()),
|
|
143
|
+
Self::Utf16 => {
|
|
144
|
+
let (units, rest) = bytes.as_chunks::<2>();
|
|
145
|
+
if !rest.is_empty() {
|
|
146
|
+
return None;
|
|
147
|
+
}
|
|
148
|
+
JsText::Utf16(units.iter().map(|unit| u16::from_le_bytes(*unit)).collect())
|
|
149
|
+
}
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
pub fn js_text(ruby: &Ruby, string: RString) -> Result<JsText, Error> {
|
|
155
|
+
let sent = Sent::of(string);
|
|
156
|
+
rb_raw::with_string_bytes(ruby, string, |bytes| sent.text(bytes))
|
|
157
|
+
.ok_or_else(|| errors::undeserializable(ruby))
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
// Under the GVL, copying a source this long (about 0.4 ms a megabyte) would
|
|
161
|
+
// stall every other Ruby thread; a shorter copy costs less than taking the
|
|
162
|
+
// GVL back from one that is busy.
|
|
163
|
+
fn source_text(ruby: &Ruby, string: RString) -> Result<JsText, Error> {
|
|
164
|
+
if string.len() < SOURCE_WITHOUT_GVL {
|
|
165
|
+
return js_text(ruby, string);
|
|
166
|
+
}
|
|
167
|
+
let sent = Sent::of(string);
|
|
168
|
+
rb_raw::read_without_gvl(ruby, string, |bytes| sent.text(bytes))?
|
|
169
|
+
.map_err(|_| errors::internal(ruby, "panic while copying the source"))?
|
|
170
|
+
.ok_or_else(|| errors::undeserializable(ruby))
|
|
135
171
|
}
|
|
136
172
|
|
|
137
173
|
// mini_racer looks for DateTime at each Context.new until it exists.
|
|
@@ -301,7 +337,7 @@ impl Context {
|
|
|
301
337
|
}
|
|
302
338
|
_ => JsText::from("<eval>"),
|
|
303
339
|
};
|
|
304
|
-
let source =
|
|
340
|
+
let source = source_text(ruby, source)?;
|
|
305
341
|
let options = ScriptOptions {
|
|
306
342
|
awaited,
|
|
307
343
|
timeout: timeout::current(ruby)?,
|
|
@@ -1,13 +1,38 @@
|
|
|
1
|
+
use magnus::value::ReprValue;
|
|
1
2
|
use magnus::{Error, Object, RHash, RModule, Ruby};
|
|
3
|
+
use mini_racer_engine::process::fork;
|
|
2
4
|
|
|
3
5
|
use crate::context::Context;
|
|
4
6
|
use crate::export;
|
|
5
7
|
|
|
6
8
|
pub fn init(rs: RModule) -> Result<(), Error> {
|
|
7
9
|
rs.define_singleton_method("counters", export!(function counters, 1))?;
|
|
10
|
+
rs.define_singleton_method("code_cache", export!(function code_cache, 0))?;
|
|
8
11
|
Ok(())
|
|
9
12
|
}
|
|
10
13
|
|
|
14
|
+
// Read from atomics alone, so a forked child reads the copy it inherited
|
|
15
|
+
// without a lock a thread of its parent may have held at the fork.
|
|
16
|
+
fn code_cache(ruby: &Ruby) -> Result<RHash, Error> {
|
|
17
|
+
let generation = fork::current();
|
|
18
|
+
let (cache, sources) = (generation.code_cache(), generation.sources());
|
|
19
|
+
let hash = ruby.hash_new();
|
|
20
|
+
for (name, count) in [
|
|
21
|
+
("entries", cache.entries()),
|
|
22
|
+
("bytes", cache.bytes()),
|
|
23
|
+
("hits", cache.hits()),
|
|
24
|
+
("builds", cache.stored()),
|
|
25
|
+
("rebuilds", cache.rebuilt()),
|
|
26
|
+
("refused", cache.refusals()),
|
|
27
|
+
("shared_sources", sources.kept()),
|
|
28
|
+
("shared_bytes", sources.kept_bytes()),
|
|
29
|
+
] {
|
|
30
|
+
hash.aset(ruby.to_symbol(name), count)?;
|
|
31
|
+
}
|
|
32
|
+
hash.freeze();
|
|
33
|
+
Ok(hash)
|
|
34
|
+
}
|
|
35
|
+
|
|
11
36
|
// Read without the context's lock, so a thread can read a context another
|
|
12
37
|
// thread is running.
|
|
13
38
|
fn counters(ruby: &Ruby, context: &Context) -> Result<RHash, Error> {
|
|
@@ -559,6 +559,49 @@ pub fn with_string_bytes<R>(_ruby: &Ruby, s: RString, read: impl FnOnce(&[u8]) -
|
|
|
559
559
|
read(unsafe { s.as_slice() })
|
|
560
560
|
}
|
|
561
561
|
|
|
562
|
+
/// Runs `read` over the bytes `s` holds now, without the GVL, so Ruby's other
|
|
563
|
+
/// threads run meanwhile. It reads a frozen share of `s` that this frame
|
|
564
|
+
/// holds, so the GC neither frees nor moves it, and whose bytes a change to
|
|
565
|
+
/// `s` copies before it writes. An interrupt already pending is run first, as
|
|
566
|
+
/// `check_ints` runs one, up to twice, then `read` runs with the GVL held. The
|
|
567
|
+
/// outer `Err` is what making the share or an interrupt raised.
|
|
568
|
+
pub fn read_without_gvl<F, R>(
|
|
569
|
+
ruby: &Ruby,
|
|
570
|
+
s: RString,
|
|
571
|
+
read: F,
|
|
572
|
+
) -> Result<Result<R, Panicked>, Error>
|
|
573
|
+
where
|
|
574
|
+
F: Fn(&[u8]) -> R + Sync,
|
|
575
|
+
R: Send,
|
|
576
|
+
{
|
|
577
|
+
let raw = s.as_raw();
|
|
578
|
+
let frozen = protect(|| {
|
|
579
|
+
// SAFETY: the `&Ruby` argument proves this thread holds the GVL, and
|
|
580
|
+
// `protect` catches what an allocation that fails raises.
|
|
581
|
+
unsafe { rb_sys::rb_str_new_frozen(raw) }
|
|
582
|
+
})?;
|
|
583
|
+
// SAFETY: `frozen` is the live String rb_str_new_frozen returned.
|
|
584
|
+
let share = RString::from_value(unsafe { Value::from_raw(frozen) })
|
|
585
|
+
.ok_or_else(|| Error::new(ruby.exception_type_error(), "expected String"))?;
|
|
586
|
+
let read = &read;
|
|
587
|
+
for _ in 0..2 {
|
|
588
|
+
// SAFETY: a frozen String's bytes never change, and `share` stays on
|
|
589
|
+
// this stack, where the GC of a thread that takes the GVL meanwhile
|
|
590
|
+
// finds and pins it, until `black_box` reads it after the call.
|
|
591
|
+
let bytes = unsafe { share.as_slice() };
|
|
592
|
+
if let Some(result) = nogvl(ruby, move || read(bytes), &|| {}) {
|
|
593
|
+
std::hint::black_box(&share);
|
|
594
|
+
return Ok(result);
|
|
595
|
+
}
|
|
596
|
+
check_ints(ruby)?;
|
|
597
|
+
}
|
|
598
|
+
// SAFETY: as above, with the GVL held and no Ruby code run until it ends.
|
|
599
|
+
let bytes = unsafe { share.as_slice() };
|
|
600
|
+
let result = guard(|| read(bytes));
|
|
601
|
+
std::hint::black_box(&share);
|
|
602
|
+
Ok(result)
|
|
603
|
+
}
|
|
604
|
+
|
|
562
605
|
/// The magnitude of an Integer as little-endian 64-bit words, and whether it is
|
|
563
606
|
/// negative; `None` past `max_words`.
|
|
564
607
|
pub fn integer_words(
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mini_racer_rs
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.6.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Andy Aragon
|
|
@@ -154,9 +154,9 @@ licenses:
|
|
|
154
154
|
- MIT
|
|
155
155
|
metadata:
|
|
156
156
|
homepage_uri: https://github.com/andymai/mini_racer_rs
|
|
157
|
-
source_code_uri: https://github.com/andymai/mini_racer_rs/tree/v1.
|
|
158
|
-
changelog_uri: https://github.com/andymai/mini_racer_rs/blob/v1.
|
|
159
|
-
documentation_uri: https://github.com/andymai/mini_racer_rs/blob/v1.
|
|
157
|
+
source_code_uri: https://github.com/andymai/mini_racer_rs/tree/v1.6.0
|
|
158
|
+
changelog_uri: https://github.com/andymai/mini_racer_rs/blob/v1.6.0/CHANGELOG.md
|
|
159
|
+
documentation_uri: https://github.com/andymai/mini_racer_rs/blob/v1.6.0/README.md#api-at-a-glance
|
|
160
160
|
bug_tracker_uri: https://github.com/andymai/mini_racer_rs/issues
|
|
161
161
|
funding_uri: https://github.com/sponsors/andymai
|
|
162
162
|
rubygems_mfa_required: 'true'
|