[tools] internal/imports: read the module index only where it is used

3 views
Skip to first unread message

Brad Moylan (Gerrit)

unread,
Aug 6, 2026, 5:48:19 PM (yesterday) Aug 6
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Brad Moylan has uploaded the change for review

Commit message

internal/imports: read the module index only where it is used

getFixesWithSource read the GOMODCACHE index on entry, but ix is not
used until the third pass, after three early returns. A file whose
imports are already complete returns from the first pass, having read
and parsed the entire index for nothing.

The cost is per file, not per run: the imports.Process wrapper builds a
fresh ProcessEnv for each call and modindex.Read is not memoized, so a
tool that formats a tree re-reads the index once per file. With a 60MB
index, goimports -l over 500 already-formatted files in x/tools took
41.5s and 233MB peak RSS; reading the index at its point of first use
instead brings that to 0.47s and 20MB.

Files that do need an import resolved still read the index and are
unaffected. Verified by comparing output before and after over 8220
files, and over a mutation corpus of 250 files in a non-vendored module
with one third-party import deleted from each, which exercises
addExternalCandidates; output is identical in every case.

This applies a change requested during review of CL 767881 (patch set
23) that was acknowledged but did not reach the submitted revision.

Fixes golang/go#80774
Change-Id: I6a7226bd70fa1a12afa3ca9e6704ed28d4f5e419

Change diff

diff --git a/internal/imports/fix.go b/internal/imports/fix.go
index 6aa1e27..274f635 100644
--- a/internal/imports/fix.go
+++ b/internal/imports/fix.go
@@ -579,17 +579,6 @@
}

func getFixesWithSource(ctx context.Context, fset *token.FileSet, f *ast.File, filename string, goroot string, logf func(string, ...any), source Source) ([]*ImportFix, error) {
- // If there is an Index for the GOMODCACHE, remember that, and later make it so that the
- // directory walk doesn't go into the module cache, since we already have all the information
- var ix *modindex.Index
- src, ok := source.(*ProcessEnvSource)
- if ok {
- var err error
- if ix, err = modindex.Read(src.env.Env["GOMODCACHE"]); err != nil {
- ix = nil // don't use it if there was an error
- }
- }
-
// This logic is defensively duplicated from getFixes.
abs, err := filepath.Abs(filename)
if err != nil {
@@ -648,6 +637,17 @@
}
p.loadRealPackageNames = true
p.otherFiles = otherFiles
+
+ // If there is an Index for the GOMODCACHE, remember that, and later make it so that the
+ // directory walk doesn't go into the module cache, since we already have all the information.
+ var ix *modindex.Index
+ if src, ok := source.(*ProcessEnvSource); ok {
+ var err error
+ if ix, err = modindex.Read(src.env.Env["GOMODCACHE"]); err != nil {
+ ix = nil // don't use it if there was an error
+ }
+ }
+
if ix != nil {
src, ok := p.source.(*ProcessEnvSource)
if ok {

Change information

Files:
  • M internal/imports/fix.go
Change size: S
Delta: 1 file changed, 11 insertions(+), 11 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I6a7226bd70fa1a12afa3ca9e6704ed28d4f5e419
Gerrit-Change-Number: 811840
Gerrit-PatchSet: 1
Gerrit-Owner: Brad Moylan <moyla...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Aug 6, 2026, 5:51:37 PM (yesterday) Aug 6
to Brad Moylan, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Message from Gopher Robot

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: comment
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I6a7226bd70fa1a12afa3ca9e6704ed28d4f5e419
Gerrit-Change-Number: 811840
Gerrit-PatchSet: 1
Gerrit-Owner: Brad Moylan <moyla...@gmail.com>
Gerrit-CC: Gopher Robot <go...@golang.org>
Gerrit-Comment-Date: Thu, 06 Aug 2026 21:51:32 +0000
Gerrit-HasComments: No
Gerrit-Has-Labels: No
unsatisfied_requirement
satisfied_requirement
open
diffy

Gerrit Bot (Gerrit)

unread,
Aug 6, 2026, 6:29:57 PM (yesterday) Aug 6
to goph...@pubsubhelper.golang.org, Brad Moylan, golang-co...@googlegroups.com

Gerrit Bot has uploaded the change for review

Commit message

internal/imports: read the module index only where it is used

getFixesWithSource read the GOMODCACHE index on entry, but ix is not used until the third pass, after three early returns. A file whose imports are already complete returns from the first pass, having read and parsed the entire index for nothing.

The cost is per file, not per run: the imports.Process wrapper builds a fresh ProcessEnv for each call and modindex.Read is not memoized, so a tool that formats a tree re-reads the index once per file. With a 60MB index, goimports -l over 500 already-formatted files in x/tools took 41.5s and 233MB peak RSS; reading the index at its point of first use instead brings that to 0.47s and 20MB.

Files that do need an import resolved still read the index and are unaffected. Verified by comparing output before and after over 8220 files, and over a mutation corpus of 250 files in a non-vendored module with one third-party import deleted from each, which exercises addExternalCandidates; output is identical in every case.

This applies a change requested during review of CL 767881 (patch set 23) that was acknowledged but did not reach the submitted revision.

Fixes golang/go#80774
Change-Id: I7187db0b5e797272f8af0afbfc595e1d3e8163d0
GitHub-Last-Rev: 33d391cb18ed5e11074db53f687d89592cb1318c
GitHub-Pull-Request: golang/tools#665
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: tools
Gerrit-Branch: master
Gerrit-Change-Id: I7187db0b5e797272f8af0afbfc595e1d3e8163d0
Gerrit-Change-Number: 811860
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Brad Moylan <moyla...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Gopher Robot (Gerrit)

unread,
Aug 6, 2026, 6:29:58 PM (yesterday) Aug 6
to Brad Moylan, Gerrit Bot, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Gopher Robot added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Gopher Robot . unresolved

I spotted some possible problems with your PR:

  1. You have a long 391 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)

Please address any problems by updating the GitHub PR.

When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

For more details, see:

(In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: tools
    Gerrit-Branch: master
    Gerrit-Change-Id: I7187db0b5e797272f8af0afbfc595e1d3e8163d0
    Gerrit-Change-Number: 811860
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-CC: Brad Moylan <moyla...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-Comment-Date: Thu, 06 Aug 2026 22:29:53 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Brad Moylan (Gerrit)

    unread,
    Aug 6, 2026, 8:40:00 PM (yesterday) Aug 6
    to goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

    Brad Moylan abandoned this change.

    View Change

    Brad Moylan abandoned this change

    Related details

    Attention set is empty
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: abandon
      Gerrit-Project: tools
      Gerrit-Branch: master
      Gerrit-Change-Id: I6a7226bd70fa1a12afa3ca9e6704ed28d4f5e419
      Gerrit-Change-Number: 811840
      Gerrit-PatchSet: 1
      Gerrit-Owner: Brad Moylan <moyla...@gmail.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy

      Gerrit Bot (Gerrit)

      unread,
      Aug 6, 2026, 8:52:54 PM (yesterday) Aug 6
      to Brad Moylan, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

      Gerrit Bot uploaded new patchset

      Gerrit Bot uploaded patch set #2 to this change.
      Open in Gerrit

      Related details

      Attention set is empty
      Submit Requirements:
        • requirement is not satisfiedCode-Review
        • requirement is not satisfiedNo-Unresolved-Comments
        • requirement is not satisfiedReview-Enforcement
        • requirement is not satisfiedTryBots-Pass
        Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
        Gerrit-MessageType: newpatchset
        Gerrit-Project: tools
        Gerrit-Branch: master
        Gerrit-Change-Id: I7187db0b5e797272f8af0afbfc595e1d3e8163d0
        Gerrit-Change-Number: 811860
        Gerrit-PatchSet: 2
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-CC: Brad Moylan <moyla...@gmail.com>
        Gerrit-CC: Gopher Robot <go...@golang.org>
        unsatisfied_requirement
        open
        diffy

        Brad Moylan (Gerrit)

        unread,
        Aug 6, 2026, 10:10:07 PM (yesterday) Aug 6
        to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, golang-co...@googlegroups.com

        Brad Moylan added 1 comment

        Patchset-level comments
        File-level comment, Patchset 1:
        Gopher Robot . resolved

        I spotted some possible problems with your PR:

          1. You have a long 391 character line in the commit message body. Please add line breaks to long lines that should be wrapped. Lines in the commit message body should be wrapped at ~76 characters unless needed for things like URLs or tables. (Note: GitHub might render long lines as soft-wrapped, so double-check in the Gerrit commit message shown above.)

        Please address any problems by updating the GitHub PR.

        When complete, mark this comment as 'Done' and click the [blue 'Reply' button](https://go.dev/wiki/GerritBot#i-left-a-reply-to-a-comment-in-gerrit-but-no-one-but-me-can-see-it) above. These findings are based on heuristics; if a finding does not apply, briefly reply here saying so.

        To update the commit title or commit message body shown here in Gerrit, you must edit the GitHub PR title and PR description (the first comment) in the GitHub web interface using the 'Edit' button or 'Edit' menu entry there. Note: pushing a new commit to the PR will not automatically update the commit message used by Gerrit.

        For more details, see:

        (In general for Gerrit code reviews, the change author is expected to [log in to Gerrit](https://go-review.googlesource.com/login/) with a Gmail or other Google account and then close out each piece of feedback by marking it as 'Done' if implemented as suggested or otherwise reply to each review comment. See the [Review](https://go.dev/doc/contribute#review) section of the Contributing Guide for details.)

        Brad Moylan

        Done

        Open in Gerrit

        Related details

        Attention set is empty
        Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: comment
          Gerrit-Project: tools
          Gerrit-Branch: master
          Gerrit-Change-Id: I7187db0b5e797272f8af0afbfc595e1d3e8163d0
          Gerrit-Change-Number: 811860
          Gerrit-PatchSet: 2
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-CC: Brad Moylan <moyla...@gmail.com>
          Gerrit-CC: Gopher Robot <go...@golang.org>
          Gerrit-Comment-Date: Fri, 07 Aug 2026 02:10:01 +0000
          Gerrit-HasComments: Yes
          Gerrit-Has-Labels: No
          Comment-In-Reply-To: Gopher Robot <go...@golang.org>
          unsatisfied_requirement
          satisfied_requirement
          open
          diffy

          Gerrit Bot (Gerrit)

          unread,
          12:31 PM (10 hours ago) 12:31 PM
          to Brad Moylan, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
          Attention needed from Hongxiang Jiang and Peter Weinberger

          Gerrit Bot uploaded new patchset

          Gerrit Bot uploaded patch set #3 to this change.
          Open in Gerrit

          Related details

          Attention is currently required from:
          • Hongxiang Jiang
          • Peter Weinberger
          Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: newpatchset
          Gerrit-Project: tools
          Gerrit-Branch: master
          Gerrit-Change-Id: I7187db0b5e797272f8af0afbfc595e1d3e8163d0
          Gerrit-Change-Number: 811860
          Gerrit-PatchSet: 3
          Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
          Gerrit-Reviewer: Hongxiang Jiang <hxj...@golang.org>
          Gerrit-Reviewer: Peter Weinberger <p...@google.com>
          Gerrit-CC: Brad Moylan <moyla...@gmail.com>
          Gerrit-CC: Gopher Robot <go...@golang.org>
          Gerrit-Attention: Hongxiang Jiang <hxj...@golang.org>
          Gerrit-Attention: Peter Weinberger <p...@google.com>
          unsatisfied_requirement
          satisfied_requirement
          open
          diffy

          Gerrit Bot (Gerrit)

          unread,
          1:09 PM (9 hours ago) 1:09 PM
          to Brad Moylan, goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
          Attention needed from Hongxiang Jiang and Peter Weinberger

          Gerrit Bot uploaded new patchset

          Gerrit Bot uploaded patch set #4 to this change.
          Open in Gerrit

          Related details

          Attention is currently required from:
          • Hongxiang Jiang
          • Peter Weinberger
          Submit Requirements:
          • requirement is not satisfiedCode-Review
          • requirement satisfiedNo-Unresolved-Comments
          • requirement is not satisfiedReview-Enforcement
          • requirement is not satisfiedTryBots-Pass
          Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
          Gerrit-MessageType: newpatchset
          Gerrit-Project: tools
          Gerrit-Branch: master
          Gerrit-Change-Id: I7187db0b5e797272f8af0afbfc595e1d3e8163d0
          Gerrit-Change-Number: 811860
          Gerrit-PatchSet: 4
          unsatisfied_requirement
          satisfied_requirement
          open
          diffy
          Reply all
          Reply to author
          Forward
          0 new messages