Skip to content

Conversation

@bagder
Copy link
Member

@bagder bagder commented Dec 7, 2025

  • delay allocations until the cookie is deemed fine. Avoid allocations completely if the incoming cookie isn't fine.
  • only store and keep the sanitized path for each cookie, instead of two versions

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.

test allocs before allocs after delta percent allocs left in PR
6 114 114 - 100%
7 128 123 -5 96%
8 274 169 -105 62%
27 292 289 -3 99%
31 415 289 -126 70%
46 224 208 -16 93%
61 160 141 -19 88%
62 275 255 -20 93%

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.
@bagder bagder added the cookies label Dec 7, 2025
@github-actions github-actions bot added the tests label Dec 7, 2025
@bagder
Copy link
Member Author

bagder commented Dec 7, 2025

augment review

@bagder bagder requested a review from Copilot December 7, 2025 16:09
@bagder bagder marked this pull request as ready for review December 7, 2025 16:09
Copy link

@augmentcode augmentcode bot left a 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.

Copy link

Copilot AI left a 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 path and spath fields into a single path field 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.
@bagder bagder force-pushed the bagder/cookie-alloc branch from ed8226b to e37e3cd Compare December 7, 2025 16:25
@bagder bagder closed this in c6e5dfa Dec 8, 2025
bagder added a commit that referenced this pull request Dec 8, 2025
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
bagder added a commit that referenced this pull request Dec 8, 2025
Instead of keeping both versions around.

Closes #19864
@bagder bagder deleted the bagder/cookie-alloc branch December 8, 2025 07:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

1 participant