Skip to content

Format a single file on the calling thread, without a thread pool #227

Open

Description

@abashev

What happens

Main.formatFiles creates a fixed pool of min(20, files) threads whatever the number of files, so a single file is read on the main thread, formatted on a pool thread and taken back with Future.get. Standard input already formats on the calling thread: formatStdin calls FormatFileCallable.call() directly.

One file on the command line is the common case: the pre-commit hook and the GitHub Action run the native binary once for each file, as --dry-run --set-exit-if-changed "$FILE", and code that runs Main in process creates and closes a pool on every call.

What the pool costs

Little. The 21.0.2 native binary on java/util/ArrayList.java of JDK 21, median of 30 runs: 111.0 ms with the file as an argument, 110.6 ms with it on standard input. The gain is one code path less and no thread to start and stop, not time.

Proposal

With one Java file left after the others are skipped, format it on the calling thread, as formatStdin does, and report its errors as the pooled path does: the diagnostics of a FormatterException, the message and stack trace of any other exception.

google-java-format

The same: formatFiles creates min(MAX_THREADS, files) threads for one file too.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions