Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More interpreter optimizations #107

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1c5d3d1
Trying to optimize interpreter
Saiv46 Jun 4, 2020
a8419b9
Merge branch 'master' of https://github.com/ProtoDef-io/node-protodef
Saiv46 Jun 13, 2020
019bb99
Revert "Merge branch 'master' of https://github.com/ProtoDef-io/node-…
Saiv46 Jun 13, 2020
e08e58e
Revert "Revert "Merge branch 'master' of https://github.com/ProtoDef-…
Saiv46 Jun 13, 2020
a68af84
Refactored things
Saiv46 Jun 13, 2020
ee00fca
Merge branch 'patch-1'
Saiv46 Jun 13, 2020
ee6b8b3
Implemented removeType & organizing changes
Saiv46 Jun 13, 2020
43f7401
Implemented `createEncoding` (closes #69)
Saiv46 Jun 13, 2020
2a34b91
Removed `DATATYPE_NOCOPY`
Saiv46 Jun 13, 2020
63de431
Removed DATATYPE_NOCOPY constant
Saiv46 Jun 15, 2020
3d168b0
Revert "Removed `DATATYPE_NOCOPY`"
Saiv46 Jun 16, 2020
b444547
Update interpreter.js
Saiv46 Jun 16, 2020
ec4c9db
Merge branch 'master' of https://github.com/Saiv46/node-protodef
Saiv46 Jun 16, 2020
48220c8
fix(interpreter/structures): Bugfix
Saiv46 Jun 17, 2020
932809e
fix(interpreter.js): Continuing expirement with DATATYPE_NOCOPY
Saiv46 Jun 17, 2020
dc47292
Lint quickfix
Saiv46 Jun 17, 2020
f1610e1
Optimize `constructProduceArgs`
Saiv46 Jan 3, 2021
9b6285c
Optimize encoding and `tryDoc`
Saiv46 Jan 3, 2021
84c1fad
Update serializer.js
Saiv46 Jan 3, 2021
86f3ba9
Optimize type extension
Saiv46 Jan 3, 2021
21f81af
Using `Result` struct
Saiv46 Jan 3, 2021
f927c8b
Using upstream
Saiv46 Jan 3, 2021
90288f1
Removed `DATATYPE_NOCOPY`
Saiv46 Jan 3, 2021
8dadd50
Refactor
Saiv46 Jan 3, 2021
88eb235
Refactor
Saiv46 Jan 3, 2021
359a3c6
More bound functions optimizations
Saiv46 Jan 3, 2021
3df8aca
Update utils.js
Saiv46 Jan 3, 2021
fcedeed
Update utils.js
Saiv46 Jan 3, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .npmignore

This file was deleted.

10 changes: 8 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "A simple yet powerful way to define binary protocols",
"main": "index.js",
"author": "roblabla <[email protected]>",
"sideEffects": false,
"scripts": {
"prepare": "require-self",
"lint": "standard",
Expand All @@ -21,7 +22,7 @@
"readable-stream": "^3.0.3"
},
"engines": {
"node": ">=6"
"node": ">=12"
},
"bugs": {
"url": "https://github.com/ProtoDef-io/node-protodef/issues"
Expand All @@ -38,5 +39,10 @@
"mocha": "^5.2.0",
"require-self": "^0.1.0",
"standard": "^12.0.1"
}
},
"files": [
"src/",
"ProtoDef/schemas/",
"*.js"
]
}
174 changes: 98 additions & 76 deletions src/compiler.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
const numeric = require('./datatypes/numeric')
const utils = require('./datatypes/utils')

const conditionalDatatypes = require('./datatypes/compiler-conditional')
const structuresDatatypes = require('./datatypes/compiler-structures')
const utilsDatatypes = require('./datatypes/compiler-utils')

const { tryCatch } = require('./utils')
const {
tryCatch,
Enum: {
CompilerTypeKind: { NATIVE, CONTEXT, PARAMETRIZABLE }
}
} = require('./utils')
const defaultDatatypes = require('./datatypes/compiler')

class ProtoDefCompiler {
constructor () {
Expand Down Expand Up @@ -96,13 +95,13 @@ class CompiledProtodef {
}

parsePacketBuffer (type, buffer) {
const { value, size } = tryCatch(() => this.read(buffer, 0, type),
const { value: data, size } = tryCatch(() => this.read(buffer, 0, type),
(e) => {
e.message = `Read error for ${e.field} : ${e.message}`
throw e
})
return {
data: value,
data,
metadata: { size },
buffer: buffer.slice(0, size)
}
Expand All @@ -111,32 +110,85 @@ class CompiledProtodef {

class Compiler {
constructor () {
this.scopeStack = []
this.clearTypes()
}

/**
* See `addNativeType` / `addContextType` / `addParametrizableType`
* @param {String} name
* @param {Function} fn
* @param {*} kind
*/
addType (name, fn, kind = NATIVE) {
switch (kind) {
case NATIVE:
this.addNativeType(name, fn); break
case CONTEXT:
this.addContextType(name, fn); break
case PARAMETRIZABLE:
this.addParametrizableType(name, fn); break
default:
throw new Error('Unknown datatype kind ' + kind)
}
}

/**
* @param {String} name
* @param {Function} fn
* @param {*} kind
*/
removeType (name, fn, kind = NATIVE) {
switch (kind) {
case NATIVE:
delete this.primitiveTypes[name]
delete this.native[name]
delete this.types[name]
break
case CONTEXT:
delete this.primitiveTypes[name]
delete this.context[name]
break
case PARAMETRIZABLE:
delete this.parameterizableTypes[name]
break
default:
throw new Error('Unknown datatype kind ' + kind)
}
}

addTypes (types) {
for (const [type, [kind, fn]] of Object.entries(types)) {
this.addType(type, fn, kind)
}
}

clearTypes () {
this.primitiveTypes = {}
this.native = {}
this.context = {}
this.types = {}
this.scopeStack = []
this.parameterizableTypes = {}
}

/**
* A native type is a type read or written by a function that will be called in it's
* original context.
* @param {*} type
* @param {*} fn
* @param {String} type
* @param {Function} fn
*/
addNativeType (type, fn) {
this.primitiveTypes[type] = `native.${type}`
this.native[type] = fn
this.types[type] = 'native'
this.types[type] = NATIVE
}

/**
* A context type is a type that will be called in the protocol's context. It can refer to
* registred native types using native.{type}() or context type (provided and generated)
* using ctx.{type}(), but cannot access it's original context.
* @param {*} type
* @param {*} fn
* @param {String} type
* @param {Function} fn
*/
addContextType (type, fn) {
this.primitiveTypes[type] = `ctx.${type}`
Expand All @@ -146,25 +198,17 @@ class Compiler {
/**
* A parametrizable type is a function that will be generated at compile time using the
* provided maker function
* @param {*} type
* @param {*} maker
* @param {String} type
* @param {Function} maker
*/
addParametrizableType (type, maker) {
this.parameterizableTypes[type] = maker
}

addTypes (types) {
for (const [type, [kind, fn]] of Object.entries(types)) {
if (kind === 'native') this.addNativeType(type, fn)
else if (kind === 'context') this.addContextType(type, fn)
else if (kind === 'parametrizable') this.addParametrizableType(type, fn)
}
}

addTypesToCompile (types) {
for (const [type, json] of Object.entries(types)) {
// Replace native type, otherwise first in wins
if (!this.types[type] || this.types[type] === 'native') this.types[type] = json
if (!this.types[type] || this.types[type] === NATIVE) this.types[type] = json
}
}

Expand All @@ -178,10 +222,17 @@ class Compiler {
recursiveAddTypes(protocolData, path.slice(0))
}

/**
* @param {String} code
* @param {String} indent
*/
indent (code, indent = ' ') {
return code.split('\n').map((line) => indent + line).join('\n')
return code.split('\n').map(line => indent + line).join('\n')
}

/**
* @param {String} name Slash-separated field name
*/
getField (name) {
let path = name.split('/')
let i = this.scopeStack.length - 1
Expand Down Expand Up @@ -210,6 +261,10 @@ class Compiler {
throw new Error('Unknown field ' + path)
}

/**
* Generates code to eval
* @private
*/
generate () {
this.scopeStack = [{}]
let functions = []
Expand All @@ -218,7 +273,7 @@ class Compiler {
}
for (const type in this.types) {
if (!functions[type]) {
if (this.types[type] !== 'native') {
if (this.types[type] !== NATIVE) {
functions[type] = this.compileType(this.types[type])
if (functions[type].startsWith('ctx')) { functions[type] = this.wrapCode(functions[type]) }
if (!isNaN(functions[type])) { functions[type] = this.wrapCode(' return ' + functions[type]) }
Expand All @@ -234,7 +289,7 @@ class Compiler {

/**
* Compile the given js code, providing native.{type} to the context, return the compiled types
* @param {*} code
* @param {String} code
*/
compile (code) {
// Local variable to provide some context to eval()
Expand All @@ -247,29 +302,18 @@ class Compiler {
class ReadCompiler extends Compiler {
constructor () {
super()

this.addTypes(conditionalDatatypes.Read)
this.addTypes(structuresDatatypes.Read)
this.addTypes(utilsDatatypes.Read)

// Add default types
for (const key in numeric) {
this.addNativeType(key, numeric[key][0])
}
for (const key in utils) {
this.addNativeType(key, utils[key][0])
}
this.addTypes(defaultDatatypes.Read)
}

compileType (type) {
if (type instanceof Array) {
if (this.parameterizableTypes[type[0]]) { return this.parameterizableTypes[type[0]](this, type[1]) }
if (this.types[type[0]] && this.types[type[0]] !== 'native') {
if (this.types[type[0]] && this.types[type[0]] !== NATIVE) {
return this.wrapCode('return ' + this.callType(type[0], 'offset', Object.values(type[1])))
}
throw new Error('Unknown parametrizable type: ' + type[0])
} else { // Primitive type
if (type === 'native') return 'null'
if (type === NATIVE) return 'null'
if (this.types[type]) { return 'ctx.' + type }
return this.primitiveTypes[type]
}
Expand All @@ -282,7 +326,7 @@ class ReadCompiler extends Compiler {

callType (type, offsetExpr = 'offset', args = []) {
if (type instanceof Array) {
if (this.types[type[0]] && this.types[type[0]] !== 'native') {
if (this.types[type[0]] && this.types[type[0]] !== NATIVE) {
return this.callType(type[0], offsetExpr, Object.values(type[1]))
}
}
Expand All @@ -297,29 +341,18 @@ class ReadCompiler extends Compiler {
class WriteCompiler extends Compiler {
constructor () {
super()

this.addTypes(conditionalDatatypes.Write)
this.addTypes(structuresDatatypes.Write)
this.addTypes(utilsDatatypes.Write)

// Add default types
for (const key in numeric) {
this.addNativeType(key, numeric[key][1])
}
for (const key in utils) {
this.addNativeType(key, utils[key][1])
}
this.addTypes(defaultDatatypes.Write)
}

compileType (type) {
if (type instanceof Array) {
if (this.parameterizableTypes[type[0]]) { return this.parameterizableTypes[type[0]](this, type[1]) }
if (this.types[type[0]] && this.types[type[0]] !== 'native') {
if (this.types[type[0]] && this.types[type[0]] !== NATIVE) {
return this.wrapCode('return ' + this.callType('value', type[0], 'offset', Object.values(type[1])))
}
throw new Error('Unknown parametrizable type: ' + type[0])
} else { // Primitive type
if (type === 'native') return 'null'
if (type === NATIVE) return 'null'
if (this.types[type]) { return 'ctx.' + type }
return this.primitiveTypes[type]
}
Expand All @@ -332,7 +365,7 @@ class WriteCompiler extends Compiler {

callType (value, type, offsetExpr = 'offset', args = []) {
if (type instanceof Array) {
if (this.types[type[0]] && this.types[type[0]] !== 'native') {
if (this.types[type[0]] && this.types[type[0]] !== NATIVE) {
return this.callType(value, type[0], offsetExpr, Object.values(type[1]))
}
}
Expand All @@ -347,18 +380,7 @@ class WriteCompiler extends Compiler {
class SizeOfCompiler extends Compiler {
constructor () {
super()

this.addTypes(conditionalDatatypes.SizeOf)
this.addTypes(structuresDatatypes.SizeOf)
this.addTypes(utilsDatatypes.SizeOf)

// Add default types
for (const key in numeric) {
this.addNativeType(key, numeric[key][2])
}
for (const key in utils) {
this.addNativeType(key, utils[key][2])
}
this.addTypes(defaultDatatypes.SizeOf)
}

/**
Expand All @@ -374,18 +396,18 @@ class SizeOfCompiler extends Compiler {
} else {
this.native[type] = fn
}
this.types[type] = 'native'
this.types[type] = NATIVE
}

compileType (type) {
if (type instanceof Array) {
if (this.parameterizableTypes[type[0]]) { return this.parameterizableTypes[type[0]](this, type[1]) }
if (this.types[type[0]] && this.types[type[0]] !== 'native') {
if (this.types[type[0]] && this.types[type[0]] !== NATIVE) {
return this.wrapCode('return ' + this.callType('value', type[0], Object.values(type[1])))
}
throw new Error('Unknown parametrizable type: ' + type[0])
} else { // Primitive type
if (type === 'native') return 'null'
if (type === NATIVE) return 'null'
if (!isNaN(this.primitiveTypes[type])) return this.primitiveTypes[type]
if (this.types[type]) { return 'ctx.' + type }
return this.primitiveTypes[type]
Expand All @@ -399,7 +421,7 @@ class SizeOfCompiler extends Compiler {

callType (value, type, args = []) {
if (type instanceof Array) {
if (this.types[type[0]] && this.types[type[0]] !== 'native') {
if (this.types[type[0]] && this.types[type[0]] !== NATIVE) {
return this.callType(value, type[0], Object.values(type[1]))
}
}
Expand Down
Loading