Skip to content

Commit

Permalink
Merge pull request #1 from holepunchto/tweak-codegen
Browse files Browse the repository at this point in the history
use allocation free approach
  • Loading branch information
andrewosh authored Dec 29, 2024
2 parents c3acaee + dc2d972 commit 63b51eb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
28 changes: 19 additions & 9 deletions lib/codegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module.exports = function generateCode (hyperdispatch) {
str += '/* eslint-disable camelcase */\n'
str += '\n'
str += 'const { c, b4a, assert } = require(\'hyperdispatch/runtime\')\n'
str += 'const { version, resolveStruct } = require(\'./messages.js\')\n'
str += 'const { version, getEncoding, setVersion } = require(\'./messages.js\')\n'
str += '\n'
str += 'const defaultVersion = version\n'
str += '\n'

str += 'class Router {\n'
Expand Down Expand Up @@ -45,10 +47,12 @@ module.exports = function generateCode (hyperdispatch) {
str += ' const state = { buffer: encoded, start: 0, end: encoded.byteLength }\n'
str += ' const id = c.uint.decode(state)\n'
str += '\n'
str += ' setVersion(defaultVersion)\n'
str += '\n'
str += ' switch (id) {\n'
for (const handler of hyperdispatch.handlers) {
str += ` case ${handler.id}:\n`
str += ` return this._handler${handler.id}(resolveStruct(${s(handler.requestType)}).decode(state), context)\n`
str += ` return this._handler${handler.id}(route${handler.id}.enc.decode(state), context)\n`
}
str += ' default:\n'
str += ' throw new Error(\'Handler not found for ID:\' + id)\n'
Expand All @@ -58,10 +62,11 @@ module.exports = function generateCode (hyperdispatch) {

str += '\n'

str += 'function dispatch (name, message, opts) {\n'
str += ' const state = c.state()\n'
str += 'function dispatch (name, message, { version = defaultVersion } = {}) {\n'
str += ' const state = { buffer: null, start: 0, end: 0 }\n'
str += '\n'
str += ' const o = getEncoderAndId(name)\n'
str += ' setVersion(version)\n'
str += '\n'
str += ' c.uint.preencode(state, o.id)\n'
str += ' o.enc.preencode(state, message)\n'
Expand All @@ -74,14 +79,19 @@ module.exports = function generateCode (hyperdispatch) {
str += '}\n'
str += '\n'

str += 'function getEncoderAndId (name, opts) {\n'
for (const handler of hyperdispatch.handlers) {
str += 'const route' + handler.id + ' = {\n'
str += ` id: ${handler.id},\n`
str += ` enc: getEncoding(${s(handler.requestType)})\n`
str += '}\n'
str += '\n'
}

str += 'function getEncoderAndId (name) {\n'
str += ' switch (name) {\n'
for (const handler of hyperdispatch.handlers) {
str += ` case ${s(handler.name)}:\n`
str += ' return {\n'
str += ` id: ${handler.id},\n`
str += ` enc: resolveStruct(${s(handler.requestType)}, opts && opts.version)\n`
str += ' }\n'
str += ` return route${handler.id}\n`
}
str += ' default:\n'
str += ' throw new Error(\'Handler not found for name: \' + name)\n'
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"test-tmp": "^1.3.0"
},
"dependencies": {
"b4a": "^1.6.6",
"compact-encoding": "^2.15.0",
"b4a": "^1.6.7",
"compact-encoding": "^2.16.0",
"generate-string": "^1.0.1",
"hyperschema": "^1.0.0",
"hyperschema": "^1.3.2",
"nanoassert": "^2.0.0"
}
}

0 comments on commit 63b51eb

Please sign in to comment.