mini_racer_rs 1.5.1 → 1.5.3
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/.yardopts +7 -0
- data/CHANGELOG.md +23 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +26 -26
- data/crates/mini_racer_engine/src/context.rs +1 -0
- data/crates/mini_racer_engine/src/process/code_cache.rs +438 -25
- data/crates/mini_racer_engine/src/process/sources.rs +2 -10
- data/crates/mini_racer_engine/src/runner.rs +90 -14
- data/crates/mini_racer_engine/src/sys.rs +42 -0
- data/crates/mini_racer_engine/src/text.rs +182 -18
- data/crates/mini_racer_sync/src/channel.rs +135 -0
- data/crates/mini_racer_sync/src/fork.rs +22 -0
- data/crates/mini_racer_sync/src/termination.rs +27 -0
- data/ext/mini_racer_rs/src/gc_root.rs +8 -3
- data/ext/mini_racer_rs/src/host.rs +57 -30
- data/ext/mini_racer_rs/src/platform.rs +8 -3
- data/ext/mini_racer_rs/src/pool.rs +11 -7
- data/ext/mini_racer_rs/src/timeout.rs +14 -0
- data/lib/mini_racer_rs/version.rb +1 -1
- metadata +7 -2
|
@@ -1,29 +1,49 @@
|
|
|
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
|
-
|
|
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
|
|
18
|
+
// evaluated once is still seen a second time: a list of 64 forgot one after 70
|
|
19
|
+
// such scripts, so only its fourth load read a cache. Each large eval scans
|
|
20
|
+
// it, in about 2 microseconds when full.
|
|
21
|
+
const MAX_SIGHTINGS: usize = 4096;
|
|
13
22
|
const MAX_REFUSED: usize = 8;
|
|
14
23
|
// Units read from each end of a source, and a smaller window from each of a
|
|
15
24
|
// few points between, to tell one sighting from another without reading it all.
|
|
16
25
|
const SAMPLE: usize = 4096;
|
|
17
26
|
const SAMPLE_POINTS: usize = 8;
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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.
|
|
22
38
|
///
|
|
23
39
|
/// A script is cached the second time the process sees it. The first sighting
|
|
24
40
|
/// costs a sampled digest and no cache, as a large script evaluated once
|
|
25
41
|
/// (per-request data, say) gains nothing from one; it costs the full digest
|
|
26
|
-
/// 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`).
|
|
27
47
|
#[derive(Default)]
|
|
28
48
|
pub struct CodeCache {
|
|
29
49
|
// Most recently used last.
|
|
@@ -39,6 +59,7 @@ pub struct CodeCache {
|
|
|
39
59
|
hasher: RandomState,
|
|
40
60
|
hits: AtomicU64,
|
|
41
61
|
stored: AtomicU64,
|
|
62
|
+
rebuilt: AtomicU64,
|
|
42
63
|
}
|
|
43
64
|
|
|
44
65
|
/// What a cached script is found by: its file name, the startup snapshot its
|
|
@@ -56,6 +77,8 @@ pub(crate) struct Key {
|
|
|
56
77
|
pub(crate) struct Entry {
|
|
57
78
|
key: Key,
|
|
58
79
|
data: Box<[u8]>,
|
|
80
|
+
// A context compiled from it.
|
|
81
|
+
read: AtomicBool,
|
|
59
82
|
}
|
|
60
83
|
|
|
61
84
|
impl Entry {
|
|
@@ -64,10 +87,20 @@ impl Entry {
|
|
|
64
87
|
}
|
|
65
88
|
}
|
|
66
89
|
|
|
90
|
+
/// A kept entry, as the context that built it builds it again once a call
|
|
91
|
+
/// has compiled more of the script.
|
|
92
|
+
pub(crate) struct Stored {
|
|
93
|
+
key: Key,
|
|
94
|
+
first: bool,
|
|
95
|
+
}
|
|
96
|
+
|
|
67
97
|
/// A key being built; dropped unbuilt, it lets another context build it.
|
|
68
98
|
pub(crate) struct Building {
|
|
69
99
|
cache: &'static CodeCache,
|
|
70
100
|
key: Key,
|
|
101
|
+
// At the script's first sighting, so it makes room only from entries no
|
|
102
|
+
// context has read.
|
|
103
|
+
first: bool,
|
|
71
104
|
}
|
|
72
105
|
|
|
73
106
|
impl Drop for Building {
|
|
@@ -82,6 +115,9 @@ pub(crate) enum Plan {
|
|
|
82
115
|
/// first sighting whose source is shared, a key being built elsewhere or
|
|
83
116
|
/// a refused cache.
|
|
84
117
|
Compile(Option<Key>),
|
|
118
|
+
/// From source, a first sighting of 16 MiB or more whose source is
|
|
119
|
+
/// shared, cached once it has run if `worth_first` says so (`CodeCache::first`).
|
|
120
|
+
First(Key),
|
|
85
121
|
/// From source, then a cache made once the dispatch is over.
|
|
86
122
|
Make(Building),
|
|
87
123
|
/// From the entry, falling back to source if V8 refuses it.
|
|
@@ -94,7 +130,7 @@ impl Plan {
|
|
|
94
130
|
match self {
|
|
95
131
|
Self::Compile(key) => key.as_ref(),
|
|
96
132
|
Self::Make(building) => Some(&building.key),
|
|
97
|
-
Self::Consume(_, key) => Some(key),
|
|
133
|
+
Self::First(key) | Self::Consume(_, key) => Some(key),
|
|
98
134
|
}
|
|
99
135
|
}
|
|
100
136
|
}
|
|
@@ -129,6 +165,9 @@ impl CodeCache {
|
|
|
129
165
|
digest: self.hasher.hash_one(source),
|
|
130
166
|
sighting,
|
|
131
167
|
};
|
|
168
|
+
if first && source.byte_len() >= FIRST_SIGHTING {
|
|
169
|
+
return Plan::First(key);
|
|
170
|
+
}
|
|
132
171
|
if first {
|
|
133
172
|
return Plan::Compile(Some(key));
|
|
134
173
|
}
|
|
@@ -149,13 +188,32 @@ impl CodeCache {
|
|
|
149
188
|
|
|
150
189
|
/// Claims `key` for this context to build, unless another is building it.
|
|
151
190
|
pub(crate) fn begin(&'static self, key: Key) -> Option<Building> {
|
|
191
|
+
self.claim(key, false)
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/// Claims a first sighting's `key` for this context to build once its
|
|
195
|
+
/// script has run, unless it was cached, refused or claimed meanwhile.
|
|
196
|
+
pub(crate) fn first(&'static self, key: Key) -> Option<Building> {
|
|
197
|
+
if lock(&self.entries).iter().any(|entry| entry.key == key)
|
|
198
|
+
|| lock(&self.refused).contains(&key)
|
|
199
|
+
{
|
|
200
|
+
return None;
|
|
201
|
+
}
|
|
202
|
+
self.claim(key, true)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
fn claim(&'static self, key: Key, first: bool) -> Option<Building> {
|
|
152
206
|
let mut building = lock(&self.building);
|
|
153
207
|
if building.contains(&key) {
|
|
154
208
|
return None;
|
|
155
209
|
}
|
|
156
210
|
building.push(key.clone());
|
|
157
211
|
drop(building);
|
|
158
|
-
Some(Building {
|
|
212
|
+
Some(Building {
|
|
213
|
+
cache: self,
|
|
214
|
+
key,
|
|
215
|
+
first,
|
|
216
|
+
})
|
|
159
217
|
}
|
|
160
218
|
|
|
161
219
|
/// A startup snapshot's identity for `Key`, from a sample of the blob:
|
|
@@ -166,31 +224,70 @@ impl CodeCache {
|
|
|
166
224
|
hasher.finish()
|
|
167
225
|
}
|
|
168
226
|
|
|
169
|
-
pub(crate) fn hit(&self) {
|
|
227
|
+
pub(crate) fn hit(&self, entry: &Entry) {
|
|
228
|
+
entry.read.store(true, Ordering::Relaxed);
|
|
170
229
|
self.hits.fetch_add(1, Ordering::Relaxed);
|
|
171
230
|
}
|
|
172
231
|
|
|
173
232
|
/// Keeps `data` for the key built, dropping the least recently used
|
|
174
|
-
/// entries past the limits; a cache larger than all of them is refused.
|
|
175
|
-
|
|
176
|
-
|
|
233
|
+
/// entries past the limits; a cache larger than all of them is refused. A
|
|
234
|
+
/// first sighting's cache that would drop an entry a context has read is
|
|
235
|
+
/// not kept, and the script is cached at its next sighting instead.
|
|
236
|
+
/// A kept entry comes back as what its builder hands `replace` once a
|
|
237
|
+
/// call has run.
|
|
238
|
+
pub(crate) fn store(&self, building: Building, data: Box<[u8]>) -> Option<Stored> {
|
|
239
|
+
let stored = Stored {
|
|
240
|
+
key: building.key.clone(),
|
|
241
|
+
first: building.first,
|
|
242
|
+
};
|
|
177
243
|
drop(building);
|
|
178
244
|
if data.len() > MAX_BYTES {
|
|
179
|
-
touch(&self.refused, key, MAX_REFUSED);
|
|
180
|
-
return;
|
|
245
|
+
touch(&self.refused, stored.key, MAX_REFUSED);
|
|
246
|
+
return None;
|
|
181
247
|
}
|
|
182
248
|
let mut entries = lock(&self.entries);
|
|
183
|
-
if entries.iter().any(|entry| entry.key == key) {
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
entries.push(Arc::new(Entry { key, data }));
|
|
187
|
-
while entries.len() > MAX_ENTRIES
|
|
188
|
-
|| entries.iter().map(|entry| entry.data.len()).sum::<usize>() > MAX_BYTES
|
|
189
|
-
{
|
|
190
|
-
entries.remove(0);
|
|
249
|
+
if entries.iter().any(|entry| entry.key == stored.key) {
|
|
250
|
+
return None;
|
|
191
251
|
}
|
|
252
|
+
let dropped = room(&entries, data.len(), stored.first)?;
|
|
253
|
+
entries.drain(..dropped);
|
|
254
|
+
entries.push(Arc::new(Entry {
|
|
255
|
+
key: stored.key.clone(),
|
|
256
|
+
data,
|
|
257
|
+
read: AtomicBool::new(false),
|
|
258
|
+
}));
|
|
192
259
|
drop(entries);
|
|
193
260
|
self.stored.fetch_add(1, Ordering::Relaxed);
|
|
261
|
+
Some(stored)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/// Puts `data`, built again by the context that stored the entry once a
|
|
265
|
+
/// call ran more of its script, in place of the entry, as the most
|
|
266
|
+
/// recently used. An entry gone since stays gone, and one the limits
|
|
267
|
+
/// leave no room for keeps what it held.
|
|
268
|
+
pub(crate) fn replace(&self, stored: &Stored, data: Box<[u8]>) {
|
|
269
|
+
if data.len() > MAX_BYTES {
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
let mut entries = lock(&self.entries);
|
|
273
|
+
let Some(at) = entries.iter().position(|entry| entry.key == stored.key) else {
|
|
274
|
+
return;
|
|
275
|
+
};
|
|
276
|
+
let held = entries.remove(at);
|
|
277
|
+
let read = held.read.load(Ordering::Relaxed);
|
|
278
|
+
let Some(dropped) = room(&entries, data.len(), stored.first && !read) else {
|
|
279
|
+
entries.insert(at, held);
|
|
280
|
+
return;
|
|
281
|
+
};
|
|
282
|
+
entries.drain(..dropped);
|
|
283
|
+
entries.push(Arc::new(Entry {
|
|
284
|
+
key: stored.key.clone(),
|
|
285
|
+
data,
|
|
286
|
+
read: AtomicBool::new(read),
|
|
287
|
+
}));
|
|
288
|
+
drop(entries);
|
|
289
|
+
drop(held);
|
|
290
|
+
self.rebuilt.fetch_add(1, Ordering::Relaxed);
|
|
194
291
|
}
|
|
195
292
|
|
|
196
293
|
/// Drops an entry V8 refused, so no later context tries it again.
|
|
@@ -209,6 +306,7 @@ impl CodeCache {
|
|
|
209
306
|
hasher: self.hasher.clone(),
|
|
210
307
|
hits: AtomicU64::new(0),
|
|
211
308
|
stored: AtomicU64::new(0),
|
|
309
|
+
rebuilt: AtomicU64::new(0),
|
|
212
310
|
}
|
|
213
311
|
}
|
|
214
312
|
|
|
@@ -224,6 +322,12 @@ impl CodeCache {
|
|
|
224
322
|
self.stored.load(Ordering::Relaxed)
|
|
225
323
|
}
|
|
226
324
|
|
|
325
|
+
/// Kept caches this process built again after a call.
|
|
326
|
+
#[must_use]
|
|
327
|
+
pub fn rebuilt(&self) -> u64 {
|
|
328
|
+
self.rebuilt.load(Ordering::Relaxed)
|
|
329
|
+
}
|
|
330
|
+
|
|
227
331
|
fn sighting(&self, filename: &JsText, source: &JsText, snapshot: Option<u64>) -> u64 {
|
|
228
332
|
let mut hasher = self.hasher.build_hasher();
|
|
229
333
|
filename.hash(&mut hasher);
|
|
@@ -247,6 +351,27 @@ impl CodeCache {
|
|
|
247
351
|
}
|
|
248
352
|
}
|
|
249
353
|
|
|
354
|
+
// How many of the least recently used entries go to make room for one of
|
|
355
|
+
// `bytes` more; `None` when a first sighting's would drop one a context has
|
|
356
|
+
// read.
|
|
357
|
+
fn room(entries: &[Arc<Entry>], bytes: usize, first: bool) -> Option<usize> {
|
|
358
|
+
let mut count = entries.len() + 1;
|
|
359
|
+
let mut total = entries.iter().map(|entry| entry.data.len()).sum::<usize>() + bytes;
|
|
360
|
+
let mut dropped = 0;
|
|
361
|
+
while count > MAX_ENTRIES || total > MAX_BYTES {
|
|
362
|
+
let Some(oldest) = entries.get(dropped) else {
|
|
363
|
+
break;
|
|
364
|
+
};
|
|
365
|
+
if first && oldest.read.load(Ordering::Relaxed) {
|
|
366
|
+
return None;
|
|
367
|
+
}
|
|
368
|
+
count -= 1;
|
|
369
|
+
total -= oldest.data.len();
|
|
370
|
+
dropped += 1;
|
|
371
|
+
}
|
|
372
|
+
Some(dropped)
|
|
373
|
+
}
|
|
374
|
+
|
|
250
375
|
// Moves `item` to the back of `list`, adding it if absent and dropping the
|
|
251
376
|
// front past `max`. Returns whether it was there.
|
|
252
377
|
fn touch<T: PartialEq>(list: &Mutex<Vec<T>>, item: T, max: usize) -> bool {
|
|
@@ -289,10 +414,31 @@ pub(crate) fn enabled() -> bool {
|
|
|
289
414
|
})
|
|
290
415
|
}
|
|
291
416
|
|
|
417
|
+
/// Whether a first sighting's script is worth a cache now, from how long it
|
|
418
|
+
/// took to compile and to run. `MINI_RACER_RS_CODE_CACHE_FIRST` set to
|
|
419
|
+
/// `always` or `never` (unstable, for tests) decides instead, read once per
|
|
420
|
+
/// process, first by `ContextHandle::spawn` as `enabled` is.
|
|
421
|
+
pub(crate) fn worth_first(compiled: Duration, ran: Duration) -> bool {
|
|
422
|
+
first_override().unwrap_or(ran >= compiled)
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
pub(crate) fn first_override() -> Option<bool> {
|
|
426
|
+
static DECIDED: OnceLock<Option<bool>> = OnceLock::new();
|
|
427
|
+
*DECIDED.get_or_init(
|
|
428
|
+
|| match std::env::var_os("MINI_RACER_RS_CODE_CACHE_FIRST")?.to_str()? {
|
|
429
|
+
"always" => Some(true),
|
|
430
|
+
"never" => Some(false),
|
|
431
|
+
_ => None,
|
|
432
|
+
},
|
|
433
|
+
)
|
|
434
|
+
}
|
|
435
|
+
|
|
292
436
|
#[cfg(test)]
|
|
293
437
|
#[allow(clippy::panic, reason = "a test fails by panicking")]
|
|
294
438
|
mod tests {
|
|
295
|
-
use
|
|
439
|
+
use std::time::Duration;
|
|
440
|
+
|
|
441
|
+
use super::{CodeCache, FIRST_SIGHTING, MAX_ENTRIES, MAX_SIGHTINGS, Plan, Stored};
|
|
296
442
|
use crate::control::JsText;
|
|
297
443
|
|
|
298
444
|
fn cache() -> &'static CodeCache {
|
|
@@ -397,6 +543,29 @@ mod tests {
|
|
|
397
543
|
));
|
|
398
544
|
}
|
|
399
545
|
|
|
546
|
+
// The list holds as many sightings as it keeps and no more: a script is
|
|
547
|
+
// seen again after one fewer others, and forgotten after that many more.
|
|
548
|
+
#[test]
|
|
549
|
+
fn a_sighting_is_forgotten_once_the_list_has_had_its_fill_of_others() {
|
|
550
|
+
let cache = cache();
|
|
551
|
+
let script = |seed: usize| JsText::Latin1(seed.to_le_bytes().to_vec());
|
|
552
|
+
for seed in 0..MAX_SIGHTINGS {
|
|
553
|
+
let _ = cache.plan(&name(), &script(seed), None, false);
|
|
554
|
+
}
|
|
555
|
+
assert!(matches!(
|
|
556
|
+
cache.plan(&name(), &script(0), None, false),
|
|
557
|
+
Plan::Make(..)
|
|
558
|
+
));
|
|
559
|
+
for seed in MAX_SIGHTINGS..2 * MAX_SIGHTINGS {
|
|
560
|
+
let _ = cache.plan(&name(), &script(seed), None, false);
|
|
561
|
+
}
|
|
562
|
+
assert_eq!(mini_racer_sync::lock(&cache.sightings).len(), MAX_SIGHTINGS);
|
|
563
|
+
assert!(matches!(
|
|
564
|
+
cache.plan(&name(), &script(0), None, false),
|
|
565
|
+
Plan::Compile(None)
|
|
566
|
+
));
|
|
567
|
+
}
|
|
568
|
+
|
|
400
569
|
#[test]
|
|
401
570
|
fn a_script_being_built_is_built_once() {
|
|
402
571
|
let cache = cache();
|
|
@@ -456,4 +625,248 @@ mod tests {
|
|
|
456
625
|
Plan::Consume(..)
|
|
457
626
|
));
|
|
458
627
|
}
|
|
628
|
+
|
|
629
|
+
#[test]
|
|
630
|
+
fn caches_past_the_size_limit_drop_the_least_recently_used() {
|
|
631
|
+
let cache = cache();
|
|
632
|
+
let half = || vec![0; super::MAX_BYTES / 2 + 1].into_boxed_slice();
|
|
633
|
+
cached(cache, &source(10), half());
|
|
634
|
+
cached(cache, &source(11), half());
|
|
635
|
+
assert_eq!(cache.stored(), 2);
|
|
636
|
+
assert!(matches!(
|
|
637
|
+
cache.plan(&name(), &source(11), None, true),
|
|
638
|
+
Plan::Consume(..)
|
|
639
|
+
));
|
|
640
|
+
assert!(matches!(
|
|
641
|
+
cache.plan(&name(), &source(10), None, true),
|
|
642
|
+
Plan::Make(..)
|
|
643
|
+
));
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
fn bundle(seed: u8, bytes: usize) -> JsText {
|
|
647
|
+
JsText::Latin1(vec![seed; bytes])
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
// Small enough to fill the list quickly; `plan` reads no size but the
|
|
651
|
+
// first sighting's.
|
|
652
|
+
fn tiny(seed: usize) -> JsText {
|
|
653
|
+
JsText::Latin1(seed.to_le_bytes().to_vec())
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// Caches `text` at its first sighting.
|
|
657
|
+
fn first(cache: &'static CodeCache, text: &JsText, data: Box<[u8]>) {
|
|
658
|
+
let Plan::First(key) = cache.plan(&name(), text, None, true) else {
|
|
659
|
+
panic!("a first sighting worth a cache at once");
|
|
660
|
+
};
|
|
661
|
+
let Some(building) = cache.first(key) else {
|
|
662
|
+
panic!("an unclaimed first sighting");
|
|
663
|
+
};
|
|
664
|
+
cache.store(building, data);
|
|
665
|
+
}
|
|
666
|
+
|
|
667
|
+
fn consumed(cache: &'static CodeCache, text: &JsText) {
|
|
668
|
+
let Plan::Consume(entry, _) = cache.plan(&name(), text, None, true) else {
|
|
669
|
+
panic!("a stored cache is found");
|
|
670
|
+
};
|
|
671
|
+
cache.hit(&entry);
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
#[test]
|
|
675
|
+
fn only_a_shared_first_sighting_of_16_mib_or_more_may_be_cached_at_once() {
|
|
676
|
+
let cache = cache();
|
|
677
|
+
assert!(matches!(
|
|
678
|
+
cache.plan(&name(), &bundle(1, FIRST_SIGHTING), None, true),
|
|
679
|
+
Plan::First(_)
|
|
680
|
+
));
|
|
681
|
+
assert!(matches!(
|
|
682
|
+
cache.plan(&name(), &bundle(2, FIRST_SIGHTING), None, false),
|
|
683
|
+
Plan::Compile(None)
|
|
684
|
+
));
|
|
685
|
+
assert!(matches!(
|
|
686
|
+
cache.plan(&name(), &bundle(3, FIRST_SIGHTING - 1), None, true),
|
|
687
|
+
Plan::Compile(Some(_))
|
|
688
|
+
));
|
|
689
|
+
let units = vec![0x61; FIRST_SIGHTING / 2];
|
|
690
|
+
assert!(matches!(
|
|
691
|
+
cache.plan(&name(), &JsText::Utf16(units), None, true),
|
|
692
|
+
Plan::First(_)
|
|
693
|
+
));
|
|
694
|
+
// Seen again, it is cached as any other script.
|
|
695
|
+
assert!(matches!(
|
|
696
|
+
cache.plan(&name(), &bundle(1, FIRST_SIGHTING), None, true),
|
|
697
|
+
Plan::Make(_)
|
|
698
|
+
));
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
#[test]
|
|
702
|
+
fn a_first_sighting_is_cached_once_it_has_run_for_as_long_as_it_compiled() {
|
|
703
|
+
let millis = Duration::from_millis;
|
|
704
|
+
assert!(super::worth_first(millis(186), millis(690)));
|
|
705
|
+
assert!(super::worth_first(millis(100), millis(100)));
|
|
706
|
+
assert!(!super::worth_first(millis(312), millis(55)));
|
|
707
|
+
let cache = cache();
|
|
708
|
+
let text = bundle(4, FIRST_SIGHTING);
|
|
709
|
+
first(cache, &text, Box::from(&b"compiled"[..]));
|
|
710
|
+
assert_eq!(cache.stored(), 1);
|
|
711
|
+
consumed(cache, &text);
|
|
712
|
+
assert_eq!(cache.hits(), 1);
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
#[test]
|
|
716
|
+
fn a_first_sighting_cached_or_claimed_meanwhile_is_not_built_again() {
|
|
717
|
+
let cache = cache();
|
|
718
|
+
let text = bundle(5, FIRST_SIGHTING);
|
|
719
|
+
let Plan::First(key) = cache.plan(&name(), &text, None, true) else {
|
|
720
|
+
panic!("a first sighting worth a cache at once");
|
|
721
|
+
};
|
|
722
|
+
let Plan::Make(building) = cache.plan(&name(), &text, None, true) else {
|
|
723
|
+
panic!("a second sighting makes a cache");
|
|
724
|
+
};
|
|
725
|
+
assert!(cache.first(key.clone()).is_none());
|
|
726
|
+
cache.store(building, Box::from(&b"compiled"[..]));
|
|
727
|
+
assert!(cache.first(key).is_none());
|
|
728
|
+
assert_eq!(cache.stored(), 1);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// A full list with a read entry the least recently used: a first
|
|
732
|
+
// sighting's cache would drop it, so it is not kept, and its script is
|
|
733
|
+
// cached at its second sighting as any other.
|
|
734
|
+
#[test]
|
|
735
|
+
fn a_first_sighting_never_drops_an_entry_a_context_has_read() {
|
|
736
|
+
let cache = cache();
|
|
737
|
+
for seed in 0..MAX_ENTRIES {
|
|
738
|
+
cached(cache, &tiny(seed), Box::from(&b"compiled"[..]));
|
|
739
|
+
}
|
|
740
|
+
consumed(cache, &tiny(0));
|
|
741
|
+
// Now the least recently used, and unread: the first sighting takes
|
|
742
|
+
// its place.
|
|
743
|
+
let roomy = bundle(6, FIRST_SIGHTING);
|
|
744
|
+
first(cache, &roomy, Box::from(&b"compiled"[..]));
|
|
745
|
+
assert_eq!(cache.stored(), 65);
|
|
746
|
+
assert!(matches!(
|
|
747
|
+
cache.plan(&name(), &tiny(1), None, true),
|
|
748
|
+
Plan::Make(_)
|
|
749
|
+
));
|
|
750
|
+
// Read in turn, every entry is read, the oldest last.
|
|
751
|
+
for seed in 2..MAX_ENTRIES {
|
|
752
|
+
consumed(cache, &tiny(seed));
|
|
753
|
+
}
|
|
754
|
+
consumed(cache, &tiny(0));
|
|
755
|
+
consumed(cache, &roomy);
|
|
756
|
+
let crowded = bundle(7, FIRST_SIGHTING);
|
|
757
|
+
first(cache, &crowded, Box::from(&b"compiled"[..]));
|
|
758
|
+
assert_eq!(cache.stored(), 65);
|
|
759
|
+
consumed(cache, &tiny(2));
|
|
760
|
+
let Plan::Make(building) = cache.plan(&name(), &crowded, None, true) else {
|
|
761
|
+
panic!("a second sighting makes a cache");
|
|
762
|
+
};
|
|
763
|
+
cache.store(building, Box::from(&b"compiled"[..]));
|
|
764
|
+
assert_eq!(cache.stored(), 66);
|
|
765
|
+
assert!(matches!(
|
|
766
|
+
cache.plan(&name(), &tiny(3), None, true),
|
|
767
|
+
Plan::Make(_)
|
|
768
|
+
));
|
|
769
|
+
}
|
|
770
|
+
|
|
771
|
+
#[test]
|
|
772
|
+
fn a_first_sighting_past_the_size_limit_drops_only_unread_entries() {
|
|
773
|
+
let cache = cache();
|
|
774
|
+
let half = || vec![0; super::MAX_BYTES / 2 + 1].into_boxed_slice();
|
|
775
|
+
cached(cache, &tiny(1), half());
|
|
776
|
+
consumed(cache, &tiny(1));
|
|
777
|
+
first(cache, &bundle(8, FIRST_SIGHTING), half());
|
|
778
|
+
assert_eq!(cache.stored(), 1);
|
|
779
|
+
cached(cache, &tiny(2), half());
|
|
780
|
+
assert_eq!(cache.stored(), 2);
|
|
781
|
+
first(cache, &bundle(9, FIRST_SIGHTING), half());
|
|
782
|
+
assert_eq!(cache.stored(), 3);
|
|
783
|
+
assert!(matches!(
|
|
784
|
+
cache.plan(&name(), &tiny(2), None, true),
|
|
785
|
+
Plan::Make(_)
|
|
786
|
+
));
|
|
787
|
+
}
|
|
788
|
+
|
|
789
|
+
fn kept(cache: &'static CodeCache, text: &JsText, data: Box<[u8]>) -> Stored {
|
|
790
|
+
let _ = cache.plan(&name(), text, None, true);
|
|
791
|
+
let Plan::Make(building) = cache.plan(&name(), text, None, true) else {
|
|
792
|
+
panic!("a second sighting makes a cache");
|
|
793
|
+
};
|
|
794
|
+
cache
|
|
795
|
+
.store(building, data)
|
|
796
|
+
.unwrap_or_else(|| panic!("a cache with room is kept"))
|
|
797
|
+
}
|
|
798
|
+
|
|
799
|
+
fn data_of(cache: &'static CodeCache, text: &JsText) -> Vec<u8> {
|
|
800
|
+
let Plan::Consume(entry, _) = cache.plan(&name(), text, None, true) else {
|
|
801
|
+
panic!("a stored cache is found");
|
|
802
|
+
};
|
|
803
|
+
entry.data().to_vec()
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// Built again, an entry holds the new cache and becomes the most recently
|
|
807
|
+
// used, read or not as it was.
|
|
808
|
+
#[test]
|
|
809
|
+
fn a_cache_built_again_takes_the_entrys_place() {
|
|
810
|
+
let cache = cache();
|
|
811
|
+
let stored = kept(cache, &tiny(0), Box::from(&b"loaded"[..]));
|
|
812
|
+
for seed in 1..MAX_ENTRIES {
|
|
813
|
+
cached(cache, &tiny(seed), Box::from(&b"compiled"[..]));
|
|
814
|
+
}
|
|
815
|
+
cache.replace(&stored, Box::from(&b"called"[..]));
|
|
816
|
+
assert_eq!(cache.rebuilt(), 1);
|
|
817
|
+
cached(cache, &tiny(MAX_ENTRIES), Box::from(&b"compiled"[..]));
|
|
818
|
+
assert_eq!(data_of(cache, &tiny(0)), b"called");
|
|
819
|
+
assert!(matches!(
|
|
820
|
+
cache.plan(&name(), &tiny(1), None, true),
|
|
821
|
+
Plan::Make(_)
|
|
822
|
+
));
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
#[test]
|
|
826
|
+
fn an_entry_gone_or_too_large_built_again_is_left_as_it_was() {
|
|
827
|
+
let cache = cache();
|
|
828
|
+
let gone = kept(cache, &tiny(0), Box::from(&b"loaded"[..]));
|
|
829
|
+
let Plan::Consume(entry, _) = cache.plan(&name(), &tiny(0), None, true) else {
|
|
830
|
+
panic!("a stored cache is found");
|
|
831
|
+
};
|
|
832
|
+
cache.forget(&entry);
|
|
833
|
+
cache.replace(&gone, Box::from(&b"called"[..]));
|
|
834
|
+
assert!(matches!(
|
|
835
|
+
cache.plan(&name(), &tiny(0), None, true),
|
|
836
|
+
Plan::Make(_)
|
|
837
|
+
));
|
|
838
|
+
let large = kept(cache, &tiny(1), Box::from(&b"loaded"[..]));
|
|
839
|
+
cache.replace(&large, vec![0; super::MAX_BYTES + 1].into_boxed_slice());
|
|
840
|
+
assert_eq!(data_of(cache, &tiny(1)), b"loaded");
|
|
841
|
+
assert_eq!(cache.rebuilt(), 0);
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
// A first sighting's entry no context has read yet still makes room only
|
|
845
|
+
// from unread entries when built again.
|
|
846
|
+
#[test]
|
|
847
|
+
fn a_first_sighting_built_again_never_drops_an_entry_a_context_has_read() {
|
|
848
|
+
let cache = cache();
|
|
849
|
+
let half = || vec![0; super::MAX_BYTES / 2 + 1].into_boxed_slice();
|
|
850
|
+
cached(cache, &tiny(1), half());
|
|
851
|
+
consumed(cache, &tiny(1));
|
|
852
|
+
let text = bundle(1, FIRST_SIGHTING);
|
|
853
|
+
let Plan::First(key) = cache.plan(&name(), &text, None, true) else {
|
|
854
|
+
panic!("a first sighting worth a cache at once");
|
|
855
|
+
};
|
|
856
|
+
let building = cache.first(key).unwrap_or_else(|| panic!("unclaimed"));
|
|
857
|
+
let stored = cache
|
|
858
|
+
.store(building, Box::from(&b"loaded"[..]))
|
|
859
|
+
.unwrap_or_else(|| panic!("a cache with room is kept"));
|
|
860
|
+
cache.replace(&stored, half());
|
|
861
|
+
assert_eq!(cache.rebuilt(), 0);
|
|
862
|
+
assert_eq!(data_of(cache, &text), b"loaded");
|
|
863
|
+
// Read now, it is built again as any other entry.
|
|
864
|
+
consumed(cache, &text);
|
|
865
|
+
cache.replace(&stored, half());
|
|
866
|
+
assert_eq!(cache.rebuilt(), 1);
|
|
867
|
+
assert!(matches!(
|
|
868
|
+
cache.plan(&name(), &tiny(1), None, true),
|
|
869
|
+
Plan::Make(_)
|
|
870
|
+
));
|
|
871
|
+
}
|
|
459
872
|
}
|
|
@@ -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))
|