⚡ perf: Optimize CORS exact-origin lookup to O(1)#4368
Merged
Conversation
Copilot
AI
changed the title
[WIP] Optimize CORS origin lookup from O(n) to O(1) using map
Optimize CORS exact-origin lookup to O(1)
May 28, 2026
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4368 +/- ##
==========================================
- Coverage 91.39% 91.34% -0.05%
==========================================
Files 132 132
Lines 13098 13098
==========================================
- Hits 11971 11965 -6
- Misses 710 715 +5
- Partials 417 418 +1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Contributor
There was a problem hiding this comment.
Pull request overview
Optimizes the CORS middleware’s exact-origin matching by replacing per-request linear scans over normalized allowed origins with a precomputed set, reducing exact-match lookup from O(n) to O(1) while preserving existing normalization and response behavior.
Changes:
- Precompute normalized exact origins into a
map[string]struct{}during middleware initialization for constant-time exact-origin checks. - Keep existing wildcard/subdomain matching logic unchanged.
- Add a regression test ensuring normalized configured origins (whitespace/trailing slash/case) still match the request origin as expected.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| middleware/cors/cors.go | Builds a normalized exact-origin set once and switches exact-match checks to map membership. |
| middleware/cors/cors_test.go | Adds a test covering normalized configured origin lookup behavior for exact matches. |
gaby
approved these changes
May 28, 2026
ReneWerner87
approved these changes
May 28, 2026
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.
Description
The CORS middleware performed a linear scan over configured exact origins on every request with an
Originheader. This change precomputes a normalized exact-origin set at middleware construction time, reducing per-request exact-match lookup from O(n) to O(1) without changing matching semantics.Exact-origin lookup
AllowOriginsslice used for exact matches with amap[string]struct{}built once during middleware initialization.Behavior preservation
Regression coverage
Changes introduced
Type of change
Please delete options that are not relevant.
Checklist
Before you submit your pull request, please make sure you meet these requirements:
/docs/directory for Fiber's documentation.Commit formatting
Please use emojis in commit messages for an easy way to identify the purpose or intention of a commit. Check out the emoji cheatsheet here: CONTRIBUTING.md