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

Move through instructions instead of pushing/ popping #27

Closed
marcusklaas opened this issue Aug 17, 2017 · 2 comments
Closed

Move through instructions instead of pushing/ popping #27

marcusklaas opened this issue Aug 17, 2017 · 2 comments

Comments

@marcusklaas
Copy link
Owner

The current approach works OK, but it has drawbacks.

Firstly, the instruction stack grows very large for deeply recursive calls. This is because we now push the instructions for both branches of a condition.

Further, we clone a vector onto the instruction stack for each function call. Since instructions are not Copy, this is pretty expensive.

It's not immediately clear what the ideal solution here is. We cannot precompile the bytecode for all functions and add them to a large vector since new functions can (only!) be made during runtime. Just pushing to a big vector whenever a function is created at runtime may work, but this may overflow when many one-shot functions are created, for example in (map (lambda (x) (lambda (y) (list x y))) (range 1 10000)).

Ideally, we don't ever clone instructions and yet throw away that which we know won't run any more. One way we may do this is by introducing (another!) stack with an Rc<Vec<Instr>> and usize, where the usize points to current instruction in the instruction vector. Instead of creating two more vectors, these values should probably be combined with the value stack pointer stack into one vector of structs, containing instruction pointer, value pointer and current instruction vector.

@marcusklaas
Copy link
Owner Author

This is mostly implemented now. Instruction vectors are now Rc<RefCell<Vec<Instr>>>, which is a bit inefficient since it may be as many as three indirections to get a vector and some runtime checking (because of the RefCell). We should probably introduce a new type which combines the properties of Rc, RefCell and Vec. It should be possible to have only a single level of indirection and no runtime checks other than the bounds check.

@marcusklaas
Copy link
Owner Author

Closing this and creating a new issue for the instruction store.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant