Skip to content

king: pre-allocate buffer in king_curl_bytes if possible#1000

Merged
pkova merged 1 commit into
developfrom
pkova/curler
Apr 17, 2026
Merged

king: pre-allocate buffer in king_curl_bytes if possible#1000
pkova merged 1 commit into
developfrom
pkova/curler

Conversation

@pkova

@pkova pkova commented Apr 17, 2026

Copy link
Copy Markdown
Collaborator

A user observed that downloading the initial pill on windows was taking forever, so I started my investigation. No matter when I paused execution in the debugger I was always ending up on c3_realloc in king_curl_alloc. It turns out that realloc is dog slow on windows because it doesn't have anything like mremap.

Here we change the memory allocation strategy in king_curl_bytes to the following:

  1. If we get a Content-Length header, we parse that and pre-allocate.

  2. If we don't or the header is lying, we amortize the reallocs by always doubling the capacity of the buffer when it runs out.

A user observed that downloading the initial pill on windows was taking forever,
so I started my investigation. The debugger showed me that no matter when I
paused execution I was always ending up on c3_realloc in king_curl_alloc. It
turns out that realloc is dog slow on windows because it doesn't have anything like
mremap.

Here we change the memory allocation strategy in king_curl_bytes to the
following:

1. If we get a Content-Length header, we parse that and pre-allocate.

2. If we don't or the header is lying, we amortize the reallocs by always
doubling the capacity of the buffer when it runs out.
@pkova pkova requested a review from a team as a code owner April 17, 2026 13:58
@pkova pkova merged commit 8165f11 into develop Apr 17, 2026
2 checks passed
@pkova pkova deleted the pkova/curler branch April 17, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant