Skip to content

[Repo Assist] perf: reuse stable temp file per document to reduce filesystem overhead#135

Draft
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-stable-tmpfile-per-document-0969dbb4a0a99fff
Draft

[Repo Assist] perf: reuse stable temp file per document to reduce filesystem overhead#135
github-actions[bot] wants to merge 1 commit intomasterfrom
repo-assist/perf-stable-tmpfile-per-document-0969dbb4a0a99fff

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

@github-actions github-actions bot commented Apr 6, 2026

🤖 This is an automated PR from Repo Assist.

Problem

format() generates a fresh random temp file name on every save:

let fileName = TmpDir + "/temp-" + Math.random().toString(36)+ ".php";
fs.writeFileSync(fileName, text);
// …phpcbf runs…
fs.unlink(fileName, function(err) {});

This means every save causes:

  1. A Math.random() call + several string operations to build the name
  2. A new filesystem inode allocation for the new file
  3. An async unlink (inode deletion) after each run

For users who save frequently (e.g. on every keystroke via editor.formatOnSave), this creates constant inode churn in the OS temp directory.

Fix

Allocate one stable temp file path per document (keyed by document.uri.toString()) on first use, and overwrite it in-place on subsequent saves. Temp file names are now phpcbf-(pid)-(n).php, scoped to the running process for easy identification.

Lifecycle:

  • Temp file is created lazily on the first format() call for a document
  • Cleaned up in onDidCloseTextDocument when the user closes the tab
  • All remaining temp files are removed synchronously in deactivate()

Trade-offs

  • Slightly more memory (one Map entry + one file on disk per open PHP document); negligible in practice
  • If VS Code crashes without calling deactivate(), a small number of temp files persist — same as the previous code, and the OS temp cleaner handles these

Test Status

Unit tests (node --test test/unit.test.js): 7/7 passing
Syntax check (node --check extension.js): OK
No CI workflow exists in this repository; all checks are manual.

Generated by Repo Assist ·

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@346204513ecfa08b81566450d7d599556807389f

Instead of generating a random temp file name on every format call and
deleting it afterwards, allocate one stable temp file path per document
(keyed by document URI) on first use and overwrite it in-place on
subsequent saves.

Benefits:
- Eliminates Math.random() + string manipulation on every save
- Replaces create-then-delete with a single in-place overwrite, reducing
  OS inode churn when the user saves frequently
- Predictable, PID-scoped names (phpcbf-<pid>-<n>.php) make temp files
  easier to identify and clean up if the process crashes

Lifecycle:
- Temp file is created on the first format of each document
- Deleted in the onDidCloseTextDocument listener when the document closes
- All remaining temp files are deleted synchronously in deactivate()

The _phpcbf reference on exports is only for deactivation; it is not
part of the public API.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants