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

Add TCO to Lisp #420

Merged
merged 16 commits into from
Oct 24, 2022
Merged

Add TCO to Lisp #420

merged 16 commits into from
Oct 24, 2022

Conversation

vinc
Copy link
Owner

@vinc vinc commented Oct 21, 2022

Add simple support for Tail Call Optimization by following how it is done on Lispy: http://norvig.com/lispy2.html

@vinc
Copy link
Owner Author

vinc commented Oct 21, 2022

Before a deep recursion would lead to a stack overflow:

> time lisp /tmp/lisp/sum.lsp 100
5050
Executed 'lisp /tmp/lisp/sum.lsp 100' in 0.020997s

> time lisp /tmp/lisp/sum.lsp 1000
500500
Executed 'lisp /tmp/lisp/sum.lsp 1000' in 0.052992s

> time lisp /tmp/lisp/sum.lsp 10000
Error: Could not allocate address 0xffffffffe8

Now deep recursions are possible but everything is a bit slower:

> time lisp /tmp/lisp/sum.lsp 100
5050
Executed 'lisp /tmp/lisp/sum.lsp 100' in 0.023996s

> time lisp /tmp/lisp/sum.lsp 1000
500500
Executed 'lisp /tmp/lisp/sum.lsp 1000' in 0.124981s

> time lisp /tmp/lisp/sum.lsp 10000
50005000
Executed 'lisp /tmp/lisp/sum.lsp 10000' in 28.046145s

@vinc
Copy link
Owner Author

vinc commented Oct 21, 2022

Another speed comparison, before:

> time lisp /tmp/lisp/pi.lsp 1000
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
Executed 'lisp /tmp/lisp/pi.lsp 1000' in 0.180972s

After:

> time lisp /tmp/lisp/pi.lsp 1000
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
Executed 'lisp /tmp/lisp/pi.lsp 1000' in 0.627904s

@vinc
Copy link
Owner Author

vinc commented Oct 22, 2022

Removing the initial cloning of exp improved the perf a little bit:

> time lisp /tmp/lisp/pi.lsp 1000
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
Executed 'lisp /tmp/lisp/pi.lsp 1000' in 0.470081s

@vinc
Copy link
Owner Author

vinc commented Oct 22, 2022

Removing another cloning improved the performance a lot:

> time lisp /tmp/lisp/pi.lsp 1000
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989
Executed 'lisp /tmp/lisp/pi.lsp 1000' in 0.221966s

@vinc vinc marked this pull request as ready for review October 24, 2022 06:46
@vinc vinc merged commit 0a29f53 into trunk Oct 24, 2022
@vinc vinc deleted the feature/lisp-tco branch October 24, 2022 08:44
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

Successfully merging this pull request may close these issues.

1 participant