Skip to content

Commit 6f2089f

Browse files
authored
perf: optimize decode mappings (#101)
* reserve vec and remove clone * remove reserve * use arrayvec * Fix clippy
1 parent a64c3e4 commit 6f2089f

File tree

5 files changed

+19
-12
lines changed

5 files changed

+19
-12
lines changed

Cargo.lock

Lines changed: 7 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ dyn-clone = "1"
3636
rustc-hash = "1"
3737
dashmap = "5"
3838
substring = "1"
39-
smallvec = "1.11.2"
4039
memchr = "2.6.4"
4140
str_indices = "0.4.3"
4241

4342
codspeed-criterion-compat = { version = "2.3.3", default-features = false, optional = true }
43+
arrayvec = "0.7.4"
4444

4545
[dev-dependencies]
4646
twox-hash = "1"

src/helpers.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use arrayvec::ArrayVec;
12
use rustc_hash::FxHashMap as HashMap;
23
use std::{
34
borrow::{BorrowMut, Cow},
@@ -136,7 +137,7 @@ pub struct SegmentIter<'a> {
136137
original_column: u32,
137138
name_index: u32,
138139
line: &'a str,
139-
nums: Vec<i64>,
140+
nums: ArrayVec<i64, 5>,
140141
segment_cursor: usize,
141142
}
142143

@@ -152,7 +153,7 @@ impl<'a> SegmentIter<'a> {
152153
generated_line: 0,
153154
segment_cursor: 0,
154155
generated_column: 0,
155-
nums: Vec::with_capacity(5),
156+
nums: ArrayVec::new(),
156157
}
157158
}
158159

@@ -652,7 +653,7 @@ fn stream_chunks_of_source_map_final(
652653
}
653654
};
654655
for mapping in source_map.decoded_mappings() {
655-
on_mapping(&mapping);
656+
on_mapping(mapping);
656657
}
657658
result
658659
}
@@ -793,7 +794,7 @@ fn stream_chunks_of_source_map_full(
793794
};
794795

795796
for mapping in source_map.decoded_mappings() {
796-
on_mapping(&mapping);
797+
on_mapping(mapping);
797798
}
798799
on_mapping(&Mapping {
799800
generated_line: final_line,
@@ -856,7 +857,7 @@ fn stream_chunks_of_source_map_lines_final(
856857
}
857858
};
858859
for mapping in source_map.decoded_mappings() {
859-
on_mapping(&mapping);
860+
on_mapping(mapping);
860861
}
861862
result
862863
}
@@ -924,7 +925,7 @@ fn stream_chunks_of_source_map_lines_full(
924925
}
925926
};
926927
for mapping in source_map.decoded_mappings() {
927-
on_mapping(&mapping);
928+
on_mapping(mapping);
928929
}
929930
while current_generated_line as usize <= lines.len() {
930931
on_chunk(

src/source.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,12 +232,10 @@ impl SourceMap {
232232
}
233233

234234
/// Get the decoded mappings in [SourceMap].
235-
pub fn decoded_mappings(&'_ self) -> impl IntoIterator<Item = Mapping> + '_ {
235+
pub fn decoded_mappings(&'_ self) -> &[Mapping] {
236236
self
237237
.decoded_mappings
238238
.get_or_init(|| decode_mappings(self).collect::<Vec<_>>())
239-
.clone()
240-
.into_iter()
241239
}
242240

243241
/// Get the mappings string in [SourceMap].

src/vlq.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Implements utilities for dealing with the sourcemap vlq encoding.
22
//! forked from [rust-sourcemap](https://github.com/getsentry/rust-sourcemap/blob/851f12bfa6c4cf2c737b94734b27f7d9bfb4de86/src/vlq.rs)
33
4+
use arrayvec::ArrayVec;
5+
46
use crate::{Error, Result};
57

68
const B64_CHARS: &[u8] =
@@ -265,7 +267,7 @@ const B64: [i8; 256] = [
265267
];
266268

267269
/// Parses a VLQ segment into a pre-allocated `Vec` instead of returning a new allocation.
268-
pub fn decode(segment: &str, rv: &mut Vec<i64>) -> Result<()> {
270+
pub fn decode(segment: &str, rv: &mut ArrayVec<i64, 5>) -> Result<()> {
269271
let mut cur = 0;
270272
let mut shift = 0;
271273

0 commit comments

Comments
 (0)