Coder Social home page Coder Social logo

Optimize tail calls about koto HOT 1 OPEN

koto-lang avatar koto-lang commented on July 22, 2024
Optimize tail calls

from koto.

Comments (1)

irh avatar irh commented on July 22, 2024

I looked at this a bit today with @alisomay and here are some notes from our discussion:

  • The optimization opportunity is in reusing registers in the value stack, rather than pushing a new frame's worth of registers into the stack.
  • The call stack (containing frame info) can be left alone for the time being, we wouldn't want to lose the stack trace in the case of an error, so at least the chunk and ip for each frame will be needed.
    • There may be a compression optimization possible when the chunk and ip match in subsequent frames..
  • A new TailCall instruction can be added which performs a tail call, reusing the current frame's registers.
    • In the case of a binary op, the RHS of the op would be the tail call, the LHS would have already been evaluated. The LHS result then needs to be cached, probably in register 0, and then the tail call frame base would be register 1, with result register also 1. The op can then be executed following the tail call before the function exits.
      • In the case of a binary op and the current frame's frame base is unused, then it would be possible to cache the LHS result in the frame base rather than register 0. How this would work exactly isn't clear (it would effectively be saying to the op that the LHS is in register -1), but it would still be a win to cache in register 0 so I think this doesn't have to be figured out to move forward.
  • To compile the TailCall instruction, the compiler could pass an 'allow tail call' flag around while compiling nodes. This flag would be set to true when compiling a return expression, and then set to false when a tail call should be explicitly disallowed (e.g. when compiling the LHS of a binary op).
    • The 'result register' which is passed around in the 'compile node' functions could be replaced with a context struct (similar to the one used by the parser) which can then be extended with the new tail call flag.

from koto.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.