Skip to content

Instantly share code, notes, and snippets.

@billywhizz
Created April 23, 2026 22:29
Show Gist options
  • Select an option

  • Save billywhizz/2eef59227e9b91e10c0105a1794ede5a to your computer and use it in GitHub Desktop.

Select an option

Save billywhizz/2eef59227e9b91e10c0105a1794ede5a to your computer and use it in GitHub Desktop.
import { Bench } from 'lib/bench.mjs'
import { Assembler, Compiler, Registers } from 'lib/asm.js'
import { bind } from 'lib/ffi.js'
const { ptr, core, utf8_encode_into } = lo
const { dlopen, dlsym } = core
const { rdi, rsi, rdx, rcx, r8, r9, rsp, rax } = Registers
function bind_parse (read_buf) {
asm.reset()
asm.sub(rsp, 32)
asm.movreg(rdi, rsi)
asm.movabs(read_buf.ptr, rdi)
asm.movabs(state.ptr, rax)
asm.movreg(rax, rdx)
asm.add(rax, 8)
asm.movreg(rax, rcx)
//asm.movsrc(rax, rcx, 8)
asm.add(rax, 8)
asm.movreg(rax, r8)
asm.add(rax, 8)
asm.movreg(rax, r9)
asm.add(rax, 8)
asm.movdest(rax, rsp, 0)
asm.add(rax, 8)
asm.movdest(rax, rsp, 8)
asm.add(rax, headers_size)
asm.movdest(rax, rsp, 16)
asm.zero(rax)
asm.movdest(rax, rsp, 24)
asm.call(phr_parse_request_sym)
asm.add(rsp, 32)
asm.ret()
return bind(compiler.compile(asm.bytes()), 'i32', ['u32'])
}
const bench = new Bench()
const asm = new Assembler()
const compiler = new Compiler()
const BUFSIZE = 16384
const iter = 10
const runs = 100000000
const num_headers = 16
const headers_size = (4 * 8 * num_headers)
const state_len = (6 * 8) + headers_size
const state = ptr(new Uint8Array(state_len))
const handle = assert(dlopen('./pico.so', 1))
const phr_parse_request_sym = assert(dlsym(handle, 'phr_parse_request'))
const read_buf = ptr(new Uint8Array(BUFSIZE))
const parse = bind_parse(read_buf)
const expected = utf8_encode_into('GET / HTTP/1.1\r\n\r\n', read_buf.ptr)
assert(parse(BUFSIZE) === expected)
{
for (let i = 0; i < iter; i++) {
bench.start(`parse_request`)
for (let j = 0; j < runs; j++) {
assert(parse(BUFSIZE) === expected)
}
bench.end(runs, expected)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment