Skip to content

Commit

Permalink
fix(stdlib): Remove intermediate resizes in Buffer.autogrow (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
cician committed Feb 5, 2022
1 parent 3d7fc57 commit c1695d0
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions stdlib/buffer.gr
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,24 @@ let getSize = ptr => WasmI32.load(ptr, _SIZE_OFFSET)

/* Doubles the size of buffer's underlying byte sequence, if the given size is larger than the size of a buffer's underlying byte sequence */
let autogrow = (len, buf) => {
while (buf.len + len > Bytes.length(buf.data)) {
let mut n = Bytes.length(buf.data)
if (n == 0) n = 4
// Make sure bytes of 0 length grow too
buf.data = Bytes.resize(0, n, buf.data)
let requiredMinimumSize = buf.len + len
let currentSize = Bytes.length(buf.data)

if (requiredMinimumSize > currentSize) {
let mut newSize = if (currentSize > 0) {
currentSize
} else {
// Make sure bytes of 0 length grow too
4
}

while (newSize < requiredMinimumSize) {
newSize *= 2
}

let growBy = newSize - currentSize

buf.data = Bytes.resize(0, growBy, buf.data)
}
}

Expand Down

0 comments on commit c1695d0

Please sign in to comment.