Skip to content

Commit

Permalink
have a try on arc source (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahabhgk authored Feb 20, 2023
1 parent 5ef0773 commit 493d395
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
9 changes: 5 additions & 4 deletions src/cached_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ use crate::{helpers::StreamChunks, MapOptions, Source, SourceMap};
/// ```
/// use rspack_sources::{
/// BoxSource, CachedSource, ConcatSource, MapOptions, OriginalSource,
/// RawSource, Source, SourceMap,
/// RawSource, Source, SourceExt, SourceMap,
/// };
///
/// let mut concat = ConcatSource::new([
/// Box::new(RawSource::from("Hello World\n".to_string())) as BoxSource,
/// Box::new(OriginalSource::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"));
///
Expand Down
39 changes: 21 additions & 18 deletions src/concat_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ use crate::{
/// ```
/// use rspack_sources::{
/// BoxSource, ConcatSource, MapOptions, OriginalSource, RawSource, Source,
/// SourceMap,
/// SourceExt, SourceMap,
/// };
///
/// let mut source = ConcatSource::new([
/// Box::new(RawSource::from("Hello World\n".to_string())) as BoxSource,
/// Box::new(OriginalSource::new(
/// RawSource::from("Hello World\n".to_string()).boxed(),
/// OriginalSource::new(
/// "console.log('test');\nconsole.log('test2');\n",
/// "console.js",
/// )),
/// )
/// .boxed(),
/// ]);
/// source.add(OriginalSource::new("Hello2\n", "hello.md"));
///
Expand Down Expand Up @@ -279,11 +280,12 @@ mod tests {
#[test]
fn should_concat_two_sources() {
let mut source = ConcatSource::new([
Box::new(RawSource::from("Hello World\n".to_string())) as BoxSource,
Box::new(OriginalSource::new(
RawSource::from("Hello World\n".to_string()).boxed(),
OriginalSource::new(
"console.log('test');\nconsole.log('test2');\n",
"console.js",
)),
)
.boxed(),
]);
source.add(OriginalSource::new("Hello2\n", "hello.md"));

Expand Down Expand Up @@ -328,11 +330,12 @@ mod tests {
#[test]
fn should_be_able_to_handle_strings_for_all_methods() {
let mut source = ConcatSource::new([
Box::new(RawSource::from("Hello World\n".to_string())) as BoxSource,
Box::new(OriginalSource::new(
RawSource::from("Hello World\n".to_string()).boxed(),
OriginalSource::new(
"console.log('test');\nconsole.log('test2');\n",
"console.js",
)),
)
.boxed(),
]);
let inner_source =
ConcatSource::new([RawSource::from("("), "'string'".into(), ")".into()]);
Expand Down Expand Up @@ -382,14 +385,14 @@ mod tests {
#[test]
fn should_allow_to_concatenate_in_a_single_line() {
let source = ConcatSource::new([
Box::new(OriginalSource::new("Hello", "hello.txt")) as BoxSource,
Box::new(RawSource::from(" ")),
Box::new(OriginalSource::new("World ", "world.txt")),
Box::new(RawSource::from("is here\n")),
Box::new(OriginalSource::new("Hello\n", "hello.txt")),
Box::new(RawSource::from(" \n")),
Box::new(OriginalSource::new("World\n", "world.txt")),
Box::new(RawSource::from("is here")),
OriginalSource::new("Hello", "hello.txt").boxed(),
RawSource::from(" ").boxed(),
OriginalSource::new("World ", "world.txt").boxed(),
RawSource::from("is here\n").boxed(),
OriginalSource::new("Hello\n", "hello.txt").boxed(),
RawSource::from(" \n").boxed(),
OriginalSource::new("World\n", "world.txt").boxed(),
RawSource::from("is here").boxed(),
]);

assert_eq!(
Expand Down
9 changes: 5 additions & 4 deletions src/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
convert::{TryFrom, TryInto},
fmt,
hash::{Hash, Hasher},
sync::Arc,
};

use dyn_clone::DynClone;
Expand All @@ -15,7 +16,7 @@ use crate::{
};

/// An alias for [Box<dyn Source>].
pub type BoxSource = Box<dyn Source>;
pub type BoxSource = Arc<dyn Source>;

/// [Source] abstraction, [webpack-sources docs](https://github.com/webpack/webpack-sources/#source).
pub trait Source:
Expand All @@ -42,7 +43,7 @@ pub trait Source:
fn to_writer(&self, writer: &mut dyn std::io::Write) -> std::io::Result<()>;
}

impl Source for Box<dyn Source> {
impl Source for BoxSource {
fn source(&self) -> Cow<str> {
self.as_ref().source()
}
Expand All @@ -66,7 +67,7 @@ impl Source for Box<dyn Source> {

dyn_clone::clone_trait_object!(Source);

impl StreamChunks for Box<dyn Source> {
impl StreamChunks for BoxSource {
fn stream_chunks(
&self,
options: &MapOptions,
Expand Down Expand Up @@ -137,7 +138,7 @@ pub trait SourceExt {

impl<T: Source + 'static> SourceExt for T {
fn boxed(self) -> BoxSource {
Box::new(self)
Arc::new(self)
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/source_map_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ impl StreamChunks for SourceMapSource {
#[cfg(test)]
mod tests {
use crate::{
BoxSource, ConcatSource, OriginalSource, RawSource, ReplaceSource,
SourceExt,
ConcatSource, OriginalSource, RawSource, ReplaceSource, SourceExt,
};

use super::*;
Expand All @@ -181,10 +180,9 @@ mod tests {
fn map_correctly() {
let inner_source_code = "Hello World\nis a test string\n";
let inner_source = ConcatSource::new([
Box::new(OriginalSource::new(inner_source_code, "hello-world.txt"))
as BoxSource,
Box::new(OriginalSource::new("Translate: ", "header.txt")),
Box::new(RawSource::from("Other text")),
OriginalSource::new(inner_source_code, "hello-world.txt").boxed(),
OriginalSource::new("Translate: ", "header.txt").boxed(),
RawSource::from("Other text").boxed(),
]);
let source_r_code =
"Translated: Hallo Welt\nist ein test Text\nAnderer Text";
Expand Down

1 comment on commit 493d395

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 493d395 Previous: c876866 Ratio
benchmark_concat_generate_base64 26769 ns/iter (± 60) 26697 ns/iter (± 155) 1.00
benchmark_concat_generate_base64_with_cache 17577 ns/iter (± 126) 17651 ns/iter (± 111) 1.00
benchmark_concat_generate_string 12127 ns/iter (± 85) 11979 ns/iter (± 42) 1.01
benchmark_concat_generate_string_with_cache 3062 ns/iter (± 32) 2954 ns/iter (± 16) 1.04

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.