Skip to content

cmd/go: store package metadata in build cache #31417

Closed
@jayconrod

Description

@jayconrod

The Go command currently spends a lot of time gathering package metadata using go/build.Import and cmd/go/internal/imports.ScanDir. Both functions read and parse .go files in directories in order to gather imports and evaluate build constraints. This is expensive both in I/O and CPU time. Source files rarely change, especially in the module cache.

We should investigate saving the package metadata we gather in the build cache. The cache key for a package would be derived from readdir on a package directory and stat on each file in the directory. If a file has changed, the stat metadata will almost certainly change as well: the modification time will be different, probably also the size.

We should take care to avoid hazards with mtime comparison. In particular, we should avoid caching stat data that is only a few seconds old because some file systems have low time resolution, and there is a window where several changes can be made without changing the recorded mtime. We may want to enable this only on certain file systems, operating systems, or only for the module cache.

An initial prototype (CL 75295) shows we can reduce go list time by roughly 20%. That may not be a big enough difference to justify added complexity, but profiling shows that more improvement may be possible if we use the cache for module loading and optimize deserialization.

Activity

added this to the Go1.13 milestone on Apr 11, 2019
self-assigned this
on Apr 11, 2019
modified the milestones: Go1.13, Go1.14 on Jul 8, 2019
modified the milestones: Go1.14, Backlog on Oct 9, 2019
removed their assignment
on Oct 14, 2021
added
NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
on Oct 15, 2021
bcmills

bcmills commented on Jul 1, 2022

@bcmills
Contributor

Oh, hey, @matloob implemented this for Go 1.19 in CL 413815! 😅

modified the milestones: Backlog, Go1.19 on Jul 1, 2022
locked and limited conversation to collaborators on Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

FrozenDueToAgeGoCommandcmd/goNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.ToolSpeed

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @toothrot@rsc@jayconrod@andybons@bcmills

      Issue actions

        cmd/go: store package metadata in build cache · Issue #31417 · golang/go