Skip to content

x/tools/cmd/guru: consider avoiding reading entire file in globalReferrersPkgLevel #24274

Closed
@josharian

Description

@josharian

cmd/guru/referrers.go:globalReferrersPkgLevel currently reads the entire file into memory, in order to do a bytes.Contains check followed by two separate parses.

If #24273 is fixed, it might be nice to avoid reading the entire file into memory. This could be done by writing a bytes.Contains implementation that works on an io.Writer. When a []byte is written, check for the needle in those bytes. If not found, check for a prefix of the needle at the end of the []byte, and save it for the next Write, and check for corresponding suffix at the beginning. (Extra care must be taken for slices that have repeating prefixes!) And of course, once a match is found, all future Writes can be ignored.

Then globalReferrersPkgLevel could use a TeeReader to write to the streaming bytes.Contains while parsing the underlying file. If the relevant import is missing, bail. If the whole file is parsed, but it doesn't contain the target name, bail. Then inspect the AST.

cc @keegancsmith @alandonovan @DanielMorsing

Metadata

Metadata

Assignees

No one assigned

    Labels

    PerformanceToolsThis label describes issues relating to any tools in the x/tools repository.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions