Skip to content

Commit

Permalink
chore(stdlib): Optimize Buffer.addChar (#1933)
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake authored Jan 26, 2024
1 parent 2efb66c commit da1271d
Showing 1 changed file with 54 additions and 1 deletion.
55 changes: 54 additions & 1 deletion stdlib/buffer.gr
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ include "runtime/unsafe/memory"
include "runtime/unsafe/wasmi32"
include "runtime/unsafe/conv"
include "runtime/exception"
include "runtime/dataStructures"
from DataStructures use { untagChar }
include "int32"
include "bytes"
include "string"
Expand Down Expand Up @@ -386,8 +388,59 @@ provide let addString = (string, buffer) => {
*
* @since v0.4.0
*/
@unsafe
provide let addChar = (char, buffer) => {
addString(Char.toString(char), buffer)
from WasmI32 use {
(-),
(*),
(&),
(|),
(>>>),
ltU as (<),
gtU as (>),
leU as (<=),
}
let usv = untagChar(char)

let bytelen = if (usv < 0x80n) {
autogrow(1, buffer)
from WasmI32 use { (+) }
let off = coerceNumberToWasmI32(buffer.len)
let dst = WasmI32.fromGrain(buffer.data) + _VALUE_OFFSET
WasmI32.store8(dst, usv, off)
1
} else {
let mut count = 0n
let mut bytelen = 0
let mut offset = 0n
if (usv <= 0x07FFn) {
count = 1n
bytelen = 2
offset = 0xC0n
} else if (usv <= 0xFFFFn) {
count = 2n
bytelen = 3
offset = 0xE0n
} else {
count = 3n
bytelen = 4
offset = 0xF0n
}
from WasmI32 use { (+) }
autogrow(bytelen, buffer)
let off = coerceNumberToWasmI32(buffer.len)
let dst = WasmI32.fromGrain(buffer.data) + _VALUE_OFFSET
WasmI32.store8(dst, (usv >>> 6n * count) + offset, off)
let mut n = 0n
while (count > 0n) {
n += 1n
let temp = usv >>> 6n * (count - 1n)
WasmI32.store8(dst + n, 0x80n | temp & 0x3Fn, off)
count -= 1n
}
bytelen
}
buffer.len += bytelen
}

/**
Expand Down

0 comments on commit da1271d

Please sign in to comment.