Skip to content

Commit 251d4da

Browse files
authored
perf(wasm): pre-size the arena from the source length (#519)
Both WASM entry points built their bump allocator with Allocator::new() while the NAPI binding already pre-sizes with for_source_len, so the WASM parse grew its arena through repeated chunk doublings on every call. Use the same source-length policy for parseAndRender (raw source) and transform (frontmatter-stripped content slice).
1 parent c4eb2bc commit 251d4da

1 file changed

Lines changed: 5 additions & 2 deletions

File tree

  • crates/ox_content_wasm/src

crates/ox_content_wasm/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,10 @@ impl From<&WasmParserOptions> for ParserOptions {
171171
#[wasm_bindgen(js_name = parseAndRender)]
172172
pub fn parse_and_render(source: &str, options: Option<WasmParserOptions>) -> JsValue {
173173
let opts = options.unwrap_or_default();
174-
let allocator = Allocator::new();
174+
// Pre-size the arena from the source length (same policy as the NAPI
175+
// binding) so the parse+render fits one bump chunk instead of growing
176+
// through repeated chunk doublings.
177+
let allocator = Allocator::for_source_len(source.len());
175178
let parser_options = ParserOptions::from(&opts);
176179
let parser = Parser::with_options(&allocator, source, parser_options);
177180

@@ -212,7 +215,7 @@ pub fn transform(source: &str, options: Option<WasmParserOptions>) -> JsValue {
212215
let (content, frontmatter) = parse_frontmatter(source);
213216

214217
// Parse markdown
215-
let allocator = Allocator::new();
218+
let allocator = Allocator::for_source_len(content.len());
216219
let parser_options = ParserOptions::from(&opts);
217220
let parser = Parser::with_options(&allocator, &content, parser_options);
218221

0 commit comments

Comments
 (0)