Skip to content

slist: avoid quadratic traversal when copying and exporting lists - #23361

Closed
cla7aye15I4nd wants to merge 9 commits into
curl:masterfrom
cla7aye15I4nd:zheng/curl-linear-list-build
Closed

cla7aye15I4nd wants to merge 9 commits into
curl:masterfrom
cla7aye15I4nd:zheng/curl-linear-list-build

Conversation

@cla7aye15I4nd

@cla7aye15I4nd cla7aye15I4nd commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

Curl_slist_duplicate() and cookie_list() append every new node by walking the output list from its head. Building n entries therefore does quadratic list traversal. Track the next link locally and append singleton nodes, making list construction linear while preserving deep copying, cookie iteration order and existing allocation-failure cleanup. The public curl_slist layout and append semantics are unchanged.

Validation:

  • Debug static build and source-format/whitespace checks pass.
  • Existing tests 1549, 2504, 1502, 1534, 1535 and 1536 pass. Test 506 was requested but not executed by the local runner.
  • An external C harness linked against before/after static libraries verifies NULL input, singleton and 8k/16k/32k deep copies, node/string independence, ordering and termination. Allocation callbacks installed with curl_global_init_mem inject failures at the first two allocations for a singleton and the first eight allocations for an 8k list; duplication returns NULL and leaves the original list valid.
  • The harness populates independent cookie engines through CURLOPT_COOKIELIST with 0/8k/16k/32k Netscape-format cookies across 127 domains, then exports CURLINFO_COOKIELIST. Counts match, and concatenated output compares byte-for-byte equal before/after. Timings exclude setup and printing.

Single-run CPU timings on macOS, Debug build (ms):

Entries Duplicate before Duplicate after Cookie export before Cookie export after
8,000 28.646 0.263 39.188 3.461
16,000 122.360 0.504 135.735 6.691
32,000 499.035 0.945 471.078 14.050

These are local performance measurements, not cross-platform guarantees. No new in-tree tests are added; the existing regressions and external checks above validate this traversal-only optimization.

AI-assisted implementation and local validation. Submitted for upstream review; cross-platform CI remains to be reviewed.

The macOS HTTP suite also exposed a DNS test-server shutdown race: a select timeout continued the event loop without checking got_exit_signal, so fixture teardown could hang. Check the signal at the loop condition. On macOS ARM64, an injected SIGTERM immediately before a select timeout reproduces the hang on the original server; the patched server exits and removes its PID file. A normal SIGTERM shutdown, the server build, checksrc and whitespace checks also pass.

@cla7aye15I4nd cla7aye15I4nd changed the title slist: avoid quadratic traversal when copying and exporting lists [WIP] slist: avoid quadratic traversal when copying and exporting lists Oct 11, 2026
@cla7aye15I4nd cla7aye15I4nd changed the title [WIP] slist: avoid quadratic traversal when copying and exporting lists slist: avoid quadratic traversal when copying and exporting lists Oct 11, 2026
@cla7aye15I4nd
cla7aye15I4nd marked this pull request as ready for review October 11, 2026 08:05
@bagder
bagder requested a balanced review from Copilot October 11, 2026 21:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The tail-pointer implementations preserve ownership, ordering, and allocation-failure cleanup semantics.

0 open findings

What changed in this PR

Optimizes internal slist construction from quadratic to linear time while preserving ordering and cleanup behavior.

Changes:

  • Builds duplicated slists using a tracked tail pointer.
  • Applies the same optimization to cookie-list exports.
File Description
lib/​slist.c Optimizes deep-copy list construction.
lib/​cookie.c Optimizes exported cookie-list construction.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@github-actions github-actions Bot added the CI Continuous Integration label Oct 13, 2026
@github-actions github-actions Bot added the tests label Oct 13, 2026
@bagder bagder closed this in 2cb089c Oct 14, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CI Continuous Integration tests

Development

Successfully merging this pull request may close these issues.

3 participants