Skip to content

perf: collapse_sourcemaps has excessive intermediate allocations #8632

@Boshen

Description

@Boshen

Context

In crates/rolldown_sourcemap/src/lib.rs:52-120, the collapse_sourcemaps function has three sources of unnecessary intermediate allocations:

1. Sourcemap + lookup table pairs collected into Vec (line 63-66)

let sourcemap_and_lookup_table = chain_without_last
    .iter()
    .map(|sourcemap| (sourcemap, sourcemap.generate_lookup_table()))
    .collect::<Vec<_>>();

This could remain as an iterator or be stored more efficiently since it's only iterated once in reverse.

2. All tokens materialized into Vec (line 79-109)

let tokens = source_view_tokens
    .filter_map(|token| { /* ... token lookup chain ... */ })
    .collect::<Vec<_>>();

The entire token stream is materialized into a Vec<Token> before being passed to SourceMap::new(). For large bundles with thousands of tokens per sourcemap across multiple transform plugins, this is a significant allocation.

3. Three separate collect calls for names, sources, source_contents (lines 113-116)

first_map.get_names().map(Arc::clone).collect::<Vec<_>>(),
first_map.get_sources().map(Arc::clone).collect::<Vec<_>>(),
first_map.get_source_contents().map(|x| x.map(Arc::clone)).collect::<Vec<_>>(),

Each of these creates a separate Vec allocation.

A benchmark exists at crates/rolldown_sourcemap/benches/join.rs but doesn't cover collapse_sourcemaps.

Metadata

Metadata

Assignees

Type

No type

Priority

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions