-
-
Notifications
You must be signed in to change notification settings - Fork 7k
cookie: improved allocations #19864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cookie: improved allocations #19864
Conversation
This delays the allocating of the cookie struct until after all the checks have been done, as many cookies are received and discarded instead of accepted and this then saves one allocation for every discarded cookie.
|
augment review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Review completed. 2 suggestions posted.
Comment augment review to trigger a new review at any time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR optimizes cookie handling in curl by reducing memory allocations through two key strategies: (1) delaying heap allocation of cookie structs until after validation succeeds, and (2) storing only the normalized/canonical path instead of maintaining both the original and sanitized versions. The optimization results in 7-38% reduction in allocations across various cookie-handling test cases, with the most significant improvements in tests with heavy cookie processing.
Key Changes
- Introduced stack-based cookie struct initialization that's only cloned to heap after all validation passes, eliminating wasted allocations for rejected cookies
- Consolidated the dual
pathandspathfields into a singlepathfield that stores the canonical (normalized) path directly - Modified
sanitize_cookie_path()to work with length parameters and integrated it directly into the parsing flow
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/cookie.h | Removed spath field from Cookie struct; path now stores canonical path only |
| lib/cookie.c | Implemented delayed heap allocation using stack struct; refactored path sanitization to be length-based; added storecookie() helper; updated strstore() signature and freecookie() to support dual-mode operation |
| tests/data/test31 | Updated expected cookie paths to canonical form (trailing slashes removed) |
| tests/data/test46 | Updated expected cookie path from /want/ to /want |
| tests/data/test61 | Updated expected cookie paths to canonical form |
| tests/data/test420 | Updated expected cookie paths and cookie ordering in protocol output |
| tests/data/test444 | Updated 50 cookie entries to use canonical paths without trailing slashes |
| tests/data/test1105 | Updated expected cookie path from "/silly/" to /silly (quotes and trailing slash removed) |
| tests/data/test1561 | Updated expected cookie path for login path |
| tests/data/test1903 | Updated expected cookie paths to canonical form |
| tests/data/test1905 | Updated expected cookie paths to canonical form |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
To avoid wasting time allocating data for incoming cookies that are discarded for one reason or another, delay allocations until after verifications are done.
Instead of keeping both versions around.
ed8226b to
e37e3cd
Compare
To avoid wasting time allocating data for incoming cookies that are discarded for one reason or another, delay allocations until after verifications are done. Closes #19864
Instead of keeping both versions around. Closes #19864
To measure what difference this patch series has, I counted the number of allocations done when running the first eight cookie-using test cases and compared before and after this PR. The allocations counted are for the entire test case.