mini_racer_rs 1.5.2 → 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/CHANGELOG.md +9 -0
- data/Cargo.lock +5 -5
- data/Cargo.toml +1 -1
- data/README.md +2 -2
- data/crates/mini_racer_engine/src/context.rs +1 -0
- data/crates/mini_racer_engine/src/process/code_cache.rs +393 -24
- data/crates/mini_racer_engine/src/runner.rs +90 -14
- 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: e86ed50c4fed6c7f1bbf5276e15277182938a0c11aa1ada7e385d74cc5db4ec0
|
|
4
|
+
data.tar.gz: 75aa12887ad5afff7aeb3073a43b0eda296eb664eadfdda84f24b93db31bd033
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5e3b56230941049091937320488d063974d20abc228dc9471dab3195d0202fd1a4c1a26cbfb3973790434c3efaffe22e47a1af2511fae9fbaafe709f8e8fb0bd
|
|
7
|
+
data.tar.gz: bd0f079417fa2f08d1e4bfeb7525ba015aeb9033e85fb1c18bdf9964da5f65f33b8c61caf41d87eb5defe329592ac6e77b42754594565d543ae86910d40e9632
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.5.3](https://github.com/andymai/mini_racer_rs/compare/v1.5.2...v1.5.3) (2026-10-24)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Performance
|
|
7
|
+
|
|
8
|
+
* **engine:** cache a large first sighting that runs as long as it compiles ([#159](https://github.com/andymai/mini_racer_rs/issues/159)) ([47cc0cc](https://github.com/andymai/mini_racer_rs/commit/47cc0ccd76c5d39ec3c9005b848e5846e213d618))
|
|
9
|
+
* **engine:** cache a script again after its builder's next call ([#160](https://github.com/andymai/mini_racer_rs/issues/160)) ([8f632e0](https://github.com/andymai/mini_racer_rs/commit/8f632e065ec3506687d69e8e13f0df56b2e21064))
|
|
10
|
+
* **engine:** cache scripts from 128 KiB and keep 64 caches ([#158](https://github.com/andymai/mini_racer_rs/issues/158)) ([0b77e64](https://github.com/andymai/mini_racer_rs/commit/0b77e640d311db4c22d711a884be5aeadbcc0e0b))
|
|
11
|
+
|
|
3
12
|
## [1.5.2](https://github.com/andymai/mini_racer_rs/compare/v1.5.1...v1.5.2) (2026-10-24)
|
|
4
13
|
|
|
5
14
|
|
data/Cargo.lock
CHANGED
|
@@ -593,7 +593,7 @@ checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
|
|
|
593
593
|
|
|
594
594
|
[[package]]
|
|
595
595
|
name = "mini_racer_engine"
|
|
596
|
-
version = "1.5.
|
|
596
|
+
version = "1.5.3"
|
|
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.
|
|
609
|
+
version = "1.5.3"
|
|
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.
|
|
620
|
+
version = "1.5.3"
|
|
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.
|
|
630
|
+
version = "1.5.3"
|
|
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.
|
|
638
|
+
version = "1.5.3"
|
|
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.5.
|
|
18
|
+
version = "1.5.3"
|
|
19
19
|
|
|
20
20
|
[workspace.dependencies]
|
|
21
21
|
criterion = {version = "0.8", default-features = false, features = ["cargo_bench_support"]}
|
data/README.md
CHANGED
|
@@ -208,7 +208,7 @@ The repository tests the `path:` form of the shim with rtlcss in `test/consumers
|
|
|
208
208
|
|
|
209
209
|
### Forking servers such as Puma
|
|
210
210
|
|
|
211
|
-
Before forking, evaluate each bundle of
|
|
211
|
+
Before forking, evaluate each bundle of 128 KiB or more twice in the master, then dispose those contexts. Use the renderer's exact source text and file name. The second evaluation builds the process-wide V8 code cache, or the first for a bundle of 16 MiB or more whose top-level code runs at least as long as it compiles. Matching evaluations compile from the cache, and forked children inherit it. The context that built a cache rebuilds it once after its next call, so a render in the master also gives children the functions that render compiled. For Babel's 2.3 MB bundle at commit `5e13c1c`, a forked child's first load took 60.8 ms without a parent load. It took 65.5 ms after one load and 25.0 ms after two. These are medians across 5 trials. Prime a `react-rails` app by building its server renderer twice. Children do not use parent contexts. Only the cache carries over.
|
|
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
|
|
|
@@ -230,7 +230,7 @@ With `mini_racer` 0.22.2, each worker compiles the bundle from source on its fir
|
|
|
230
230
|
|
|
231
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 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
|
|
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 128 KiB or more use a process-wide V8 code cache. Contexts loading the same script of 1 MiB or more share one source copy.
|
|
234
234
|
|
|
235
235
|
| Measure | mini_racer 0.22.2 | mini_racer_rs |
|
|
236
236
|
|---|---:|---:|
|
|
@@ -251,6 +251,7 @@ impl ContextHandle {
|
|
|
251
251
|
process::ensure_initialized();
|
|
252
252
|
// Read here, on the caller's thread, not first on a runner's.
|
|
253
253
|
let _ = process::code_cache::enabled();
|
|
254
|
+
let _ = process::code_cache::first_override();
|
|
254
255
|
let _ = process::fast_calls();
|
|
255
256
|
let generation = fork::current();
|
|
256
257
|
let block = Arc::new(ControlBlock::new(config));
|
|
@@ -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
|
-
|
|
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
|
-
|
|
24
|
-
|
|
25
|
-
|
|
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,7 @@ pub struct CodeCache {
|
|
|
43
59
|
hasher: RandomState,
|
|
44
60
|
hits: AtomicU64,
|
|
45
61
|
stored: AtomicU64,
|
|
62
|
+
rebuilt: AtomicU64,
|
|
46
63
|
}
|
|
47
64
|
|
|
48
65
|
/// What a cached script is found by: its file name, the startup snapshot its
|
|
@@ -60,6 +77,8 @@ pub(crate) struct Key {
|
|
|
60
77
|
pub(crate) struct Entry {
|
|
61
78
|
key: Key,
|
|
62
79
|
data: Box<[u8]>,
|
|
80
|
+
// A context compiled from it.
|
|
81
|
+
read: AtomicBool,
|
|
63
82
|
}
|
|
64
83
|
|
|
65
84
|
impl Entry {
|
|
@@ -68,10 +87,20 @@ impl Entry {
|
|
|
68
87
|
}
|
|
69
88
|
}
|
|
70
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
|
+
|
|
71
97
|
/// A key being built; dropped unbuilt, it lets another context build it.
|
|
72
98
|
pub(crate) struct Building {
|
|
73
99
|
cache: &'static CodeCache,
|
|
74
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,
|
|
75
104
|
}
|
|
76
105
|
|
|
77
106
|
impl Drop for Building {
|
|
@@ -86,6 +115,9 @@ pub(crate) enum Plan {
|
|
|
86
115
|
/// first sighting whose source is shared, a key being built elsewhere or
|
|
87
116
|
/// a refused cache.
|
|
88
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),
|
|
89
121
|
/// From source, then a cache made once the dispatch is over.
|
|
90
122
|
Make(Building),
|
|
91
123
|
/// From the entry, falling back to source if V8 refuses it.
|
|
@@ -98,7 +130,7 @@ impl Plan {
|
|
|
98
130
|
match self {
|
|
99
131
|
Self::Compile(key) => key.as_ref(),
|
|
100
132
|
Self::Make(building) => Some(&building.key),
|
|
101
|
-
Self::Consume(_, key) => Some(key),
|
|
133
|
+
Self::First(key) | Self::Consume(_, key) => Some(key),
|
|
102
134
|
}
|
|
103
135
|
}
|
|
104
136
|
}
|
|
@@ -133,6 +165,9 @@ impl CodeCache {
|
|
|
133
165
|
digest: self.hasher.hash_one(source),
|
|
134
166
|
sighting,
|
|
135
167
|
};
|
|
168
|
+
if first && source.byte_len() >= FIRST_SIGHTING {
|
|
169
|
+
return Plan::First(key);
|
|
170
|
+
}
|
|
136
171
|
if first {
|
|
137
172
|
return Plan::Compile(Some(key));
|
|
138
173
|
}
|
|
@@ -153,13 +188,32 @@ impl CodeCache {
|
|
|
153
188
|
|
|
154
189
|
/// Claims `key` for this context to build, unless another is building it.
|
|
155
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> {
|
|
156
206
|
let mut building = lock(&self.building);
|
|
157
207
|
if building.contains(&key) {
|
|
158
208
|
return None;
|
|
159
209
|
}
|
|
160
210
|
building.push(key.clone());
|
|
161
211
|
drop(building);
|
|
162
|
-
Some(Building {
|
|
212
|
+
Some(Building {
|
|
213
|
+
cache: self,
|
|
214
|
+
key,
|
|
215
|
+
first,
|
|
216
|
+
})
|
|
163
217
|
}
|
|
164
218
|
|
|
165
219
|
/// A startup snapshot's identity for `Key`, from a sample of the blob:
|
|
@@ -170,31 +224,70 @@ impl CodeCache {
|
|
|
170
224
|
hasher.finish()
|
|
171
225
|
}
|
|
172
226
|
|
|
173
|
-
pub(crate) fn hit(&self) {
|
|
227
|
+
pub(crate) fn hit(&self, entry: &Entry) {
|
|
228
|
+
entry.read.store(true, Ordering::Relaxed);
|
|
174
229
|
self.hits.fetch_add(1, Ordering::Relaxed);
|
|
175
230
|
}
|
|
176
231
|
|
|
177
232
|
/// 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
|
-
|
|
180
|
-
|
|
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
|
+
};
|
|
181
243
|
drop(building);
|
|
182
244
|
if data.len() > MAX_BYTES {
|
|
183
|
-
touch(&self.refused, key, MAX_REFUSED);
|
|
184
|
-
return;
|
|
245
|
+
touch(&self.refused, stored.key, MAX_REFUSED);
|
|
246
|
+
return None;
|
|
185
247
|
}
|
|
186
248
|
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);
|
|
249
|
+
if entries.iter().any(|entry| entry.key == stored.key) {
|
|
250
|
+
return None;
|
|
195
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
|
+
}));
|
|
196
259
|
drop(entries);
|
|
197
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);
|
|
198
291
|
}
|
|
199
292
|
|
|
200
293
|
/// Drops an entry V8 refused, so no later context tries it again.
|
|
@@ -213,6 +306,7 @@ impl CodeCache {
|
|
|
213
306
|
hasher: self.hasher.clone(),
|
|
214
307
|
hits: AtomicU64::new(0),
|
|
215
308
|
stored: AtomicU64::new(0),
|
|
309
|
+
rebuilt: AtomicU64::new(0),
|
|
216
310
|
}
|
|
217
311
|
}
|
|
218
312
|
|
|
@@ -228,6 +322,12 @@ impl CodeCache {
|
|
|
228
322
|
self.stored.load(Ordering::Relaxed)
|
|
229
323
|
}
|
|
230
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
|
+
|
|
231
331
|
fn sighting(&self, filename: &JsText, source: &JsText, snapshot: Option<u64>) -> u64 {
|
|
232
332
|
let mut hasher = self.hasher.build_hasher();
|
|
233
333
|
filename.hash(&mut hasher);
|
|
@@ -251,6 +351,27 @@ impl CodeCache {
|
|
|
251
351
|
}
|
|
252
352
|
}
|
|
253
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
|
+
|
|
254
375
|
// Moves `item` to the back of `list`, adding it if absent and dropping the
|
|
255
376
|
// front past `max`. Returns whether it was there.
|
|
256
377
|
fn touch<T: PartialEq>(list: &Mutex<Vec<T>>, item: T, max: usize) -> bool {
|
|
@@ -293,10 +414,31 @@ pub(crate) fn enabled() -> bool {
|
|
|
293
414
|
})
|
|
294
415
|
}
|
|
295
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
|
+
|
|
296
436
|
#[cfg(test)]
|
|
297
437
|
#[allow(clippy::panic, reason = "a test fails by panicking")]
|
|
298
438
|
mod tests {
|
|
299
|
-
use
|
|
439
|
+
use std::time::Duration;
|
|
440
|
+
|
|
441
|
+
use super::{CodeCache, FIRST_SIGHTING, MAX_ENTRIES, MAX_SIGHTINGS, Plan, Stored};
|
|
300
442
|
use crate::control::JsText;
|
|
301
443
|
|
|
302
444
|
fn cache() -> &'static CodeCache {
|
|
@@ -500,4 +642,231 @@ mod tests {
|
|
|
500
642
|
Plan::Make(..)
|
|
501
643
|
));
|
|
502
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
|
+
}
|
|
503
872
|
}
|
|
@@ -18,7 +18,7 @@ use crate::control::{
|
|
|
18
18
|
CallRequest, CallbackReply, ControlBlock, Depth, HeapWatch, IsolateTerminator, JsText, Request,
|
|
19
19
|
};
|
|
20
20
|
use crate::fast_calls::{self, Hoisted, Shape};
|
|
21
|
-
use crate::process::code_cache::{Building, Key, Plan};
|
|
21
|
+
use crate::process::code_cache::{self, Building, Key, Plan, Stored};
|
|
22
22
|
use crate::process::fork::Generation;
|
|
23
23
|
use crate::process::platform::{self, Mode};
|
|
24
24
|
use crate::process::watchdog;
|
|
@@ -70,6 +70,9 @@ struct Runtime {
|
|
|
70
70
|
// Large scripts to cache once their dispatch is over, so making a cache
|
|
71
71
|
// never counts against a timeout.
|
|
72
72
|
uncached: Mutex<Vec<Uncached>>,
|
|
73
|
+
// Caches this context stored, built again once its next call is over, so
|
|
74
|
+
// they hold the functions that call compiled.
|
|
75
|
+
recached: Mutex<Vec<Recached>>,
|
|
73
76
|
hoisting: Mutex<Hoisting>,
|
|
74
77
|
// The values of each fast call running, innermost last, for `hoisted`.
|
|
75
78
|
frames: Mutex<Vec<v8::Global<v8::Array>>>,
|
|
@@ -149,6 +152,11 @@ struct Uncached {
|
|
|
149
152
|
building: Building,
|
|
150
153
|
}
|
|
151
154
|
|
|
155
|
+
struct Recached {
|
|
156
|
+
script: v8::Global<v8::UnboundScript>,
|
|
157
|
+
stored: Stored,
|
|
158
|
+
}
|
|
159
|
+
|
|
152
160
|
// A context's fast calls: the binding their templates read values through,
|
|
153
161
|
// the templates compiled so far, most recently used last, and the last source
|
|
154
162
|
// a fast call took, up to `LARGE_SOURCE`.
|
|
@@ -218,6 +226,7 @@ impl Runtime {
|
|
|
218
226
|
snapshot,
|
|
219
227
|
reader: generation.sources().reader(),
|
|
220
228
|
uncached: Mutex::default(),
|
|
229
|
+
recached: Mutex::default(),
|
|
221
230
|
hoisting: Mutex::default(),
|
|
222
231
|
frames: Mutex::default(),
|
|
223
232
|
matched: Mutex::default(),
|
|
@@ -409,8 +418,14 @@ fn fits(isolate: &mut v8::Isolate, watch: &HeapWatch) -> bool {
|
|
|
409
418
|
}
|
|
410
419
|
|
|
411
420
|
// A full collection the runner asks for itself, with no script to end if the
|
|
412
|
-
// heap is at its limit.
|
|
421
|
+
// heap is at its limit. The scripts held to cache again after a call go
|
|
422
|
+
// first: held, a script whose top level declared 40,000 functions kept its
|
|
423
|
+
// context's heap 0.7 MB larger through the collection.
|
|
413
424
|
fn collect(isolate: &mut v8::Isolate, block: &ControlBlock) {
|
|
425
|
+
let held = isolate
|
|
426
|
+
.get_slot::<Arc<Runtime>>()
|
|
427
|
+
.map(|runtime| std::mem::take(&mut *lock(&runtime.recached)));
|
|
428
|
+
drop(held);
|
|
414
429
|
block.heap.collecting.store(true, Ordering::Relaxed);
|
|
415
430
|
isolate.low_memory_notification();
|
|
416
431
|
block.heap.collecting.store(false, Ordering::Relaxed);
|
|
@@ -720,11 +735,15 @@ fn serve_one(
|
|
|
720
735
|
Some(ToV8::Reply(_)) => return Served::Next,
|
|
721
736
|
None => return Served::Stop { disposed: false },
|
|
722
737
|
};
|
|
738
|
+
let calls = calls(&request);
|
|
723
739
|
let response = {
|
|
724
740
|
v8::scope_with_context!(let scope, isolate, &runtime.context);
|
|
725
741
|
handle(scope, runtime, request)
|
|
726
742
|
};
|
|
727
743
|
// Before the response, so a fork that follows finds this runner idle.
|
|
744
|
+
if calls {
|
|
745
|
+
recache(isolate, runtime);
|
|
746
|
+
}
|
|
728
747
|
cache_uncached(isolate, runtime);
|
|
729
748
|
recover(isolate, block);
|
|
730
749
|
let served = respond(block, response);
|
|
@@ -743,23 +762,58 @@ fn cache_uncached(isolate: &mut v8::Isolate, runtime: &Runtime) {
|
|
|
743
762
|
let cache = runtime.generation.code_cache();
|
|
744
763
|
v8::scope!(let scope, isolate);
|
|
745
764
|
for Uncached { script, building } in uncached {
|
|
746
|
-
let
|
|
747
|
-
|
|
748
|
-
|
|
765
|
+
let Some(data) = v8::Local::new(scope, &script).create_code_cache() else {
|
|
766
|
+
continue;
|
|
767
|
+
};
|
|
768
|
+
if let Some(stored) = cache.store(building, Box::from(&**data)) {
|
|
769
|
+
lock(&runtime.recached).push(Recached { script, stored });
|
|
770
|
+
}
|
|
771
|
+
}
|
|
772
|
+
}
|
|
773
|
+
|
|
774
|
+
// A call, as opposed to a load: what runs after a script is cached compiles
|
|
775
|
+
// more of it, which the cache then holds once built again. An eval the cache
|
|
776
|
+
// would take is another load.
|
|
777
|
+
const fn calls(request: &Request) -> bool {
|
|
778
|
+
match request {
|
|
779
|
+
Request::Call { .. } => true,
|
|
780
|
+
Request::Eval { source, .. } => !at_least(source, CACHED_SOURCE),
|
|
781
|
+
_ => false,
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
fn recache(isolate: &mut v8::Isolate, runtime: &Runtime) {
|
|
786
|
+
let recached = std::mem::take(&mut *lock(&runtime.recached));
|
|
787
|
+
if recached.is_empty() || runtime.block.term.is_disposing() {
|
|
788
|
+
return;
|
|
789
|
+
}
|
|
790
|
+
let cache = runtime.generation.code_cache();
|
|
791
|
+
v8::scope!(let scope, isolate);
|
|
792
|
+
for Recached { script, stored } in recached {
|
|
793
|
+
if let Some(data) = v8::Local::new(scope, &script).create_code_cache() {
|
|
794
|
+
cache.replace(&stored, Box::from(&**data));
|
|
749
795
|
}
|
|
750
796
|
}
|
|
751
797
|
}
|
|
752
798
|
|
|
753
|
-
// A bundle, not a render: what parsing it frees is worth handing back
|
|
799
|
+
// A bundle, not a render: what parsing it frees is worth handing back, and
|
|
800
|
+
// its source is worth one copy every context shares.
|
|
754
801
|
const LARGE_SOURCE: usize = 1 << 20;
|
|
802
|
+
// A new context loaded React's 217 KB bundle in 10.1 ms, and in 2.3 from a
|
|
803
|
+
// cache that cost the context building it 0.3 ms more.
|
|
804
|
+
const CACHED_SOURCE: usize = 128 << 10;
|
|
755
805
|
|
|
756
|
-
const fn
|
|
806
|
+
const fn at_least(source: &JsText, bytes: usize) -> bool {
|
|
757
807
|
match source {
|
|
758
|
-
JsText::Utf8(
|
|
759
|
-
JsText::Utf16(units) => units.len() >=
|
|
808
|
+
JsText::Utf8(text) | JsText::Latin1(text) => text.len() >= bytes,
|
|
809
|
+
JsText::Utf16(units) => units.len() >= bytes / 2,
|
|
760
810
|
}
|
|
761
811
|
}
|
|
762
812
|
|
|
813
|
+
const fn large(source: &JsText) -> bool {
|
|
814
|
+
at_least(source, LARGE_SOURCE)
|
|
815
|
+
}
|
|
816
|
+
|
|
763
817
|
fn respond(block: &ControlBlock, response: Result<Vec<u8>, EngineError>) -> Served {
|
|
764
818
|
if block.channel.respond(response).is_ok() {
|
|
765
819
|
Served::Next
|
|
@@ -1103,31 +1157,51 @@ fn evaluate(
|
|
|
1103
1157
|
if let Some(value) = fast_call(tc, runtime, filename, source)? {
|
|
1104
1158
|
return settle(tc, runtime, value, (depth, awaited));
|
|
1105
1159
|
}
|
|
1106
|
-
let
|
|
1160
|
+
let bundle = large(source);
|
|
1161
|
+
if bundle {
|
|
1107
1162
|
runtime.freed.store(true, Ordering::Relaxed);
|
|
1163
|
+
}
|
|
1164
|
+
let plan = if at_least(source, CACHED_SOURCE) {
|
|
1108
1165
|
runtime.generation.code_cache().plan(
|
|
1109
1166
|
filename,
|
|
1110
1167
|
source,
|
|
1111
1168
|
runtime.snapshot,
|
|
1112
|
-
shares_sources(runtime),
|
|
1169
|
+
bundle && shares_sources(runtime),
|
|
1113
1170
|
)
|
|
1114
1171
|
} else {
|
|
1115
1172
|
Plan::Compile(None)
|
|
1116
1173
|
};
|
|
1117
1174
|
let too_large = || EngineError::Internal("source too large".to_owned());
|
|
1118
1175
|
let text = source;
|
|
1119
|
-
let
|
|
1176
|
+
let shared = bundle
|
|
1177
|
+
.then(|| shared_source(tc, runtime, plan.key(), source))
|
|
1178
|
+
.flatten();
|
|
1179
|
+
let source = shared
|
|
1120
1180
|
.or_else(|| string(tc, source))
|
|
1121
1181
|
.ok_or_else(too_large)?;
|
|
1122
1182
|
let name = string(tc, filename).ok_or_else(too_large)?;
|
|
1123
1183
|
let origin = serialize::origin(tc, name.into());
|
|
1184
|
+
let first = match &plan {
|
|
1185
|
+
Plan::First(key) => Some(key.clone()),
|
|
1186
|
+
_ => None,
|
|
1187
|
+
};
|
|
1188
|
+
let compiling = first.is_some().then(Instant::now);
|
|
1124
1189
|
let (script, make) = compile(tc, runtime, source, &origin, plan);
|
|
1190
|
+
let compiled = compiling.map(|at| at.elapsed());
|
|
1125
1191
|
let script = script.ok_or_else(|| failure(tc, JsErrorKind::Parse))?;
|
|
1126
1192
|
match_bundle(tc, runtime, text, name, script);
|
|
1193
|
+
let running = compiled.is_some().then(Instant::now);
|
|
1127
1194
|
let value = script
|
|
1128
1195
|
.run(tc)
|
|
1129
1196
|
.ok_or_else(|| failure(tc, JsErrorKind::Runtime))?;
|
|
1197
|
+
let ran = running.map(|at| at.elapsed());
|
|
1130
1198
|
let reply = settle(tc, runtime, value, (depth, awaited))?;
|
|
1199
|
+
let make = make.or_else(|| {
|
|
1200
|
+
let first = first?;
|
|
1201
|
+
code_cache::worth_first(compiled?, ran?)
|
|
1202
|
+
.then(|| runtime.generation.code_cache().first(first))
|
|
1203
|
+
.flatten()
|
|
1204
|
+
});
|
|
1131
1205
|
if let Some(building) = make {
|
|
1132
1206
|
let script = v8::Global::new(tc, script.get_unbound_script(tc));
|
|
1133
1207
|
lock(&runtime.uncached).push(Uncached { script, building });
|
|
@@ -1493,7 +1567,9 @@ fn compile<'s>(
|
|
|
1493
1567
|
plan: Plan,
|
|
1494
1568
|
) -> (Option<v8::Local<'s, v8::Script>>, Option<Building>) {
|
|
1495
1569
|
let (entry, key) = match plan {
|
|
1496
|
-
Plan::Compile(_)
|
|
1570
|
+
Plan::Compile(_) | Plan::First(_) => {
|
|
1571
|
+
return (v8::Script::compile(scope, source, Some(origin)), None);
|
|
1572
|
+
}
|
|
1497
1573
|
Plan::Make(building) => {
|
|
1498
1574
|
return (
|
|
1499
1575
|
v8::Script::compile(scope, source, Some(origin)),
|
|
@@ -1524,7 +1600,7 @@ fn compile<'s>(
|
|
|
1524
1600
|
cache.forget(&entry);
|
|
1525
1601
|
return (script, cache.begin(key));
|
|
1526
1602
|
}
|
|
1527
|
-
cache.hit();
|
|
1603
|
+
cache.hit(&entry);
|
|
1528
1604
|
(script, None)
|
|
1529
1605
|
}
|
|
1530
1606
|
|
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.5.
|
|
4
|
+
version: 1.5.3
|
|
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.5.
|
|
158
|
-
changelog_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.
|
|
159
|
-
documentation_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.
|
|
157
|
+
source_code_uri: https://github.com/andymai/mini_racer_rs/tree/v1.5.3
|
|
158
|
+
changelog_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.3/CHANGELOG.md
|
|
159
|
+
documentation_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.3/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'
|