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

Improve LuaTimer performance #5453

Merged
merged 2 commits into from
Dec 25, 2022

Conversation

sturnclaw
Copy link
Member

@sturnclaw sturnclaw commented Dec 25, 2022

The first of several PRs being split out from the Scout Mission branch. Don't expect a long shelf-life before merge.

This PR refactors the LuaTimer implementation to use a sorted C++ queue to store a list of "timeouts" rather than walking an unordered Lua table to see if a timer is ready to fire yet. This should improve worst-case per-frame performance of the timer implementation, though it does make the worst-case insertion performance of queuing a new timer O(log N) + O(N/2) instead of a classic hashtable insert O(log N). If this does prove to be an issue in the future with many 100s of timers being created each second, another approach might be a sorted intrusive-linked-list. EDIT: that's not an option because of the O(N) search time to find an insertion point...

LuaTimer has not (yet) shown up as a major performance issue, but this work also lays the groundwork for future improvements like cancelling timers and #5222.

- Use a C++ sorted deque to store timeout entries, significantly reduce overhead of iterating the list of timers every frame
- Timer insertion cost is higher (roughly O(N/2) worst-case), but that's generally less of a problem when timers have a long timeout and they're created irregularly
- Use std::lower_bound for log-search in the timeouts list
- Also document LuaSerializer for NaturalDocs as well
@sturnclaw sturnclaw merged commit b1b667a into pioneerspacesim:master Dec 25, 2022
@sturnclaw sturnclaw deleted the lua-timer-improvement branch December 26, 2022 01:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant