Reduce allocations and GC pressure for mobile devices#5
Merged
Conversation
Key optimizations: - Fix time.After timer leak in crawler/cacheSaver (use NewTimer+Reset) - candidates(): int64 timestamps + slices.SortFunc (44% faster, 40% less memory) - rewriteRequest: avoid URL string→parse round-trip, share header slices, set scheme="http" directly — eliminates schemeRewriter (45% faster, 33% fewer allocs) - dialFront: build tls.Config once instead of alloc+Clone - Provider.Lookup: zero-alloc for lowercase port-less hostnames (31% faster) - frontsToCache: skip never-succeeded fronts (83% less memory in realistic usage) - Lower default ready channel buffer from 4000 to 500 - Add WithReadyQueueSize and WithCacheSaveInterval options for mobile tuning Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
Performance-focused PR to reduce allocations/GC pressure in hot paths (front selection, request rewriting, provider lookup) and in background goroutines, with added tuning knobs for memory/I/O on constrained (mobile) devices.
Changes:
- Refactors request rewriting/transport usage to avoid URL string round-trips and extra wrapper transport.
- Optimizes
frontPool.candidates()sorting andProvider.Lookup()to reduce allocations. - Reworks crawler/cache-saver scheduling and cache selection behavior; adds options for ready queue size and cache save interval.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| roundtrip.go | Removes scheme rewriter and rebuilds requests directly for fewer allocations. |
| domainfront.go | Adds mobile tuning options and replaces time.After with reusable timers; tweaks vet request flow. |
| front.go | Optimizes candidate sorting (timestamp snapshot + slices.SortFunc) and makes ready queue size configurable. |
| dialer.go | Builds tls.Config without cloning to reduce allocations. |
| config.go | Makes Provider.Lookup zero-alloc in common lowercase/port-less case. |
| cache.go | Skips never-succeeded fronts when building cache entries to reduce allocations. |
| cache_test.go | Updates tests to reflect cache behavior change (skip never-succeeded). |
| front_test.go | Updates for newFrontPool(readySize) signature change. |
| bench_test.go | Updates benchmarks and adds a more realistic cache benchmark. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Drain timer.C after Stop to prevent spurious immediate wake on first loop iteration (0-duration timer fires before Reset) - Clone header value slices in rewriteRequest to decouple from caller's request and prevent data races on retry - Update WithReadyQueueSize doc comment: default is 500, not 4000 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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
time.Aftertimer leak in background goroutines (crawler, cacheSaver)candidates()with int64 timestamps +slices.SortFuncpdqsortschemeRewriter—rewriteRequestnow builds URL directly and sets scheme="http"tls.Configallocation indialFront(no Clone)Provider.Lookupfor lowercase port-less hostnamesfrontsToCacheWithReadyQueueSizeandWithCacheSaveIntervaloptions for mobile tuningBenchmark results
Test plan
go vetclean🤖 Generated with Claude Code