mini_racer_rs 1.5.2 → 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.
@@ -1,13 +1,18 @@
1
1
  use std::collections::hash_map::RandomState;
2
2
  use std::hash::{BuildHasher, Hash, Hasher};
3
- use std::sync::atomic::{AtomicU64, Ordering};
3
+ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
4
4
  use std::sync::{Arc, Mutex, OnceLock};
5
+ use std::time::Duration;
5
6
 
6
7
  use mini_racer_sync::lock;
7
8
 
8
9
  use crate::control::JsText;
9
10
 
10
- const MAX_ENTRIES: usize = 8;
11
+ // The byte cap bounds the memory; the count keeps each large eval's scan of
12
+ // the list short. Caches of scripts of 128 KiB to 1 MiB measured 99 KB
13
+ // (React's bundle) to 530 KB (Autoprefixer's), so 64 of them take about 34 MB
14
+ // at most, where 8 entries let a few such scripts push a bundle's cache out.
15
+ const MAX_ENTRIES: usize = 64;
11
16
  const MAX_BYTES: usize = 256 << 20;
12
17
  // 32 KiB of digests, so a bundle loaded again after thousands of large scripts
13
18
  // evaluated once is still seen a second time: a list of 64 forgot one after 70
@@ -19,15 +24,26 @@ const MAX_REFUSED: usize = 8;
19
24
  // few points between, to tell one sighting from another without reading it all.
20
25
  const SAMPLE: usize = 4096;
21
26
  const SAMPLE_POINTS: usize = 8;
22
-
23
- /// V8 code caches for the large scripts one process's contexts have run, so
24
- /// the next context to evaluate one reads its compiled code back instead of
25
- /// compiling it again.
27
+ // A first sighting this large is cached at once when its top-level run took
28
+ // at least as long as its compile, as a bundle that renders as it loads does:
29
+ // a 44 MB server rendering bundle compiled in 186 ms and ran for 690, and its
30
+ // cache of 8 MB took 35 ms. A data literal runs in a fraction of its compile,
31
+ // and its cache is larger than its source: 16 MiB of JSON compiled in 312 ms,
32
+ // ran for 55 and made a cache of 24 MB in 164 ms.
33
+ const FIRST_SIGHTING: usize = 16 << 20;
34
+
35
+ /// V8 code caches for the scripts of 128 KiB or more one process's contexts
36
+ /// have run, so the next context to evaluate one reads its compiled code back
37
+ /// instead of compiling it again.
26
38
  ///
27
39
  /// A script is cached the second time the process sees it. The first sighting
28
40
  /// costs a sampled digest and no cache, as a large script evaluated once
29
41
  /// (per-request data, say) gains nothing from one; it costs the full digest
30
- /// too only in a context that reads the script from its shared source.
42
+ /// too only in a context that reads the script from its shared source. Such a
43
+ /// context caches a first sighting of 16 MiB or more whose run took at least
44
+ /// as long as its compile (`worth_first`), unless that would drop an entry a
45
+ /// context has read. The context that cached a script caches it again after
46
+ /// its next call (`replace`).
31
47
  #[derive(Default)]
32
48
  pub struct CodeCache {
33
49
  // Most recently used last.
@@ -43,6 +59,12 @@ pub struct CodeCache {
43
59
  hasher: RandomState,
44
60
  hits: AtomicU64,
45
61
  stored: AtomicU64,
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,
46
68
  }
47
69
 
48
70
  /// What a cached script is found by: its file name, the startup snapshot its
@@ -60,6 +82,8 @@ pub(crate) struct Key {
60
82
  pub(crate) struct Entry {
61
83
  key: Key,
62
84
  data: Box<[u8]>,
85
+ // A context compiled from it.
86
+ read: AtomicBool,
63
87
  }
64
88
 
65
89
  impl Entry {
@@ -68,10 +92,20 @@ impl Entry {
68
92
  }
69
93
  }
70
94
 
95
+ /// A kept entry, as the context that built it builds it again once a call
96
+ /// has compiled more of the script.
97
+ pub(crate) struct Stored {
98
+ key: Key,
99
+ first: bool,
100
+ }
101
+
71
102
  /// A key being built; dropped unbuilt, it lets another context build it.
72
103
  pub(crate) struct Building {
73
104
  cache: &'static CodeCache,
74
105
  key: Key,
106
+ // At the script's first sighting, so it makes room only from entries no
107
+ // context has read.
108
+ first: bool,
75
109
  }
76
110
 
77
111
  impl Drop for Building {
@@ -86,6 +120,9 @@ pub(crate) enum Plan {
86
120
  /// first sighting whose source is shared, a key being built elsewhere or
87
121
  /// a refused cache.
88
122
  Compile(Option<Key>),
123
+ /// From source, a first sighting of 16 MiB or more whose source is
124
+ /// shared, cached once it has run if `worth_first` says so (`CodeCache::first`).
125
+ First(Key),
89
126
  /// From source, then a cache made once the dispatch is over.
90
127
  Make(Building),
91
128
  /// From the entry, falling back to source if V8 refuses it.
@@ -98,7 +135,7 @@ impl Plan {
98
135
  match self {
99
136
  Self::Compile(key) => key.as_ref(),
100
137
  Self::Make(building) => Some(&building.key),
101
- Self::Consume(_, key) => Some(key),
138
+ Self::First(key) | Self::Consume(_, key) => Some(key),
102
139
  }
103
140
  }
104
141
  }
@@ -133,6 +170,9 @@ impl CodeCache {
133
170
  digest: self.hasher.hash_one(source),
134
171
  sighting,
135
172
  };
173
+ if first && source.byte_len() >= FIRST_SIGHTING {
174
+ return Plan::First(key);
175
+ }
136
176
  if first {
137
177
  return Plan::Compile(Some(key));
138
178
  }
@@ -153,13 +193,32 @@ impl CodeCache {
153
193
 
154
194
  /// Claims `key` for this context to build, unless another is building it.
155
195
  pub(crate) fn begin(&'static self, key: Key) -> Option<Building> {
196
+ self.claim(key, false)
197
+ }
198
+
199
+ /// Claims a first sighting's `key` for this context to build once its
200
+ /// script has run, unless it was cached, refused or claimed meanwhile.
201
+ pub(crate) fn first(&'static self, key: Key) -> Option<Building> {
202
+ if lock(&self.entries).iter().any(|entry| entry.key == key)
203
+ || lock(&self.refused).contains(&key)
204
+ {
205
+ return None;
206
+ }
207
+ self.claim(key, true)
208
+ }
209
+
210
+ fn claim(&'static self, key: Key, first: bool) -> Option<Building> {
156
211
  let mut building = lock(&self.building);
157
212
  if building.contains(&key) {
158
213
  return None;
159
214
  }
160
215
  building.push(key.clone());
161
216
  drop(building);
162
- Some(Building { cache: self, key })
217
+ Some(Building {
218
+ cache: self,
219
+ key,
220
+ first,
221
+ })
163
222
  }
164
223
 
165
224
  /// A startup snapshot's identity for `Key`, from a sample of the blob:
@@ -170,49 +229,114 @@ impl CodeCache {
170
229
  hasher.finish()
171
230
  }
172
231
 
173
- pub(crate) fn hit(&self) {
232
+ pub(crate) fn hit(&self, entry: &Entry) {
233
+ entry.read.store(true, Ordering::Relaxed);
174
234
  self.hits.fetch_add(1, Ordering::Relaxed);
175
235
  }
176
236
 
177
237
  /// Keeps `data` for the key built, dropping the least recently used
178
- /// entries past the limits; a cache larger than all of them is refused.
179
- pub(crate) fn store(&self, building: Building, data: Box<[u8]>) {
180
- let key = building.key.clone();
238
+ /// entries past the limits; a cache larger than all of them is refused. A
239
+ /// first sighting's cache that would drop an entry a context has read is
240
+ /// not kept, and the script is cached at its next sighting instead.
241
+ /// A kept entry comes back as what its builder hands `replace` once a
242
+ /// call has run.
243
+ pub(crate) fn store(&self, building: Building, data: Box<[u8]>) -> Option<Stored> {
244
+ let stored = Stored {
245
+ key: building.key.clone(),
246
+ first: building.first,
247
+ };
181
248
  drop(building);
182
249
  if data.len() > MAX_BYTES {
183
- touch(&self.refused, key, MAX_REFUSED);
184
- return;
250
+ touch(&self.refused, stored.key, MAX_REFUSED);
251
+ self.refusals.fetch_add(1, Ordering::Relaxed);
252
+ return None;
185
253
  }
186
254
  let mut entries = lock(&self.entries);
187
- if entries.iter().any(|entry| entry.key == key) {
188
- return;
189
- }
190
- entries.push(Arc::new(Entry { key, data }));
191
- while entries.len() > MAX_ENTRIES
192
- || entries.iter().map(|entry| entry.data.len()).sum::<usize>() > MAX_BYTES
193
- {
194
- entries.remove(0);
255
+ if entries.iter().any(|entry| entry.key == stored.key) {
256
+ return None;
195
257
  }
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
+ };
263
+ entries.drain(..dropped);
264
+ entries.push(Arc::new(Entry {
265
+ key: stored.key.clone(),
266
+ data,
267
+ read: AtomicBool::new(false),
268
+ }));
269
+ self.publish(&entries);
196
270
  drop(entries);
197
271
  self.stored.fetch_add(1, Ordering::Relaxed);
272
+ Some(stored)
273
+ }
274
+
275
+ /// Puts `data`, built again by the context that stored the entry once a
276
+ /// call ran more of its script, in place of the entry, as the most
277
+ /// recently used. An entry gone since stays gone, and one the limits
278
+ /// leave no room for keeps what it held.
279
+ pub(crate) fn replace(&self, stored: &Stored, data: Box<[u8]>) {
280
+ if data.len() > MAX_BYTES {
281
+ self.refusals.fetch_add(1, Ordering::Relaxed);
282
+ return;
283
+ }
284
+ let mut entries = lock(&self.entries);
285
+ let Some(at) = entries.iter().position(|entry| entry.key == stored.key) else {
286
+ return;
287
+ };
288
+ let held = entries.remove(at);
289
+ let read = held.read.load(Ordering::Relaxed);
290
+ let Some(dropped) = room(&entries, data.len(), stored.first && !read) else {
291
+ entries.insert(at, held);
292
+ drop(entries);
293
+ self.refusals.fetch_add(1, Ordering::Relaxed);
294
+ return;
295
+ };
296
+ entries.drain(..dropped);
297
+ entries.push(Arc::new(Entry {
298
+ key: stored.key.clone(),
299
+ data,
300
+ read: AtomicBool::new(read),
301
+ }));
302
+ self.publish(&entries);
303
+ drop(entries);
304
+ drop(held);
305
+ self.rebuilt.fetch_add(1, Ordering::Relaxed);
198
306
  }
199
307
 
200
308
  /// Drops an entry V8 refused, so no later context tries it again.
201
309
  pub(crate) fn forget(&self, entry: &Arc<Entry>) {
202
- lock(&self.entries).retain(|kept| !Arc::ptr_eq(kept, entry));
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);
203
321
  }
204
322
 
205
323
  /// A forked child's cache: what its parent kept, had seen and refused,
206
324
  /// unless a thread of the parent held it at the fork, under the same key.
207
325
  pub(crate) fn inherited(&self) -> Self {
326
+ let entries = copied(&self.entries);
327
+ let (count, bytes) = totals(&entries);
208
328
  Self {
209
- entries: Mutex::new(copied(&self.entries)),
329
+ entries: Mutex::new(entries),
210
330
  sightings: Mutex::new(copied(&self.sightings)),
211
331
  building: Mutex::default(),
212
332
  refused: Mutex::new(copied(&self.refused)),
213
333
  hasher: self.hasher.clone(),
214
334
  hits: AtomicU64::new(0),
215
335
  stored: AtomicU64::new(0),
336
+ rebuilt: AtomicU64::new(0),
337
+ refusals: AtomicU64::new(0),
338
+ kept: AtomicU64::new(count),
339
+ kept_bytes: AtomicU64::new(bytes),
216
340
  }
217
341
  }
218
342
 
@@ -228,6 +352,32 @@ impl CodeCache {
228
352
  self.stored.load(Ordering::Relaxed)
229
353
  }
230
354
 
355
+ /// Kept caches this process built again after a call.
356
+ #[must_use]
357
+ pub fn rebuilt(&self) -> u64 {
358
+ self.rebuilt.load(Ordering::Relaxed)
359
+ }
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
+
231
381
  fn sighting(&self, filename: &JsText, source: &JsText, snapshot: Option<u64>) -> u64 {
232
382
  let mut hasher = self.hasher.build_hasher();
233
383
  filename.hash(&mut hasher);
@@ -251,6 +401,27 @@ impl CodeCache {
251
401
  }
252
402
  }
253
403
 
404
+ // How many of the least recently used entries go to make room for one of
405
+ // `bytes` more; `None` when a first sighting's would drop one a context has
406
+ // read.
407
+ fn room(entries: &[Arc<Entry>], bytes: usize, first: bool) -> Option<usize> {
408
+ let mut count = entries.len() + 1;
409
+ let mut total = entries.iter().map(|entry| entry.data.len()).sum::<usize>() + bytes;
410
+ let mut dropped = 0;
411
+ while count > MAX_ENTRIES || total > MAX_BYTES {
412
+ let Some(oldest) = entries.get(dropped) else {
413
+ break;
414
+ };
415
+ if first && oldest.read.load(Ordering::Relaxed) {
416
+ return None;
417
+ }
418
+ count -= 1;
419
+ total -= oldest.data.len();
420
+ dropped += 1;
421
+ }
422
+ Some(dropped)
423
+ }
424
+
254
425
  // Moves `item` to the back of `list`, adding it if absent and dropping the
255
426
  // front past `max`. Returns whether it was there.
256
427
  fn touch<T: PartialEq>(list: &Mutex<Vec<T>>, item: T, max: usize) -> bool {
@@ -266,6 +437,15 @@ fn touch<T: PartialEq>(list: &Mutex<Vec<T>>, item: T, max: usize) -> bool {
266
437
  found.is_some()
267
438
  }
268
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
+
269
449
  fn copied<T: Clone>(list: &Mutex<Vec<T>>) -> Vec<T> {
270
450
  list.try_lock().map(|list| list.clone()).unwrap_or_default()
271
451
  }
@@ -293,10 +473,31 @@ pub(crate) fn enabled() -> bool {
293
473
  })
294
474
  }
295
475
 
476
+ /// Whether a first sighting's script is worth a cache now, from how long it
477
+ /// took to compile and to run. `MINI_RACER_RS_CODE_CACHE_FIRST` set to
478
+ /// `always` or `never` (unstable, for tests) decides instead, read once per
479
+ /// process, first by `ContextHandle::spawn` as `enabled` is.
480
+ pub(crate) fn worth_first(compiled: Duration, ran: Duration) -> bool {
481
+ first_override().unwrap_or(ran >= compiled)
482
+ }
483
+
484
+ pub(crate) fn first_override() -> Option<bool> {
485
+ static DECIDED: OnceLock<Option<bool>> = OnceLock::new();
486
+ *DECIDED.get_or_init(
487
+ || match std::env::var_os("MINI_RACER_RS_CODE_CACHE_FIRST")?.to_str()? {
488
+ "always" => Some(true),
489
+ "never" => Some(false),
490
+ _ => None,
491
+ },
492
+ )
493
+ }
494
+
296
495
  #[cfg(test)]
297
496
  #[allow(clippy::panic, reason = "a test fails by panicking")]
298
497
  mod tests {
299
- use super::{CodeCache, MAX_SIGHTINGS, Plan};
498
+ use std::time::Duration;
499
+
500
+ use super::{CodeCache, FIRST_SIGHTING, MAX_ENTRIES, MAX_SIGHTINGS, Plan, Stored};
300
501
  use crate::control::JsText;
301
502
 
302
503
  fn cache() -> &'static CodeCache {
@@ -440,11 +641,14 @@ mod tests {
440
641
  ));
441
642
  }
442
643
 
644
+ // Its entries, and counts of its own work alone.
443
645
  #[test]
444
646
  fn a_child_starts_from_its_parents_entries() {
445
647
  let cache = cache();
446
648
  cached(cache, &source(4), Box::from(&b"compiled"[..]));
447
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));
448
652
  assert!(matches!(
449
653
  child.plan(&name(), &source(4), None, true),
450
654
  Plan::Consume(..)
@@ -458,12 +662,75 @@ mod tests {
458
662
  let held = cache.entries.lock();
459
663
  let child: &'static CodeCache = Box::leak(Box::new(cache.inherited()));
460
664
  drop(held);
665
+ assert_eq!((child.entries(), child.bytes()), (0, 0));
461
666
  assert!(matches!(
462
667
  child.plan(&name(), &source(5), None, true),
463
668
  Plan::Make(..)
464
669
  ));
465
670
  }
466
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
+
467
734
  #[test]
468
735
  fn a_cache_larger_than_the_limit_is_refused_and_not_rebuilt() {
469
736
  let cache = cache();
@@ -500,4 +767,232 @@ mod tests {
500
767
  Plan::Make(..)
501
768
  ));
502
769
  }
770
+
771
+ fn bundle(seed: u8, bytes: usize) -> JsText {
772
+ JsText::Latin1(vec![seed; bytes])
773
+ }
774
+
775
+ // Small enough to fill the list quickly; `plan` reads no size but the
776
+ // first sighting's.
777
+ fn tiny(seed: usize) -> JsText {
778
+ JsText::Latin1(seed.to_le_bytes().to_vec())
779
+ }
780
+
781
+ // Caches `text` at its first sighting.
782
+ fn first(cache: &'static CodeCache, text: &JsText, data: Box<[u8]>) {
783
+ let Plan::First(key) = cache.plan(&name(), text, None, true) else {
784
+ panic!("a first sighting worth a cache at once");
785
+ };
786
+ let Some(building) = cache.first(key) else {
787
+ panic!("an unclaimed first sighting");
788
+ };
789
+ cache.store(building, data);
790
+ }
791
+
792
+ fn consumed(cache: &'static CodeCache, text: &JsText) {
793
+ let Plan::Consume(entry, _) = cache.plan(&name(), text, None, true) else {
794
+ panic!("a stored cache is found");
795
+ };
796
+ cache.hit(&entry);
797
+ }
798
+
799
+ #[test]
800
+ fn only_a_shared_first_sighting_of_16_mib_or_more_may_be_cached_at_once() {
801
+ let cache = cache();
802
+ assert!(matches!(
803
+ cache.plan(&name(), &bundle(1, FIRST_SIGHTING), None, true),
804
+ Plan::First(_)
805
+ ));
806
+ assert!(matches!(
807
+ cache.plan(&name(), &bundle(2, FIRST_SIGHTING), None, false),
808
+ Plan::Compile(None)
809
+ ));
810
+ assert!(matches!(
811
+ cache.plan(&name(), &bundle(3, FIRST_SIGHTING - 1), None, true),
812
+ Plan::Compile(Some(_))
813
+ ));
814
+ let units = vec![0x61; FIRST_SIGHTING / 2];
815
+ assert!(matches!(
816
+ cache.plan(&name(), &JsText::Utf16(units), None, true),
817
+ Plan::First(_)
818
+ ));
819
+ // Seen again, it is cached as any other script.
820
+ assert!(matches!(
821
+ cache.plan(&name(), &bundle(1, FIRST_SIGHTING), None, true),
822
+ Plan::Make(_)
823
+ ));
824
+ }
825
+
826
+ #[test]
827
+ fn a_first_sighting_is_cached_once_it_has_run_for_as_long_as_it_compiled() {
828
+ let millis = Duration::from_millis;
829
+ assert!(super::worth_first(millis(186), millis(690)));
830
+ assert!(super::worth_first(millis(100), millis(100)));
831
+ assert!(!super::worth_first(millis(312), millis(55)));
832
+ let cache = cache();
833
+ let text = bundle(4, FIRST_SIGHTING);
834
+ first(cache, &text, Box::from(&b"compiled"[..]));
835
+ assert_eq!(cache.stored(), 1);
836
+ consumed(cache, &text);
837
+ assert_eq!(cache.hits(), 1);
838
+ }
839
+
840
+ #[test]
841
+ fn a_first_sighting_cached_or_claimed_meanwhile_is_not_built_again() {
842
+ let cache = cache();
843
+ let text = bundle(5, FIRST_SIGHTING);
844
+ let Plan::First(key) = cache.plan(&name(), &text, None, true) else {
845
+ panic!("a first sighting worth a cache at once");
846
+ };
847
+ let Plan::Make(building) = cache.plan(&name(), &text, None, true) else {
848
+ panic!("a second sighting makes a cache");
849
+ };
850
+ assert!(cache.first(key.clone()).is_none());
851
+ cache.store(building, Box::from(&b"compiled"[..]));
852
+ assert!(cache.first(key).is_none());
853
+ assert_eq!(cache.stored(), 1);
854
+ }
855
+
856
+ // A full list with a read entry the least recently used: a first
857
+ // sighting's cache would drop it, so it is not kept, and its script is
858
+ // cached at its second sighting as any other.
859
+ #[test]
860
+ fn a_first_sighting_never_drops_an_entry_a_context_has_read() {
861
+ let cache = cache();
862
+ for seed in 0..MAX_ENTRIES {
863
+ cached(cache, &tiny(seed), Box::from(&b"compiled"[..]));
864
+ }
865
+ consumed(cache, &tiny(0));
866
+ // Now the least recently used, and unread: the first sighting takes
867
+ // its place.
868
+ let roomy = bundle(6, FIRST_SIGHTING);
869
+ first(cache, &roomy, Box::from(&b"compiled"[..]));
870
+ assert_eq!(cache.stored(), 65);
871
+ assert!(matches!(
872
+ cache.plan(&name(), &tiny(1), None, true),
873
+ Plan::Make(_)
874
+ ));
875
+ // Read in turn, every entry is read, the oldest last.
876
+ for seed in 2..MAX_ENTRIES {
877
+ consumed(cache, &tiny(seed));
878
+ }
879
+ consumed(cache, &tiny(0));
880
+ consumed(cache, &roomy);
881
+ let crowded = bundle(7, FIRST_SIGHTING);
882
+ assert_eq!(cache.refusals(), 0);
883
+ first(cache, &crowded, Box::from(&b"compiled"[..]));
884
+ assert_eq!((cache.stored(), cache.refusals()), (65, 1));
885
+ consumed(cache, &tiny(2));
886
+ let Plan::Make(building) = cache.plan(&name(), &crowded, None, true) else {
887
+ panic!("a second sighting makes a cache");
888
+ };
889
+ cache.store(building, Box::from(&b"compiled"[..]));
890
+ assert_eq!(cache.stored(), 66);
891
+ assert!(matches!(
892
+ cache.plan(&name(), &tiny(3), None, true),
893
+ Plan::Make(_)
894
+ ));
895
+ }
896
+
897
+ #[test]
898
+ fn a_first_sighting_past_the_size_limit_drops_only_unread_entries() {
899
+ let cache = cache();
900
+ let half = || vec![0; super::MAX_BYTES / 2 + 1].into_boxed_slice();
901
+ cached(cache, &tiny(1), half());
902
+ consumed(cache, &tiny(1));
903
+ first(cache, &bundle(8, FIRST_SIGHTING), half());
904
+ assert_eq!((cache.stored(), cache.refusals()), (1, 1));
905
+ cached(cache, &tiny(2), half());
906
+ assert_eq!(cache.stored(), 2);
907
+ first(cache, &bundle(9, FIRST_SIGHTING), half());
908
+ assert_eq!(cache.stored(), 3);
909
+ assert!(matches!(
910
+ cache.plan(&name(), &tiny(2), None, true),
911
+ Plan::Make(_)
912
+ ));
913
+ }
914
+
915
+ fn kept(cache: &'static CodeCache, text: &JsText, data: Box<[u8]>) -> Stored {
916
+ let _ = cache.plan(&name(), text, None, true);
917
+ let Plan::Make(building) = cache.plan(&name(), text, None, true) else {
918
+ panic!("a second sighting makes a cache");
919
+ };
920
+ cache
921
+ .store(building, data)
922
+ .unwrap_or_else(|| panic!("a cache with room is kept"))
923
+ }
924
+
925
+ fn data_of(cache: &'static CodeCache, text: &JsText) -> Vec<u8> {
926
+ let Plan::Consume(entry, _) = cache.plan(&name(), text, None, true) else {
927
+ panic!("a stored cache is found");
928
+ };
929
+ entry.data().to_vec()
930
+ }
931
+
932
+ // Built again, an entry holds the new cache and becomes the most recently
933
+ // used, read or not as it was.
934
+ #[test]
935
+ fn a_cache_built_again_takes_the_entrys_place() {
936
+ let cache = cache();
937
+ let stored = kept(cache, &tiny(0), Box::from(&b"loaded"[..]));
938
+ for seed in 1..MAX_ENTRIES {
939
+ cached(cache, &tiny(seed), Box::from(&b"compiled"[..]));
940
+ }
941
+ cache.replace(&stored, Box::from(&b"called"[..]));
942
+ assert_eq!(cache.rebuilt(), 1);
943
+ cached(cache, &tiny(MAX_ENTRIES), Box::from(&b"compiled"[..]));
944
+ assert_eq!(data_of(cache, &tiny(0)), b"called");
945
+ assert!(matches!(
946
+ cache.plan(&name(), &tiny(1), None, true),
947
+ Plan::Make(_)
948
+ ));
949
+ }
950
+
951
+ #[test]
952
+ fn an_entry_gone_or_too_large_built_again_is_left_as_it_was() {
953
+ let cache = cache();
954
+ let gone = kept(cache, &tiny(0), Box::from(&b"loaded"[..]));
955
+ let Plan::Consume(entry, _) = cache.plan(&name(), &tiny(0), None, true) else {
956
+ panic!("a stored cache is found");
957
+ };
958
+ cache.forget(&entry);
959
+ cache.replace(&gone, Box::from(&b"called"[..]));
960
+ assert!(matches!(
961
+ cache.plan(&name(), &tiny(0), None, true),
962
+ Plan::Make(_)
963
+ ));
964
+ let large = kept(cache, &tiny(1), Box::from(&b"loaded"[..]));
965
+ cache.replace(&large, vec![0; super::MAX_BYTES + 1].into_boxed_slice());
966
+ assert_eq!(data_of(cache, &tiny(1)), b"loaded");
967
+ assert_eq!(cache.rebuilt(), 0);
968
+ }
969
+
970
+ // A first sighting's entry no context has read yet still makes room only
971
+ // from unread entries when built again.
972
+ #[test]
973
+ fn a_first_sighting_built_again_never_drops_an_entry_a_context_has_read() {
974
+ let cache = cache();
975
+ let half = || vec![0; super::MAX_BYTES / 2 + 1].into_boxed_slice();
976
+ cached(cache, &tiny(1), half());
977
+ consumed(cache, &tiny(1));
978
+ let text = bundle(1, FIRST_SIGHTING);
979
+ let Plan::First(key) = cache.plan(&name(), &text, None, true) else {
980
+ panic!("a first sighting worth a cache at once");
981
+ };
982
+ let building = cache.first(key).unwrap_or_else(|| panic!("unclaimed"));
983
+ let stored = cache
984
+ .store(building, Box::from(&b"loaded"[..]))
985
+ .unwrap_or_else(|| panic!("a cache with room is kept"));
986
+ cache.replace(&stored, half());
987
+ assert_eq!((cache.rebuilt(), cache.refusals()), (0, 1));
988
+ assert_eq!(data_of(cache, &text), b"loaded");
989
+ // Read now, it is built again as any other entry.
990
+ consumed(cache, &text);
991
+ cache.replace(&stored, half());
992
+ assert_eq!(cache.rebuilt(), 1);
993
+ assert!(matches!(
994
+ cache.plan(&name(), &tiny(1), None, true),
995
+ Plan::Make(_)
996
+ ));
997
+ }
503
998
  }