Skip to content

Commit 47ffc05

Browse files
committed
optimize concat
1 parent 38f4ce4 commit 47ffc05

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

src/compiler/utils/string_with_sourcemap.ts

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ export function sourcemap_add_offset(
3737
} as SourceMappings;
3838
}
3939

40-
function merge_tables<T>(this_table: T[], other_table): [T[], number[]] {
40+
function merge_tables<T>(this_table: T[], other_table): [T[], number[], boolean] {
4141
const new_table = this_table.slice();
4242
const idx_map = [];
4343
other_table = other_table || [];
44+
let has_changed = false;
4445
for (const [other_idx, other_val] of other_table.entries()) {
4546
const this_idx = this_table.indexOf(other_val);
4647
if (this_idx >= 0) {
@@ -49,9 +50,10 @@ function merge_tables<T>(this_table: T[], other_table): [T[], number[]] {
4950
const new_idx = new_table.length;
5051
new_table[new_idx] = other_val;
5152
idx_map[other_idx] = new_idx;
53+
has_changed = true;
5254
}
5355
}
54-
return [new_table, idx_map];
56+
return [new_table, idx_map, has_changed];
5557
}
5658

5759
export class StringWithSourcemap {
@@ -69,18 +71,20 @@ export class StringWithSourcemap {
6971
if (other.string == '') return this;
7072

7173
// combine sources and names
72-
const [sources, new_source_idx] = merge_tables(this.map.sources, other.map.sources);
73-
const [names, new_name_idx] = merge_tables(this.map.names, other.map.names);
74+
const [sources, new_source_idx, sources_changed] = merge_tables(this.map.sources, other.map.sources);
75+
const [names, new_name_idx, names_changed] = merge_tables(this.map.names, other.map.names);
7476

7577
// update source refs and name refs
76-
const other_mappings = other.map.mappings.map((line) =>
77-
line.map(seg => {
78-
const new_seg = seg.slice() as MappingSegment;
79-
if (seg[1]) new_seg[1] = new_source_idx[seg[1]];
80-
if (seg[4]) new_seg[4] = new_name_idx[seg[4]];
81-
return new_seg;
82-
})
83-
);
78+
const other_mappings =
79+
(sources_changed || names_changed)
80+
? other.map.mappings.slice().map(line =>
81+
line.map(seg => {
82+
if (seg[1]) seg[1] = new_source_idx[seg[1]];
83+
if (seg[4]) seg[4] = new_name_idx[seg[4]];
84+
return seg;
85+
})
86+
)
87+
: other.map.mappings;
8488

8589
// combine the mappings
8690

@@ -89,17 +93,18 @@ export class StringWithSourcemap {
8993
// 2. first line of second map
9094
// columns of 2 must be shifted
9195

92-
const col_offset = last_line_length(this.string);
96+
const column_offset = last_line_length(this.string);
9397

9498
const first_line: MappingSegment[] =
9599
other_mappings.length == 0
96100
? []
97-
: col_offset == 0
101+
: column_offset == 0
98102
? other_mappings[0].slice() as MappingSegment[]
99-
: other_mappings[0].map(seg => (
103+
: other_mappings[0].slice().map(seg => {
100104
// shift column
101-
[seg[0] + col_offset].concat(seg.slice(1)) as MappingSegment
102-
));
105+
seg[0] += column_offset;
106+
return seg;
107+
});
103108

104109
const mappings: MappingSegment[][] =
105110
this.map.mappings.slice(0, -1)

0 commit comments

Comments
 (0)