Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
x/tools/cmd/guru: consider avoiding reading entire file in globalReferrersPkgLevel #24274
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.