Skip to content

Commit 02f0220

Browse files
committed
refactor: try unsafe cell
1 parent 728629d commit 02f0220

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/replace_source.rs

+23-18
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
2-
borrow::Cow,
3-
cell::RefCell,
2+
borrow::{Borrow, Cow},
3+
cell::{RefCell, UnsafeCell},
44
hash::{Hash, Hasher},
55
sync::{
66
atomic::{AtomicBool, Ordering},
@@ -265,7 +265,7 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
265265
on_name: crate::helpers::OnName<'_, 'a>,
266266
) -> crate::helpers::GeneratedInfo {
267267
self.sort_replacement();
268-
let on_name = RefCell::new(on_name);
268+
let on_name = UnsafeCell::new(on_name);
269269
let repls = self.replacements();
270270
let mut pos: u32 = 0;
271271
let mut i: usize = 0;
@@ -274,12 +274,12 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
274274
let mut generated_line_offset: i64 = 0;
275275
let mut generated_column_offset: i64 = 0;
276276
let mut generated_column_offset_line = 0;
277-
let source_content_lines: RefCell<Vec<Option<Vec<&str>>>> =
278-
RefCell::new(Vec::new());
279-
let name_mapping: RefCell<HashMap<Cow<str>, u32>> =
280-
RefCell::new(HashMap::default());
281-
let name_index_mapping: RefCell<HashMap<u32, u32>> =
282-
RefCell::new(HashMap::default());
277+
let source_content_lines: UnsafeCell<Vec<Option<Vec<&str>>>> =
278+
UnsafeCell::new(Vec::new());
279+
let name_mapping: UnsafeCell<HashMap<Cow<str>, u32>> =
280+
UnsafeCell::new(HashMap::default());
281+
let name_index_mapping: UnsafeCell<HashMap<u32, u32>> =
282+
UnsafeCell::new(HashMap::default());
283283

284284
// check if source_content[line][col] is equal to expect
285285
// Why this is needed?
@@ -309,8 +309,9 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
309309
// webpack-sources also have this function, refer https://github.com/webpack/webpack-sources/blob/main/lib/ReplaceSource.js#L158
310310
let check_original_content =
311311
|source_index: u32, line: u32, column: u32, expected_chunk: &str| {
312+
let source_content_lines = unsafe { &mut *source_content_lines.get() };
312313
if let Some(Some(content_lines)) =
313-
source_content_lines.borrow().get(source_index as usize)
314+
source_content_lines.get(source_index as usize)
314315
{
315316
if let Some(content_line) = content_lines.get(line as usize - 1) {
316317
WithIndices::new(content_line).substring(
@@ -405,7 +406,8 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
405406
original_line: original.original_line,
406407
original_column: original.original_column,
407408
name_index: original.name_index.and_then(|name_index| {
408-
name_index_mapping.borrow().get(&name_index).copied()
409+
let name_index_mapping = unsafe { &mut *name_index_mapping.get() };
410+
name_index_mapping.get(&name_index).copied()
409411
}),
410412
}
411413
}),
@@ -437,12 +439,13 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
437439
if let Some(name) =
438440
repl.name.as_ref().filter(|_| mapping.original.is_some())
439441
{
440-
let mut name_mapping = name_mapping.borrow_mut();
442+
let name_mapping = unsafe { &mut *name_mapping.get() };
441443
let mut global_index = name_mapping.get(name.as_str()).copied();
442444
if global_index.is_none() {
443445
let len = name_mapping.len() as u32;
444446
name_mapping.insert(Cow::Borrowed(name), len);
445-
on_name.borrow_mut()(len, Cow::Owned(name.to_string()));
447+
let on_name = unsafe { &mut *on_name.get() };
448+
on_name(len, Cow::Owned(name.to_string()));
446449
global_index = Some(len);
447450
}
448451
replacement_name_index = global_index;
@@ -582,7 +585,8 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
582585
original_line: original.original_line,
583586
original_column: original.original_column,
584587
name_index: original.name_index.and_then(|name_index| {
585-
name_index_mapping.borrow().get(&name_index).copied()
588+
let name_index_mapping = unsafe { &mut *name_index_mapping.get() };
589+
name_index_mapping.get(&name_index).copied()
586590
}),
587591
}
588592
}),
@@ -592,7 +596,7 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
592596
pos = end_pos;
593597
},
594598
&mut |source_index, source, source_content| {
595-
let mut source_content_lines = source_content_lines.borrow_mut();
599+
let source_content_lines = unsafe { &mut *source_content_lines.get() };
596600
while source_content_lines.len() <= source_index as usize {
597601
source_content_lines.push(None);
598602
}
@@ -601,16 +605,17 @@ impl<'a, T: Source> StreamChunks<'a> for ReplaceSource<T> {
601605
on_source(source_index, source, source_content);
602606
},
603607
&mut |name_index, name| {
604-
let mut name_mapping = name_mapping.borrow_mut();
608+
let name_mapping = unsafe { &mut *name_mapping.get() };
605609
let mut global_index = name_mapping.get(&name).copied();
606610
if global_index.is_none() {
607611
let len = name_mapping.len() as u32;
608612
name_mapping.insert(name.clone(), len);
609-
on_name.borrow_mut()(len, name);
613+
let on_name = unsafe { &mut *on_name.get() };
614+
on_name(len, name);
610615
global_index = Some(len);
611616
}
617+
let name_index_mapping = unsafe { &mut *name_index_mapping.get() };
612618
name_index_mapping
613-
.borrow_mut()
614619
.insert(name_index, global_index.unwrap());
615620
},
616621
);

0 commit comments

Comments
 (0)