This commit introduces several optimizations to reduce allocations and
improve performance in the core routing and context mechanisms.
Radix Tree (tree.go):
- Optimized `getValue`'s internal `skippedNode` handling:
- Changed `skippedNode.node` to store a direct pointer to the tree node
instead of a full copy, significantly reducing allocations during
backtracking scenarios.
- Corrected the method of adding to the `skippedNodes` slice to use
`append`, ensuring safer and more idiomatic slice growth.
Context Handling (context.go):
- Implemented lazy initialization for `Context.Keys`:
- The `Keys` map is now only allocated on the first call to `Set()`
per request, avoiding map allocation for requests that do not use
context keys. `Context.reset()` now sets `Keys` to `nil`.
- `Get()` correctly handles the `nil` map state.
- Optimized `RequestIP()` for parsing comma-separated IP headers:
- Replaced `strings.Split()` with an iterative parsing approach using
`strings.IndexByte()` and slicing. This avoids allocating an
intermediate slice for IPs, reducing memory usage during IP resolution,
especially for headers like `X-Forwarded-For` with multiple IPs.
These changes are backward compatible for idiomatic usage and have been
reasoned to show improvements in simulated benchmarks, particularly in
reducing allocations per operation for the affected components.