Skip to content

Optimize Promise and Await (fixes #1499), fix Promise.resolve bug#1530

Merged
Perryvw merged 2 commits intoTypeScriptToLua:masterfrom
rhazarian:fix-promise
Feb 4, 2024
Merged

Optimize Promise and Await (fixes #1499), fix Promise.resolve bug#1530
Perryvw merged 2 commits intoTypeScriptToLua:masterfrom
rhazarian:fix-promise

Conversation

@rhazarian
Copy link
Contributor

@rhazarian rhazarian commented Feb 3, 2024

Fixes #1499.

I've tried to find a shaky balance between readable and optimized code. I've refactored almost all of the recursive calls into tail calls, and also prevented calling coroutine.resume from inside pcall's. Additionally, I got rid of some of the unnecessary allocations, and discovered & fixed a bug with Promise.resolve (tests added).

The new Promise + Async version can handle virtually any amount of chained waits. Due to the coroutine.resume/pcall optimization, C stack doesn't grow, and due to tail calls Lua stack doesn't grow as well. It stays constant and very small: http://tpcg.io/_ZQYND5

Copy link
Member

@Perryvw Perryvw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@Perryvw Perryvw merged commit 223b49e into TypeScriptToLua:master Feb 4, 2024
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.

C stack overflow when using await in a loop

2 participants