Skip to content

perf: reduce allocations - #68

Merged
dsherret merged 2 commits into
mainfrom
perf/reduce-allocations
Sep 15, 2026
Merged

dsherret merged 2 commits into
mainfrom
perf/reduce-allocations

Conversation

@dsherret

Copy link
Copy Markdown
Member

Reduces allocations when generating print items and when sorting a package.json, without changing output.

  • push_str instead of push_string(x.to_string()) for strings, property names and numbers, so no throwaway String is created before dprint-core copies it into its bump allocator.
  • true, false, null and , use sc! string containers, so their width is computed at compile time and nothing is allocated per value.
  • Comma separated values take an ExactSizeIterator instead of collecting a Vec<Option<Node>> for every array and object. The None case was never used.
  • The ignore comment check reuses the leading comments lookup, and dangling comment handling returns early before its line lookup when there are no comments.
  • package.json sorting uses PropertySort::by_key, which caches keys, instead of comparators that decoded both property names into Strings on every comparison.
  • The .jsonc extension check uses eq_ignore_ascii_case instead of allocating a lowercased string.

Benchmark

Median of 15 runs, comparing main against this branch; the output is asserted identical.

Input Time Allocations
~4.7 MB jsonc 894ms → 885ms (within noise) 2,470,190 → 1,490,174 (-39.7%)
package.json with 3000 dependencies 6.18ms → 4.66ms (-24.6%) 78,394 → 69,368 (-11.5%)

On the large file most of the time is spent in dprint-core's printer (~65%) and in parsing, so the allocation drop doesn't show up as a measurable speedup there.

@dsherret
dsherret merged commit ff6a39f into main Sep 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant