perf(parser): cut redundant NNTP reads and cap PAR2 scan#532
Merged
Conversation
Three optimizations to the NZB import parser that together cut parse-time NNTP reads by ~50-70% on typical releases: - Share yEnc standard PartSize across the NZB. One representative second-segment fetch feeds normalization for every multi-segment file, replacing ~N per-file second-segment fetches. - Defer 16KB first-segment completion fan-out. Only run it when the NZB actually contains a PAR2 index (so Hash16k matching is useful), and skip obvious sidecars (.nfo/.txt/.srt/.sub/.jpg/.nzb/.sfv/.md5). - Cap PAR2 descriptor scan timeout at 90s and break early after a window of non-FileDesc packets past the last descriptor. The representative yEnc fetch runs in parallel with PAR2 extraction via errgroup, so wall time stays at max(par2, yenc) instead of par2 + yenc. Falls back to the previous per-file behavior when the representative fetch fails.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three optimizations to
internal/importer/parserthat together cut parse-time NNTP reads by ~50–70% on typical multi-file releases.1. Share yEnc
standardPartSizeacross the NZBnormalizeSegmentSizesWithYencpreviously fetched the second and last segment of every multi-segment file to learn the yEncPartSize. Every file produced by the same encoder shares the same middle-segment size, so we now fetch it once per NZB viapickRepresentativeMiddleSegmentand reuse it. Per-file normalization only fetches the last segment.2. Defer 16KB first-segment completion behind PAR2 presence
The fan-out loop in
fetchAllFirstSegmentsthat reads extra segments to reach 16KB exists only for PAR2Hash16kMD5 matching. It now runs only whenhasPar2IndexCandidateconfirms the NZB actually contains a PAR2 index, and it skips obvious sidecars (.nfo,.txt,.srt,.sub,.jpg,.jpeg,.png,.nzb,.sfv,.md5).3. Cap PAR2 scan timeout and short-circuit on FileDesc completion
par2/descriptor.gopreviously used30s × segments(up to 2.5 min for a 5-segment index) and read up to 1000 packets. Now:4. Parallelize PAR2 extraction and representative yEnc fetch
par2.GetFileDescriptorsand the one-shot representative yEnc fetch are independent. They now run together in anerrgroup, so wall time ismax(par2, yenc)instead ofpar2 + yenc.Notes for reviewers
ConnectionPool/BodyAsynccontracts.notFoundIDssemantics.ParsedNzbshould be byte-identical for existing NZBs — the only behavioral change is when / whether certain reads happen, not which data lands in the parsed result.fileinfo.getFileInfoalready handles the case where the padded MD5 doesn't match any descriptor (it's the normal path for non-PAR2 NZBs), so skipping 16KB completion is safe.Test plan
go build ./...go vet ./internal/importer/parser/...go test -race ./internal/importer/parser/...— all existing tests passParsedNzbmatches byte-for-byte vs. pre-change output, and that parse time drops meaningfully viaprogressTrackertimings