Skip to content

Commit

Permalink
avoid dynamic table prealloc
Browse files Browse the repository at this point in the history
  • Loading branch information
nitely committed Oct 3, 2024
1 parent eeb6e73 commit 0814f6b
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/hpack/hcollections.nim
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ proc initDynHeaders*(strsize: int): DynHeaders {.inline.} =
## ``strsize`` is the max size in bytes
## of all headers put together.
DynHeaders(
s: newString(strsize),
s: "",
pos: 0,
filled: 0,
bounds: initDeque[HBounds](0),
Expand Down Expand Up @@ -111,6 +111,9 @@ proc add*(q: var DynHeaders, n, v: openArray[char]) {.inline.} =
discard q.pop()
if nvLen > q.size-32:
return
if q.pos+nvLen >= q.s.len:
let desiredLen = max(q.s.len*2, q.pos+nvLen+1)
q.s.setLen max(q.s.len, min(q.size, desiredLen))
let hbn = q.pos .. q.pos+n.len-1
let nLen = min(n.len, q.s.len-q.pos)
strcopy(q.s, n, q.pos, 0, nLen)
Expand All @@ -137,7 +140,7 @@ proc setSize*(q: var DynHeaders, strsize: Natural) {.inline.} =
# and grow needs to un-wrap around the headers
if strsize > q.s.len:
let oldLen = q.s.len
q.s.setLen max(strsize, oldLen*2)
q.s.setLen oldLen*2
for i in 0 .. oldLen-1:
q.s[oldLen+i] = q.s[i]
if strsize == 0:
Expand Down

0 comments on commit 0814f6b

Please sign in to comment.