question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

implement stack-based interpreter to improve performance

See original GitHub issue

currently sval is a tree-walking interpreter and has a ordinary performance. compile the code into some kinds of intermediate representation based on stack to improve performance.

I have a demo as followed. it may be difficult and take some time to migrate.

const { parse } = require('acorn')
const { simple } = require('acorn-walk')

const ast = parse('1 + 8 * 2 / 2 + 1')

const stack = []

simple(ast, {
  Literal(node) {
    stack.push(node.value)
  },
  BinaryExpression(node) {
    const right = stack.pop()
    const left = stack.pop()
    const binaryOps = {
      '+': () => left + right,
      '-': () => left - right,
      '*': () => left * right,
      '/': () => left / right
    }
    const handler = binaryOps[node.operator]
    if (handler) {
      stack.push(handler())
    } else {
      throw new SyntaxError(`Unexpected token ${node.operator}`)
    }
  }
})

console.log(stack[0])

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:7
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

3reactions
Siubaakcommented, Jul 4, 2019

When will this performance improvements reach master branch? Any ETA? I’m looking into moving from Interpreter-JS to Sval.

PD: Quite a nice job.

I will try to complete in August

1reaction
Siubaakcommented, May 8, 2019

fengari did a great job of running lua over js. mark for reference - Fengari

<script src="./fengari-web.js"></script>
<script type="application/lua">
  local start = os.time()
  for i=1, 10000000 do
  end
  print(os.time() - start) -- 0~1s cool :)
</script>
<script src="https://unpkg.com/sval"></script>
<script>
  const interpreter = new Sval()
  interpreter.run(`
    var start = Date.now()
    for (var i = 0; i < 10000000; i++) {}
    console.log((Date.now() - start) / 1000) // around 6s :(
  `)
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cheap interpreter, part 6: faster stack machines
The easiest way to improve the performance of the interpreter we defined in part 4 is to pay attention to the data structures...
Read more >
RegCPython: A Register-based Python Interpreter for Better ...
The stack architecture is considered simpler and smaller, while the register architecture is considered more efficient but more complex. CPython ...
Read more >
How I developed a faster Ruby interpreter - Red Hat Developer
To implement a decent MIR-based JIT compiler, I decided earlier to develop some features with a design specific to CRuby. I use dynamically ......
Read more >
Performance comparison between two virtual machine ...
machine implementing in a high level language. We observe that for a stack based machine the performance limit of the interpreter is likely...
Read more >
Stack Caching for Interpreters
The dynamic method is based on having one ... of Ccode are compilation speed and exibility ... whether to use a stack or...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found