-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcached_source.rs
More file actions
397 lines (359 loc) · 11.6 KB
/
cached_source.rs
File metadata and controls
397 lines (359 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
use std::{
borrow::Cow,
hash::{BuildHasherDefault, Hash, Hasher},
sync::{Arc, OnceLock},
};
use dashmap::{mapref::entry::Entry, DashMap};
use rustc_hash::FxHasher;
use crate::{
helpers::{
stream_and_get_source_and_map, stream_chunks_of_raw_source,
stream_chunks_of_source_map, StreamChunks,
},
MapOptions, Source, SourceMap,
};
/// It tries to reused cached results from other methods to avoid calculations,
/// usually used after modify is finished.
///
/// - [webpack-sources docs](https://github.com/webpack/webpack-sources/#cachedsource).
///
/// ```
/// use rspack_sources::{
/// BoxSource, CachedSource, ConcatSource, MapOptions, OriginalSource,
/// RawSource, Source, SourceExt, SourceMap,
/// };
///
/// let mut concat = ConcatSource::new([
/// RawSource::from("Hello World\n".to_string()).boxed(),
/// OriginalSource::new(
/// "console.log('test');\nconsole.log('test2');\n",
/// "console.js",
/// )
/// .boxed(),
/// ]);
/// concat.add(OriginalSource::new("Hello2\n", "hello.md"));
///
/// let cached = CachedSource::new(concat);
///
/// assert_eq!(
/// cached.source(),
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
/// );
/// // second time will be fast.
/// assert_eq!(
/// cached.source(),
/// "Hello World\nconsole.log('test');\nconsole.log('test2');\nHello2\n"
/// );
/// ```
pub struct CachedSource<T> {
inner: Arc<T>,
cached_buffer: Arc<OnceLock<Vec<u8>>>,
cached_source: Arc<OnceLock<Arc<str>>>,
cached_hash: Arc<OnceLock<u64>>,
cached_maps:
Arc<DashMap<MapOptions, Option<SourceMap>, BuildHasherDefault<FxHasher>>>,
}
impl<T> CachedSource<T> {
/// Create a [CachedSource] with the original [Source].
pub fn new(inner: T) -> Self {
Self {
inner: Arc::new(inner),
cached_buffer: Default::default(),
cached_source: Default::default(),
cached_hash: Default::default(),
cached_maps: Default::default(),
}
}
/// Get the original [Source].
pub fn original(&self) -> &T {
&self.inner
}
}
impl<T: Source + Hash + PartialEq + Eq + 'static> Source for CachedSource<T> {
fn source(&self) -> Cow<str> {
let cached = self
.cached_source
.get_or_init(|| self.inner.source().into());
Cow::Borrowed(cached)
}
fn buffer(&self) -> Cow<[u8]> {
let cached = self
.cached_buffer
.get_or_init(|| self.inner.buffer().to_vec());
Cow::Borrowed(cached)
}
fn size(&self) -> usize {
self.source().len()
}
fn map(&self, options: &MapOptions) -> Option<Cow<SourceMap>> {
if let Some(map) = self.cached_maps.get(options) {
match map.as_ref() {
Some(map) => {
#[allow(unsafe_code)]
// SAFETY: We guarantee that once a `SourceMap` is stored in the cache, it will never be removed.
// Therefore, even if we force its lifetime to be longer, the reference remains valid.
// This is based on the following assumptions:
// 1. `SourceMap` will be valid for the entire duration of the application.
// 2. The cached `SourceMap` will not be manually removed or replaced, ensuring the reference's safety.
let map = unsafe {
std::mem::transmute::<&SourceMap, &'static SourceMap>(map)
};
Some(Cow::Borrowed(map))
}
None => None,
}
} else {
let map = self.inner.map(options);
self
.cached_maps
.insert(options.clone(), map.map(|m| m.into_owned()));
match self.cached_maps.get(options).unwrap().as_ref() {
Some(map) => {
#[allow(unsafe_code)]
// SAFETY: We guarantee that once a `SourceMap` is stored in the cache, it will never be removed.
// Therefore, even if we force its lifetime to be longer, the reference remains valid.
// This is based on the following assumptions:
// 1. `SourceMap` will be valid for the entire duration of the application.
// 2. The cached `SourceMap` will not be manually removed or replaced, ensuring the reference's safety.
let map = unsafe {
std::mem::transmute::<&SourceMap, &'static SourceMap>(map)
};
Some(Cow::Borrowed(map))
}
None => None,
}
}
}
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()> {
self.inner.to_writer(writer)
}
}
impl<T: Source + Hash + PartialEq + Eq + 'static> StreamChunks<'_>
for CachedSource<T>
{
fn stream_chunks<'a>(
&'a self,
options: &MapOptions,
on_chunk: crate::helpers::OnChunk<'_, 'a>,
on_source: crate::helpers::OnSource<'_, 'a>,
on_name: crate::helpers::OnName<'_, 'a>,
) -> crate::helpers::GeneratedInfo {
let cached_map = self.cached_maps.entry(options.clone());
match cached_map {
Entry::Occupied(entry) => {
let source = self
.cached_source
.get_or_init(|| self.inner.source().into());
if let Some(map) = entry.get() {
#[allow(unsafe_code)]
// SAFETY: We guarantee that once a `SourceMap` is stored in the cache, it will never be removed.
// Therefore, even if we force its lifetime to be longer, the reference remains valid.
// This is based on the following assumptions:
// 1. `SourceMap` will be valid for the entire duration of the application.
// 2. The cached `SourceMap` will not be manually removed or replaced, ensuring the reference's safety.
let map =
unsafe { std::mem::transmute::<&SourceMap, &'a SourceMap>(map) };
stream_chunks_of_source_map(
source, map, on_chunk, on_source, on_name, options,
)
} else {
stream_chunks_of_raw_source(
source, options, on_chunk, on_source, on_name,
)
}
}
Entry::Vacant(entry) => {
let (generated_info, map) = stream_and_get_source_and_map(
&self.inner as &T,
options,
on_chunk,
on_source,
on_name,
);
entry.insert(map);
generated_info
}
}
}
}
impl<T> Clone for CachedSource<T> {
fn clone(&self) -> Self {
Self {
inner: self.inner.clone(),
cached_buffer: self.cached_buffer.clone(),
cached_source: self.cached_source.clone(),
cached_hash: self.cached_hash.clone(),
cached_maps: self.cached_maps.clone(),
}
}
}
impl<T: Source + Hash + PartialEq + Eq + 'static> Hash for CachedSource<T> {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
(self.cached_hash.get_or_init(|| {
let mut hasher = FxHasher::default();
hasher.write(self.source().as_bytes());
hasher.finish()
}))
.hash(state);
}
}
impl<T: PartialEq> PartialEq for CachedSource<T> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<T: Eq> Eq for CachedSource<T> {}
impl<T: std::fmt::Debug> std::fmt::Debug for CachedSource<T> {
fn fmt(
&self,
f: &mut std::fmt::Formatter<'_>,
) -> Result<(), std::fmt::Error> {
f.debug_struct("CachedSource")
.field("inner", self.inner.as_ref())
.field("cached_buffer", &self.cached_buffer.get().is_some())
.field("cached_source", &self.cached_source.get().is_some())
.field("cached_maps", &(!self.cached_maps.is_empty()))
.finish()
}
}
#[cfg(test)]
mod tests {
use std::borrow::Borrow;
use crate::{
ConcatSource, OriginalSource, RawSource, SourceExt, SourceMapSource,
WithoutOriginalOptions,
};
use super::*;
#[test]
fn line_number_should_not_add_one() {
let source = ConcatSource::new([
CachedSource::new(RawSource::from("\n")).boxed(),
SourceMapSource::new(WithoutOriginalOptions {
value: "\nconsole.log(1);\n".to_string(),
name: "index.js".to_string(),
source_map: SourceMap::new(
";AACA",
vec!["index.js".into()],
vec!["// DELETE IT\nconsole.log(1)".into()],
vec![],
),
})
.boxed(),
]);
let map = source.map(&Default::default()).unwrap();
assert_eq!(map.mappings(), ";;AACA");
}
#[test]
fn should_allow_to_store_and_share_cached_data() {
let original = OriginalSource::new("Hello World", "test.txt");
let source = CachedSource::new(original);
let clone = source.clone();
// fill up cache
let map_options = MapOptions::default();
source.source();
source.buffer();
source.size();
source.map(&map_options);
assert_eq!(clone.cached_source.get().unwrap().borrow(), source.source());
assert_eq!(
*clone.cached_buffer.get().unwrap(),
source.buffer().to_vec()
);
assert_eq!(
*clone.cached_maps.get(&map_options).unwrap().value(),
source.map(&map_options).map(|map| map.into_owned())
);
}
#[test]
fn should_return_the_correct_size_for_binary_files() {
let source = OriginalSource::new(
String::from_utf8(vec![0; 256]).unwrap(),
"file.wasm",
);
let cached_source = CachedSource::new(source);
assert_eq!(cached_source.size(), 256);
assert_eq!(cached_source.size(), 256);
}
#[test]
fn should_return_the_correct_size_for_cached_binary_files() {
let source = OriginalSource::new(
String::from_utf8(vec![0; 256]).unwrap(),
"file.wasm",
);
let cached_source = CachedSource::new(source);
cached_source.source();
assert_eq!(cached_source.size(), 256);
assert_eq!(cached_source.size(), 256);
}
#[test]
fn should_return_the_correct_size_for_text_files() {
let source = OriginalSource::new("TestTestTest", "file.js");
let cached_source = CachedSource::new(source);
assert_eq!(cached_source.size(), 12);
assert_eq!(cached_source.size(), 12);
}
#[test]
fn should_return_the_correct_size_for_cached_text_files() {
let source = OriginalSource::new("TestTestTest", "file.js");
let cached_source = CachedSource::new(source);
cached_source.source();
assert_eq!(cached_source.size(), 12);
assert_eq!(cached_source.size(), 12);
}
#[test]
fn should_produce_correct_output_for_cached_raw_source() {
let map_options = MapOptions {
columns: true,
final_source: true,
};
let source = RawSource::from("Test\nTest\nTest\n");
let mut on_chunk_count = 0;
let mut on_source_count = 0;
let mut on_name_count = 0;
let generated_info = source.stream_chunks(
&map_options,
&mut |_chunk, _mapping| {
on_chunk_count += 1;
},
&mut |_source_index, _source, _source_content| {
on_source_count += 1;
},
&mut |_name_index, _name| {
on_name_count += 1;
},
);
let cached_source = CachedSource::new(source);
cached_source.stream_chunks(
&map_options,
&mut |_chunk, _mapping| {},
&mut |_source_index, _source, _source_content| {},
&mut |_name_index, _name| {},
);
let mut cached_on_chunk_count = 0;
let mut cached_on_source_count = 0;
let mut cached_on_name_count = 0;
let cached_generated_info = cached_source.stream_chunks(
&map_options,
&mut |_chunk, _mapping| {
cached_on_chunk_count += 1;
},
&mut |_source_index, _source, _source_content| {
cached_on_source_count += 1;
},
&mut |_name_index, _name| {
cached_on_name_count += 1;
},
);
assert_eq!(on_chunk_count, cached_on_chunk_count);
assert_eq!(on_source_count, cached_on_source_count);
assert_eq!(on_name_count, cached_on_name_count);
assert_eq!(generated_info, cached_generated_info);
}
#[test]
fn should_have_correct_buffer_if_cache_buffer_from_cache_source() {
let buf = vec![128u8];
let source = CachedSource::new(RawSource::from(buf.clone()));
source.source();
assert_eq!(source.buffer(), buf.as_slice());
}
}