Skip to content

Commit 05ca0a9

Browse files
committed
Owned instance
1 parent f3cabc9 commit 05ca0a9

File tree

6 files changed

+51
-17
lines changed

6 files changed

+51
-17
lines changed

turbopack/crates/turbo-tasks-fs/src/rope.rs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use serde_bytes::ByteBuf;
1818
use tokio::io::{AsyncRead, ReadBuf};
1919
use triomphe::Arc;
2020
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};
21-
use unsize::{CoerceUnsize, Coercion};
2221

2322
static EMPTY_BUF: &[u8] = &[];
2423

@@ -42,7 +41,7 @@ pub struct Rope {
4241
/// An Arc container for ropes. This indirection allows for easily sharing the
4342
/// contents between Ropes (and also RopeBuilders/RopeReaders).
4443
#[derive(Clone, Debug)]
45-
struct InnerRope(Arc<[RopeElem]>);
44+
struct InnerRope(Arc<Vec<RopeElem>>);
4645

4746
/// Differentiates the types of stored bytes in a rope.
4847
#[derive(Clone, Debug)]
@@ -117,6 +116,10 @@ impl Rope {
117116
pub fn to_bytes(&self) -> Cow<'_, [u8]> {
118117
self.data.to_bytes(self.length)
119118
}
119+
120+
pub fn into_bytes(self) -> Cow<'static, [u8]> {
121+
self.data.into_bytes(self.length)
122+
}
120123
}
121124

122125
impl From<Vec<u8>> for Rope {
@@ -142,7 +145,7 @@ impl<T: Into<Bytes>> From<T> for Rope {
142145
} else {
143146
Rope {
144147
length: bytes.len(),
145-
data: InnerRope(Arc::from([Local(bytes)]).unsize(Coercion::to_slice())),
148+
data: InnerRope(Arc::from(vec![Local(bytes)])),
146149
}
147150
}
148151
}
@@ -536,11 +539,33 @@ impl InnerRope {
536539
}
537540
}
538541
}
542+
543+
fn into_bytes(mut self, len: usize) -> Cow<'static, [u8]> {
544+
if self.0.is_empty() {
545+
return Cow::Borrowed(EMPTY_BUF);
546+
} else if self.0.len() == 1 {
547+
let data = Arc::try_unwrap(self.0);
548+
match data {
549+
Ok(data) => {
550+
return data.into_iter().next().unwrap().into_bytes(len);
551+
}
552+
Err(data) => {
553+
self.0 = data;
554+
}
555+
}
556+
}
557+
558+
let mut read = RopeReader::new(&self, 0);
559+
let mut buf = Vec::with_capacity(len);
560+
read.read_to_end(&mut buf)
561+
.expect("read of rope cannot fail");
562+
Cow::Owned(buf)
563+
}
539564
}
540565

541566
impl Default for InnerRope {
542567
fn default() -> Self {
543-
InnerRope(Arc::new([]).unsize(Coercion::to_slice()))
568+
InnerRope(Arc::new(vec![]))
544569
}
545570
}
546571

@@ -579,7 +604,7 @@ impl From<Vec<RopeElem>> for InnerRope {
579604
}
580605

581606
impl Deref for InnerRope {
582-
type Target = Arc<[RopeElem]>;
607+
type Target = Arc<Vec<RopeElem>>;
583608

584609
fn deref(&self) -> &Self::Target {
585610
&self.0
@@ -610,6 +635,13 @@ impl RopeElem {
610635
_ => None,
611636
}
612637
}
638+
639+
fn into_bytes(self, len: usize) -> Cow<'static, [u8]> {
640+
match self {
641+
Local(bytes) => bytes,
642+
Shared(inner) => inner.into_bytes(len),
643+
}
644+
}
613645
}
614646

615647
impl DeterministicHash for RopeElem {

turbopack/crates/turbopack-browser/src/ecmascript/content.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl EcmascriptBrowserChunkContent {
128128
let mut code = code.build();
129129

130130
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
131-
code = minify(&code, source_maps, mangle)?;
131+
code = minify(code, source_maps, mangle)?;
132132
}
133133

134134
Ok(code.cell())

turbopack/crates/turbopack-browser/src/ecmascript/evaluate/chunk.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ impl EcmascriptBrowserEvaluateChunk {
194194
let mut code = code.build();
195195

196196
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
197-
code = minify(&code, source_maps, mangle)?;
197+
code = minify(code, source_maps, mangle)?;
198198
}
199199

200200
Ok(code.cell())

turbopack/crates/turbopack-core/src/code_builder.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ impl Code {
3434
pub fn has_source_map(&self) -> bool {
3535
!self.mappings.is_empty()
3636
}
37+
38+
/// Take the source code out of the Code.
39+
pub fn into_source_code(self) -> Rope {
40+
self.code
41+
}
3742
}
3843

3944
/// CodeBuilder provides a mutable container to append source code.

turbopack/crates/turbopack-ecmascript/src/minify.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ use turbopack_core::{
3131
use crate::parse::generate_js_source_map;
3232

3333
#[instrument(level = Level::INFO, skip_all)]
34-
pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
34+
pub fn minify(code: Code, source_maps: bool, mangle: Option<MangleType>) -> Result<Code> {
3535
let source_maps = source_maps
3636
.then(|| code.generate_source_map_ref())
3737
.transpose()?;
3838

39+
let source_code = code.into_source_code().into_bytes().into_owned();
40+
let source_code = String::from_utf8(source_code)?;
41+
3942
let cm = Arc::new(SwcSourceMap::new(FilePathMapping::empty()));
4043
let (src, mut src_map_buf) = {
41-
let fm = cm.new_source_file(
42-
FileName::Anon.into(),
43-
code.source_code().to_str()?.into_owned(),
44-
);
44+
let fm = cm.new_source_file(FileName::Anon.into(), source_code);
4545

4646
let lexer = Lexer::new(
4747
Syntax::default(),
@@ -57,10 +57,7 @@ pub fn minify(code: &Code, source_maps: bool, mangle: Option<MangleType>) -> Res
5757
Ok(program) => program,
5858
Err(err) => {
5959
err.into_diagnostic(handler).emit();
60-
bail!(
61-
"failed to parse source code\n{}",
62-
code.source_code().to_str()?
63-
)
60+
bail!("failed to parse source code\n{}", fm.src)
6461
}
6562
};
6663
let comments = SingleThreadedComments::default();

turbopack/crates/turbopack-nodejs/src/ecmascript/node/content.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ impl EcmascriptBuildNodeChunkContent {
7878
let mut code = code.build();
7979

8080
if let MinifyType::Minify { mangle } = this.chunking_context.await?.minify_type() {
81-
code = minify(&code, source_maps, mangle)?;
81+
code = minify(code, source_maps, mangle)?;
8282
}
8383

8484
Ok(code.cell())

0 commit comments

Comments
 (0)