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.
@@ -71,6 +71,10 @@ pub struct Sources {
71
71
  made: AtomicU64,
72
72
  reads: AtomicU64,
73
73
  conversions: AtomicU64,
74
+ // What the list keeps, set under its lock, so a reader never waits on it
75
+ // and a forked child never finds it held.
76
+ kept: AtomicU64,
77
+ kept_bytes: AtomicU64,
74
78
  }
75
79
 
76
80
  impl Default for Sources {
@@ -179,6 +183,19 @@ impl List {
179
183
  .find(|(refused, _)| refused == key)
180
184
  .map_or_else(|| least_bytes(text), |&(_, bytes)| bytes)
181
185
  }
186
+
187
+ fn totals(&self) -> (u64, u64) {
188
+ let bytes = self
189
+ .kept
190
+ .iter()
191
+ .map(|kept| kept.units.bytes())
192
+ .sum::<usize>();
193
+ (to_u64(self.kept.len()), to_u64(bytes))
194
+ }
195
+ }
196
+
197
+ fn to_u64(n: usize) -> u64 {
198
+ u64::try_from(n).unwrap_or(u64::MAX)
182
199
  }
183
200
 
184
201
  // The fewest bytes `text` can take as units, so a source that cannot fit is
@@ -201,9 +218,17 @@ impl Sources {
201
218
  made: AtomicU64::new(0),
202
219
  reads: AtomicU64::new(0),
203
220
  conversions: AtomicU64::new(0),
221
+ kept: AtomicU64::new(0),
222
+ kept_bytes: AtomicU64::new(0),
204
223
  }
205
224
  }
206
225
 
226
+ fn publish(&self, list: &List) {
227
+ let (count, bytes) = list.totals();
228
+ self.kept.store(count, Ordering::Relaxed);
229
+ self.kept_bytes.store(bytes, Ordering::Relaxed);
230
+ }
231
+
207
232
  /// A number for a new context to read shared sources as, told apart from
208
233
  /// every other context of this process.
209
234
  pub(crate) fn reader(&self) -> u64 {
@@ -223,6 +248,7 @@ impl Sources {
223
248
  // unread is read again, and kept, rather than copied again.
224
249
  let found = list.find(key, reader);
225
250
  let spent = list.sweep();
251
+ self.publish(&list);
226
252
  let room = found.is_none() && list.room(list.least_bytes(key, text), self.bounds);
227
253
  drop(list);
228
254
  drop(spent);
@@ -271,6 +297,7 @@ impl Sources {
271
297
  held: 1,
272
298
  readers,
273
299
  });
300
+ self.publish(&list);
274
301
  drop(list);
275
302
  // Freed once the lock is released, as a large source takes a while,
276
303
  // like the spent copies above.
@@ -305,9 +332,11 @@ impl Sources {
305
332
  swept: list.swept.clone(),
306
333
  })
307
334
  .unwrap_or_default();
335
+ let inherited = Self::bounded(self.bounds.0, self.bounds.1);
336
+ inherited.publish(&list);
308
337
  Self {
309
338
  list: Mutex::new(list),
310
- ..Self::bounded(self.bounds.0, self.bounds.1)
339
+ ..inherited
311
340
  }
312
341
  }
313
342
 
@@ -329,6 +358,18 @@ impl Sources {
329
358
  self.conversions.load(Ordering::Relaxed)
330
359
  }
331
360
 
361
+ /// Sources kept now, a forked child's inherited ones included.
362
+ #[must_use]
363
+ pub fn kept(&self) -> u64 {
364
+ self.kept.load(Ordering::Relaxed)
365
+ }
366
+
367
+ /// The bytes of the sources kept now, one a unit or two, as V8 reads them.
368
+ #[must_use]
369
+ pub fn kept_bytes(&self) -> u64 {
370
+ self.kept_bytes.load(Ordering::Relaxed)
371
+ }
372
+
332
373
  /// Strings alive now that read a kept source.
333
374
  #[must_use]
334
375
  pub fn reading(&self) -> usize {
@@ -412,10 +453,12 @@ mod tests {
412
453
  let sources = Sources::default();
413
454
  let text = script(1);
414
455
  let key = key(&text);
456
+ assert_eq!((sources.kept(), sources.kept_bytes()), (0, 0));
415
457
  let first = one_byte(sources.units(&key, &text, sources.reader()));
416
458
  let second = one_byte(sources.units(&key, &text, sources.reader()));
417
459
  assert!(Arc::ptr_eq(&first, &second));
418
460
  assert_eq!(sources.made(), 1);
461
+ assert_eq!((sources.kept(), sources.kept_bytes()), (1, 2 << 20));
419
462
  assert_eq!(sources.reading(), 2);
420
463
  drop((first, second));
421
464
  assert_eq!(sources.reading(), 0);
@@ -491,6 +534,7 @@ mod tests {
491
534
  assert!(copy.upgrade().is_some());
492
535
  drop(sources.units(&other_key, &other, sources.reader()));
493
536
  assert!(copy.upgrade().is_none());
537
+ assert_eq!((sources.kept(), sources.kept_bytes()), (1, 2 << 20));
494
538
  drop(sources.units(&key, &text, sources.reader()));
495
539
  assert_eq!(sources.made(), 3);
496
540
  }
@@ -611,6 +655,12 @@ mod tests {
611
655
  drop(still_read);
612
656
  assert!(two_byte(sources.units(&astral_key, &astral, sources.reader())).len() > 2 << 20);
613
657
  assert_eq!((sources.conversions(), sources.made()), (3, 2));
658
+ // The copy one context read is swept, and the two-byte one counts two
659
+ // bytes a unit.
660
+ assert_eq!(
661
+ (sources.kept(), sources.kept_bytes()),
662
+ (1, 2 * ((2 << 20) + 2))
663
+ );
614
664
  }
615
665
 
616
666
  #[test]
@@ -637,6 +687,8 @@ mod tests {
637
687
  let key = key(&text);
638
688
  let shared = parent.units(&key, &text, parent.reader());
639
689
  let child = parent.inherited();
690
+ assert_eq!((child.kept(), child.kept_bytes()), (1, 2 << 20));
691
+ assert_eq!((child.made(), child.reads()), (0, 0));
640
692
  let in_child = child.units(&key, &text, child.reader());
641
693
  assert!(same(shared.as_ref(), in_child.as_ref()));
642
694
  assert_eq!(child.reading(), 2);
@@ -714,8 +766,11 @@ mod tests {
714
766
  let key = key(&text);
715
767
  let shared = one_byte(parent.units(&key, &text, parent.reader()));
716
768
  let held = parent.list.lock();
769
+ // Read without the lock, which a child may find held.
770
+ assert_eq!((parent.kept(), parent.kept_bytes()), (1, 2 << 20));
717
771
  let child = parent.inherited();
718
772
  drop(held);
773
+ assert_eq!((child.kept(), child.kept_bytes()), (0, 0));
719
774
  assert!(!Arc::ptr_eq(
720
775
  &shared,
721
776
  &one_byte(child.units(&key, &text, child.reader()))
@@ -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 script = v8::Local::new(scope, &script);
747
- if let Some(data) = script.create_code_cache() {
748
- cache.store(building, Box::from(&**data));
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 large(source: &JsText) -> bool {
806
+ const fn at_least(source: &JsText, bytes: usize) -> bool {
757
807
  match source {
758
- JsText::Utf8(bytes) | JsText::Latin1(bytes) => bytes.len() >= LARGE_SOURCE,
759
- JsText::Utf16(units) => units.len() >= LARGE_SOURCE / 2,
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 plan = if large(source) {
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 source = shared_source(tc, runtime, plan.key(), source)
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(_) => return (v8::Script::compile(scope, source, Some(origin)), None),
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
 
@@ -27,6 +27,7 @@ const ISOLATE_BYTES: usize = 2 << 20;
27
27
  const POLL: std::time::Duration = std::time::Duration::from_millis(1);
28
28
  // mini_racer warns of every fiftieth level of js->ruby->js nesting.
29
29
  const DEEP_NESTING: u32 = 50;
30
+ const SOURCE_WITHOUT_GVL: usize = 1 << 20;
30
31
 
31
32
  static OWNED: LazyId = LazyId::new("owned?");
32
33
 
@@ -119,19 +120,54 @@ impl Drop for Locked {
119
120
 
120
121
  // mini_racer sends ISO-8859-1 and UTF-16LE strings in their own encoding and
121
122
  // every other string's bytes as UTF-8.
122
- pub fn js_text(ruby: &Ruby, string: RString) -> Result<JsText, Error> {
123
- let bytes = rb_raw::string_bytes(ruby, string);
124
- Ok(match RbEncoding::from(string.enc_get()).name() {
125
- "ISO-8859-1" => JsText::Latin1(bytes),
126
- "UTF-16LE" => {
127
- let (units, rest) = bytes.as_chunks::<2>();
128
- if !rest.is_empty() {
129
- return Err(errors::undeserializable(ruby));
130
- }
131
- JsText::Utf16(units.iter().map(|unit| u16::from_le_bytes(*unit)).collect())
123
+ #[derive(Clone, Copy)]
124
+ enum Sent {
125
+ Utf8,
126
+ Latin1,
127
+ Utf16,
128
+ }
129
+
130
+ impl Sent {
131
+ fn of(string: RString) -> Self {
132
+ match RbEncoding::from(string.enc_get()).name() {
133
+ "ISO-8859-1" => Self::Latin1,
134
+ "UTF-16LE" => Self::Utf16,
135
+ _ => Self::Utf8,
132
136
  }
133
- _ => JsText::Utf8(bytes),
134
- })
137
+ }
138
+
139
+ fn text(self, bytes: &[u8]) -> Option<JsText> {
140
+ Some(match self {
141
+ Self::Utf8 => JsText::Utf8(bytes.to_vec()),
142
+ Self::Latin1 => JsText::Latin1(bytes.to_vec()),
143
+ Self::Utf16 => {
144
+ let (units, rest) = bytes.as_chunks::<2>();
145
+ if !rest.is_empty() {
146
+ return None;
147
+ }
148
+ JsText::Utf16(units.iter().map(|unit| u16::from_le_bytes(*unit)).collect())
149
+ }
150
+ })
151
+ }
152
+ }
153
+
154
+ pub fn js_text(ruby: &Ruby, string: RString) -> Result<JsText, Error> {
155
+ let sent = Sent::of(string);
156
+ rb_raw::with_string_bytes(ruby, string, |bytes| sent.text(bytes))
157
+ .ok_or_else(|| errors::undeserializable(ruby))
158
+ }
159
+
160
+ // Under the GVL, copying a source this long (about 0.4 ms a megabyte) would
161
+ // stall every other Ruby thread; a shorter copy costs less than taking the
162
+ // GVL back from one that is busy.
163
+ fn source_text(ruby: &Ruby, string: RString) -> Result<JsText, Error> {
164
+ if string.len() < SOURCE_WITHOUT_GVL {
165
+ return js_text(ruby, string);
166
+ }
167
+ let sent = Sent::of(string);
168
+ rb_raw::read_without_gvl(ruby, string, |bytes| sent.text(bytes))?
169
+ .map_err(|_| errors::internal(ruby, "panic while copying the source"))?
170
+ .ok_or_else(|| errors::undeserializable(ruby))
135
171
  }
136
172
 
137
173
  // mini_racer looks for DateTime at each Context.new until it exists.
@@ -301,7 +337,7 @@ impl Context {
301
337
  }
302
338
  _ => JsText::from("<eval>"),
303
339
  };
304
- let source = js_text(ruby, source)?;
340
+ let source = source_text(ruby, source)?;
305
341
  let options = ScriptOptions {
306
342
  awaited,
307
343
  timeout: timeout::current(ruby)?,
@@ -1,13 +1,38 @@
1
+ use magnus::value::ReprValue;
1
2
  use magnus::{Error, Object, RHash, RModule, Ruby};
3
+ use mini_racer_engine::process::fork;
2
4
 
3
5
  use crate::context::Context;
4
6
  use crate::export;
5
7
 
6
8
  pub fn init(rs: RModule) -> Result<(), Error> {
7
9
  rs.define_singleton_method("counters", export!(function counters, 1))?;
10
+ rs.define_singleton_method("code_cache", export!(function code_cache, 0))?;
8
11
  Ok(())
9
12
  }
10
13
 
14
+ // Read from atomics alone, so a forked child reads the copy it inherited
15
+ // without a lock a thread of its parent may have held at the fork.
16
+ fn code_cache(ruby: &Ruby) -> Result<RHash, Error> {
17
+ let generation = fork::current();
18
+ let (cache, sources) = (generation.code_cache(), generation.sources());
19
+ let hash = ruby.hash_new();
20
+ for (name, count) in [
21
+ ("entries", cache.entries()),
22
+ ("bytes", cache.bytes()),
23
+ ("hits", cache.hits()),
24
+ ("builds", cache.stored()),
25
+ ("rebuilds", cache.rebuilt()),
26
+ ("refused", cache.refusals()),
27
+ ("shared_sources", sources.kept()),
28
+ ("shared_bytes", sources.kept_bytes()),
29
+ ] {
30
+ hash.aset(ruby.to_symbol(name), count)?;
31
+ }
32
+ hash.freeze();
33
+ Ok(hash)
34
+ }
35
+
11
36
  // Read without the context's lock, so a thread can read a context another
12
37
  // thread is running.
13
38
  fn counters(ruby: &Ruby, context: &Context) -> Result<RHash, Error> {
@@ -559,6 +559,49 @@ pub fn with_string_bytes<R>(_ruby: &Ruby, s: RString, read: impl FnOnce(&[u8]) -
559
559
  read(unsafe { s.as_slice() })
560
560
  }
561
561
 
562
+ /// Runs `read` over the bytes `s` holds now, without the GVL, so Ruby's other
563
+ /// threads run meanwhile. It reads a frozen share of `s` that this frame
564
+ /// holds, so the GC neither frees nor moves it, and whose bytes a change to
565
+ /// `s` copies before it writes. An interrupt already pending is run first, as
566
+ /// `check_ints` runs one, up to twice, then `read` runs with the GVL held. The
567
+ /// outer `Err` is what making the share or an interrupt raised.
568
+ pub fn read_without_gvl<F, R>(
569
+ ruby: &Ruby,
570
+ s: RString,
571
+ read: F,
572
+ ) -> Result<Result<R, Panicked>, Error>
573
+ where
574
+ F: Fn(&[u8]) -> R + Sync,
575
+ R: Send,
576
+ {
577
+ let raw = s.as_raw();
578
+ let frozen = protect(|| {
579
+ // SAFETY: the `&Ruby` argument proves this thread holds the GVL, and
580
+ // `protect` catches what an allocation that fails raises.
581
+ unsafe { rb_sys::rb_str_new_frozen(raw) }
582
+ })?;
583
+ // SAFETY: `frozen` is the live String rb_str_new_frozen returned.
584
+ let share = RString::from_value(unsafe { Value::from_raw(frozen) })
585
+ .ok_or_else(|| Error::new(ruby.exception_type_error(), "expected String"))?;
586
+ let read = &read;
587
+ for _ in 0..2 {
588
+ // SAFETY: a frozen String's bytes never change, and `share` stays on
589
+ // this stack, where the GC of a thread that takes the GVL meanwhile
590
+ // finds and pins it, until `black_box` reads it after the call.
591
+ let bytes = unsafe { share.as_slice() };
592
+ if let Some(result) = nogvl(ruby, move || read(bytes), &|| {}) {
593
+ std::hint::black_box(&share);
594
+ return Ok(result);
595
+ }
596
+ check_ints(ruby)?;
597
+ }
598
+ // SAFETY: as above, with the GVL held and no Ruby code run until it ends.
599
+ let bytes = unsafe { share.as_slice() };
600
+ let result = guard(|| read(bytes));
601
+ std::hint::black_box(&share);
602
+ Ok(result)
603
+ }
604
+
562
605
  /// The magnitude of an Integer as little-endian 64-bit words, and whether it is
563
606
  /// negative; `None` past `max_words`.
564
607
  pub fn integer_words(
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MiniRacerRs
4
- VERSION = "1.5.2"
4
+ VERSION = "1.6.0"
5
5
  end
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.2
4
+ version: 1.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Aragon
@@ -154,9 +154,9 @@ licenses:
154
154
  - MIT
155
155
  metadata:
156
156
  homepage_uri: https://github.com/andymai/mini_racer_rs
157
- source_code_uri: https://github.com/andymai/mini_racer_rs/tree/v1.5.2
158
- changelog_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.2/CHANGELOG.md
159
- documentation_uri: https://github.com/andymai/mini_racer_rs/blob/v1.5.2/README.md#api-at-a-glance
157
+ source_code_uri: https://github.com/andymai/mini_racer_rs/tree/v1.6.0
158
+ changelog_uri: https://github.com/andymai/mini_racer_rs/blob/v1.6.0/CHANGELOG.md
159
+ documentation_uri: https://github.com/andymai/mini_racer_rs/blob/v1.6.0/README.md#api-at-a-glance
160
160
  bug_tracker_uri: https://github.com/andymai/mini_racer_rs/issues
161
161
  funding_uri: https://github.com/sponsors/andymai
162
162
  rubygems_mfa_required: 'true'