Skip to content

perf(http): cut per-request allocs (buffer memset, header keys, empty tables - #698

Open
jmgomez wants to merge 4 commits into
status-im:masterfrom
jmgomez:http_perf
Open

perf(http): cut per-request allocs (buffer memset, header keys, empty tables#698
jmgomez wants to merge 4 commits into
status-im:masterfrom
jmgomez:http_perf

Conversation

@jmgomez

@jmgomez jmgomez commented Jul 22, 2026

Copy link
Copy Markdown
Contributor
image

@jmgomez
jmgomez requested a review from arnetheduck July 23, 2026 14:16
Comment thread chronos/apps/http/httptable.nim Outdated
proc set*(ht: var HttpTables, key: string, value: string) =
## Set/replace value of header with key ``key`` to value ``value``.
let lowkey = key.toLowerAscii()
let lowkey = lowerKey(key)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this will still perform several unnecesary allocations - what you're looking for is:

proc replaceWithOne(v: var seq[string], s: string) =
  v.setLen(1)
  v[0] = s
ht.table[lowerKey(...)].replaceWithOne()

the values here should also use chronosSink and move into the seq

Comment thread chronos/apps/http/httptable.nim Outdated
Comment thread chronos/apps/http/httpserver.nim Outdated
Comment thread chronos/apps/http/httpserver.nim Outdated
Comment thread chronos/apps/http/httpserver.nim Outdated
conn.buffer.setLen(res)
let header = parseRequest(conn.buffer)
let header = parseRequest(
conn.buffer.toOpenArray(0, res - 1), makeCopy = true)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it might be that we don't need the makecopy here - afair, parseRequest will store offsets into the buffer so as long as the buffer is not touched, its length should really not matter

## response has no streaming writer — on that path closeWait() never
## actually suspends, so the future allocation is pure overhead.
## Returns false when the async variant is required.
if req.state == HttpState.Alive:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

instead of repeating this code, this should be refactored to something like:

template closeImpl(..) =
  if ...
``

then the template can be inlined in both the public closeWait and the other places - this should achieve the same goal but with less future maintenance - the state management is hairy enough as it is.

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.

2 participants