Commit graph

2 commits

Author SHA1 Message Date
google-labs-jules[bot]
b07f9ee1dd Optimize radix tree and context handling for performance
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.
2025-06-23 19:55:07 +00:00
wjqserver
484f2f016b init(v0.0.1) 2025-05-28 18:25:28 +08:00