Repository navigation
feat: Support Router.attach - #62
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the 📝 Walkthrough""" WalkthroughThe changes unify static and dynamic route storage in the Changes
Sequence Diagram(s)sequenceDiagram
participant MainRouter as Router
participant SubRouter as Router
participant PathTrie
MainRouter->SubRouter: attach('/foo', SubRouter)
SubRouter->PathTrie: get _allRoutes
MainRouter->PathTrie: merge SubRouter._allRoutes
PathTrie-->>MainRouter: merged trie
MainRouter->MainRouter: clear SubRouter's _staticCache
sequenceDiagram
participant Router
participant PathTrie
participant StaticCache
Router->StaticCache: lookup(path)
alt found in cache
StaticCache-->>Router: return value
else not found
Router->PathTrie: lookup(path)
PathTrie-->>Router: LookupResult(value, params, isDynamic)
alt isDynamic == false
Router->StaticCache: cache value
end
Router-->>Caller: return LookupResult
end
Assessment against linked issues
Possibly related PRs
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #62 +/- ##
==========================================
+ Coverage 80.07% 80.09% +0.01%
==========================================
Files 78 78
Lines 2279 2281 +2
Branches 1289 1290 +1
==========================================
+ Hits 1825 1827 +2
Misses 454 454 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
✅ Actions performedReview triggered.
|
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (3)
benchmark/benchmark.dart (2)
60-68: Uselate finalto prevent unintended reassignment of benchmark routers
staticRouterForLookupis only set once insetup()and never reassigned.
Declaring it aslate finaldocuments this intent and protects against
accidental mutation inside future refactors.- late Router<int> staticRouterForLookup; + late final Router<int> staticRouterForLookup;(The same applies to
dynamicRouterForLookup,staticRouterForLookup
anddynamicRouterForLookupin the routing-kit benchmarks further
below.)
116-187: Consider factoring out duplicated benchmark boiler-plateAll four routing-kit benchmark classes replicate the same
loop-initialisation logic already present in the in-house router
benchmarks. Extracting a small helper (or even a generic mix-in)
would:
- eliminate ~70 duplicate LOC,
- ensure the two router families stay behaviour-identical, and
- make it easier to tweak parameters such as
routeCount
without touching six separate places.This is an optional readability / maintainability refactor, no
functional issue.lib/src/router/router.dart (1)
50-63: Minor: avoid constructing a freshLookupResultwhen cache hitsOn a cache hit we already know the route is static and parameters are
empty. Returning a pre-allocated constant can save a small amount of
GC pressure inside tight loops (e.g. benchmarks):-if (value != null) return LookupResult(value, {}, false); +if (value != null) { + // Re-use a shared empty map to avoid allocation on every hit. + return LookupResult(value, const {}, false); +}Not critical, but it moves the hot-path to zero allocations.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (6)
.gitignore(1 hunks)benchmark/benchmark.dart(5 hunks)lib/src/router/path_trie.dart(4 hunks)lib/src/router/router.dart(2 hunks)pubspec.yaml(1 hunks)test/router/router_test.dart(13 hunks)
🔇 Additional comments (9)
pubspec.yaml (1)
24-24: Dependency addition looks good for benchmarking.Adding the
routingkitpackage as a dev dependency will enable performance comparison benchmarks as mentioned in the PR objectives..gitignore (1)
17-17: Good addition to ignore executable files.Adding
*.exeto .gitignore is appropriate when introducing benchmarking code that might generate Windows executables during testing.lib/src/router/path_trie.dart (5)
11-12: Clean implementation of the isDynamic flag.Good documentation explaining the purpose of this new field, which will help track whether a matched route contains parameters.
15-15: Constructor updated correctly for the new field.The constructor has been properly modified to include the new
isDynamicparameter.
156-156: Flag initialization for route dynamism tracking.Good initialization of the
isDynamicflag tofalseby default, which will be toggled when parameter segments are matched.
169-169: Dynamic route detection logic.The flag is correctly set to
truewhen a parameterized segment is matched during path traversal.
178-178: Complete implementation of the lookup result.The
isDynamicflag is properly passed to theLookupResultconstructor, completing the implementation of the dynamic route tracking feature.test/router/router_test.dart (2)
24-25: Improved test descriptions for better readability.The commas added to the test descriptions improve readability and adhere to proper grammar in multi-line descriptions.
Also applies to: 31-32, 38-39, 45-46, 53-54, 61-62, 80-81, 87-88, 94-95, 112-113, 119-120, 138-139, 145-146, 166-167, 228-229, 235-236, 242-243, 259-260, 266-267, 283-284, 290-291, 317-318, 324-325, 352-353, 560-561
607-666: Comprehensive tests for Router.attach feature.Excellent test coverage for the new
attachfunctionality, including:
- Verifying routes from sub-routers are accessible with the correct path prefix
- Testing with and without trailing slashes
- Validating root path handling
- Checking non-existent route behavior
- Confirming error handling for conflicting routes
These tests thoroughly validate the feature described in the PR objectives.
|
@coderabbitai review |
✅ Actions performedReview triggered.
|
SandPod
left a comment
There was a problem hiding this comment.
LGTM!
Nothing to comment about.
Description
This Pull Request introduces the
Router.attachfeature, with corresponding unit tests.This PR changes how we optimize for static routes. Now all routes are added to the trie,
wheather they are static or not and the HashMap of static routes is purely a performance
optimization that is updated lazily on lookup.
The advantage of maintaining all routes in the trie is that it makes implementing attach
trivial.
I considered dropping the optimization for static routes completely, but maintaining it
lazily proved simple.
The PR also extends the benchmark for comparision with another router wrt. add and lookup
of dynamic and static routes.
Related Issues
Pre-Launch Checklist
Please ensure that your PR meets the following requirements before submitting:
///), ensuring consistency with existing project documentation.Breaking Changes
Benchmark results:
2,4 GHz 8-Core Intel Core i9
Summary by CodeRabbit
New Features
routingkitpackage.Bug Fixes
Refactor
Tests
Chores
.exefiles to.gitignore.routingkitto development dependencies.