titan-lang / titan

The Titan programming language
http://titan-lang.org
MIT License
406 stars 13 forks source link

only push temporaries that can be live at a gc checkpoint #217

Closed mascarenhas closed 6 years ago

mascarenhas commented 6 years ago

I based this PR on top of the records one, so it should be merged only after that one, as both are pretty invasive on the code generator.

This PR implements an optimization where we try to push GC-able temporaries only prior to intra-statement function calls (a possible GC point). The other GC points that we have, loops, do not count, as by definition temporaries are not live across statements. I am pretty confident that I have covered all possible bases so we will not have any dangling pointers, but of course I have not formally proven that the optimization is sound. 😈

This should really cut into the stack usage of code that uses complex expressions. The next step is to do liveness analysis on local variables, so a similar optimization can be done for them too, but this can go in another PR.

By the way, I recall that the Lua test suite compiles Lua with some flag where the GC is very aggressive so dangling pointers surface more easily, I think we should do something similar in our test suite so we can have effective GC tests.

codecov-io commented 6 years ago

Codecov Report

Merging #217 into master will increase coverage by 0.13%. The diff coverage is 89.62%.

Impacted Files Coverage Δ
titan-compiler/coder.lua 96.01% <89.62%> (-0.34%) :arrow_down:
titan-compiler/util.lua 61.81% <0%> (-7.28%) :arrow_down:
titan-compiler/checker.lua 90.12% <0%> (+0.61%) :arrow_up:
titan-compiler/driver.lua 63.08% <0%> (+3.51%) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update bfd59ac...7036403. Read the comment docs.

mascarenhas commented 6 years ago

This branch got really broken, as it should just have the last two commits instead of duplicates of all the record-related commits. @hishamhm, I have no intention to merge this for the time being, but it is a good idea to fix it so I can keep it tracking the master branch.

hishamhm commented 6 years ago

@mascarenhas Rebased the PR!

mascarenhas commented 6 years ago

Thanks @hishamhm!

mascarenhas commented 6 years ago

I really need to make a habit of using git rebase -i instead of just rebase, would have saved me an hour of headaches when updating this branch.

mascarenhas commented 6 years ago

If it gets too troublesome to keep making this track master I will just drop this, we can do this optimization properly once we have a language that is actually useful and pleasant to program in so we will have time to focus solely on improving the code generator.

hishamhm commented 6 years ago

git rebase -i is indeed your best friend when doing rebases!

Other tips:

mascarenhas commented 6 years ago

Thanks for the tips. This tool looks very interesting, is free for open source projects, and seems easy to write Lua backend, but the current version if Windows-only (they are working on Linux/OSX versions, though).