Skip to content

Commit 4345294

Browse files
Fix slow Go rescan from per-dep go.sum parse (#1424) (#1746)
* Start fix for #1424 Assisted-by: Claude Code:claude-opus-5-5 * Speed up Go rescan on large go.sum files Hosted Go scans got about a third slower in #1403. For each patched dependency the rewriter asks go.sum which versions the build loads, and that tokenized every go.sum line every time. On a project with 1200 modules and 30 patches this was about 30% of the scan's work. Lines that can't belong to the module are now skipped with one prefix check before tokenizing, so the answer stays the same. Tests pin the edge cases: shared path prefixes, /go.mod lines, odd whitespace and short lines. Fixes #1424 Assisted-by: Claude Code:claude-opus-5-5 * Parse each go.sum line once in zip_versions Lines that pass the module prefilter are now parsed directly instead of going back through the multi-line reader. The equivalence test now also checks the editor after an edit, when it holds split lines. Refs #1424 Assisted-by: Claude Code:claude-opus-5-5 --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent 95f4cae commit 4345294

1 file changed

Lines changed: 77 additions & 15 deletions

File tree

‎crates/socket-patch-core/src/vendor/go_sum_edit.rs‎

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,9 +293,18 @@ impl GoSumEditor {
293293
/// module tree) line for, in file order — the versions whose packages
294294
/// the build loads, unlike the `/go.mod` lines go writes for every
295295
/// version MVS reads.
296+
///
297+
/// The hosted Go rewriter calls this once per patched dep, so a line
298+
/// whose first token can't be `module` is skipped with one prefix
299+
/// compare instead of being tokenized (#1424).
296300
pub(crate) fn zip_versions(&self, module: &str) -> Vec<String> {
297301
self.current_lines()
298-
.flat_map(go_sum_lines)
302+
.filter(|l| {
303+
l.trim_start()
304+
.strip_prefix(module)
305+
.is_some_and(|rest| rest.starts_with(char::is_whitespace))
306+
})
307+
.filter_map(parse_go_sum_line)
299308
.filter_map(|l| l.zip_version_of(module).map(str::to_string))
300309
.collect()
301310
}
@@ -388,20 +397,24 @@ impl<'a> GoSumLine<'a> {
388397
/// lines are skipped. Each caller keeps its own token rule
389398
/// ([`GoSumLine::extra_tokens`]) and hash filter.
390399
pub(crate) fn go_sum_lines(text: &str) -> impl Iterator<Item = GoSumLine<'_>> {
391-
text.lines().filter_map(|line| {
392-
let mut tokens = line.split_whitespace();
393-
let (module, version, hash) = (tokens.next()?, tokens.next()?, tokens.next()?);
394-
let (version, go_mod) = match version.strip_suffix("/go.mod") {
395-
Some(version) => (version, true),
396-
None => (version, false),
397-
};
398-
Some(GoSumLine {
399-
module,
400-
version,
401-
go_mod,
402-
hash,
403-
extra_tokens: tokens.next().is_some(),
404-
})
400+
text.lines().filter_map(parse_go_sum_line)
401+
}
402+
403+
/// One line of [`go_sum_lines`], or `None` when it has fewer than three
404+
/// tokens.
405+
fn parse_go_sum_line(line: &str) -> Option<GoSumLine<'_>> {
406+
let mut tokens = line.split_whitespace();
407+
let (module, version, hash) = (tokens.next()?, tokens.next()?, tokens.next()?);
408+
let (version, go_mod) = match version.strip_suffix("/go.mod") {
409+
Some(version) => (version, true),
410+
None => (version, false),
411+
};
412+
Some(GoSumLine {
413+
module,
414+
version,
415+
go_mod,
416+
hash,
417+
extra_tokens: tokens.next().is_some(),
405418
})
406419
}
407420

@@ -817,4 +830,53 @@ mod tests {
817830
}
818831
}
819832
}
833+
834+
#[test]
835+
fn zip_versions_reads_only_the_modules_own_zip_lines() {
836+
let content = "example.com/a v1.0.0 h1:A=\n\
837+
example.com/a v1.1.0/go.mod h1:B=\n\
838+
example.com/ab v9.0.0 h1:C=\n\
839+
example.com/a/v2 v2.0.0 h1:D=\n\
840+
\t example.com/a v1.2.0\th1:E=\n\
841+
example.com/a v1.3.0\n\
842+
example.com/a\n\
843+
\n\
844+
example.com/a v1.4.0 h1:F= extra\n\
845+
Example.com/a v1.5.0 h1:G=\n\
846+
example.com/a v1.0.0 h1:A=\r\n";
847+
let want = ["v1.0.0", "v1.2.0", "v1.4.0", "v1.0.0"];
848+
let mut editor = GoSumEditor::new(content.to_string());
849+
assert_eq!(editor.zip_versions("example.com/a"), want);
850+
assert_eq!(editor.zip_versions("example.com/ab"), ["v9.0.0"]);
851+
assert_eq!(editor.zip_versions("example.com/a/v2"), ["v2.0.0"]);
852+
assert!(editor.zip_versions("example.com").is_empty());
853+
assert!(editor.zip_versions("v1.0.0").is_empty());
854+
// Same answer once an edit has split the content into lines.
855+
assert!(editor.upsert_module_lines(MOD, VER, ZIP_H1, GOMOD_H1));
856+
assert_eq!(editor.zip_versions("example.com/a"), want);
857+
assert_eq!(editor.zip_versions(MOD), [VER]);
858+
}
859+
860+
#[test]
861+
fn zip_versions_matches_the_full_line_reader() {
862+
// The prefilter must never change the answer of tokenizing every
863+
// line (#1424): compare against `go_sum_lines` over odd spacing.
864+
let content =
865+
"m v1 h1:a\n m v2 h\n\u{a0}m v3 h\nm\u{a0}v4 h\nmm v5 h\nm v6/go.mod h\nm\tv7\th\n";
866+
let text = GoSumEditor::new(content.to_string());
867+
// An edit leaves the editor holding split lines; ask both forms.
868+
let mut lines = GoSumEditor::new(content.to_string());
869+
assert!(lines.upsert_module_lines(MOD, VER, ZIP_H1, GOMOD_H1));
870+
for module in ["m", "mm", "m\u{a0}v4", ""] {
871+
let reference: Vec<String> = go_sum_lines(content)
872+
.filter_map(|l| l.zip_version_of(module).map(str::to_string))
873+
.collect();
874+
assert_eq!(text.zip_versions(module), reference, "module {module:?}");
875+
assert_eq!(
876+
lines.zip_versions(module),
877+
reference,
878+
"lines, module {module:?}"
879+
);
880+
}
881+
}
820882
}

0 commit comments

Comments
 (0)