Skip to content

perf: reduce allocations when formatting and resolving files - #1354

Merged
dsherret merged 4 commits into
mainfrom
perf-reduce-allocations
Oct 11, 2026
Merged

dsherret merged 4 commits into
mainfrom
perf-reduce-allocations

Conversation

@dsherret

Copy link
Copy Markdown
Member

Removes allocations found while reviewing the CLI's per-file paths. Nothing here was benchmarked; the changes remove copies by construction.

Formatting

  • The file bytes are shared with plugins (Arc<Vec<u8>>) instead of copied for each plugin and each stabilization pass. This applies to dprint fmt/check, the language server, the editor service, --stdin, and host formatting.
  • The slow format warning runs in the file's own task with a select! instead of a spawned task, cancellation token, and path clone for every file (including files the incremental cache skips).
  • The file path is shared for a file instead of cloned several times, and the host format callback and null cancellation token are created once per task instead of per plugin per file.
  • Outputting a diff validates the texts as UTF-8 in place instead of copying both.

Language server

  • The document text and line index are stored behind an Arc, so getting them for a format request doesn't copy them and a change only copies them when a request is still using them.

File and plugin resolution

  • Resolving the plugins for a file returns a ResolvedPluginNames that refers to the plugins instead of allocating a Vec<&str> for every file.
  • DirEntry::File no longer has a name allocated separately from its path.
  • CLI path arguments: slashes are only replaced when there's a backslash, finding the base directory allocates once, checking the directory chain doesn't build a Vec, and in-scope literal paths are deduplicated without cloning.
  • GlobPattern::deepest_base_dir gets the deepest base directory without cloning the pattern.

dprint-core

  • Breaking: ProcessPluginCommunicatorFormatRequest.file_bytes and FormatMessageBody.file_bytes are now Arc<Vec<u8>> so the CLI can give a process plugin the bytes without copying them. Bumped to 0.71.0.

Behavior notes

  • The callback given to run_parallelized takes &Path, &[u8], &[u8] instead of owned values.
  • PluginsScope::format resolves the plugins and writes its debug log when the future is first polled instead of when it's called.

- Share the file bytes with plugins instead of copying them for each
  plugin and each stabilization pass. This changes
  `ProcessPluginCommunicatorFormatRequest.file_bytes` in dprint-core to
  `Arc<Vec<u8>>`, so dprint-core is bumped to 0.71.0.
- Run the slow format warning in the file's own task instead of
  spawning a task for every file.
- Share the document text and line index in the language server instead
  of copying them for each format request and change.
- Don't copy the file texts to output a diff.
- Don't allocate a list of plugin names for every file.
- Don't allocate the name of every file separately from its path when
  reading a directory.
- Do less work for each cli path argument.
@dsherret
dsherret merged commit 9e8d3ef into main Oct 11, 2026
1 check passed
@dsherret
dsherret deleted the perf-reduce-allocations branch October 11, 2026 23:18
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