replace hardcoded hookInterval with BogoMIPS calculator #2877
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
During my development on Kallisti, I decided to look into OpenComputers's performance characteristics relative to native Lua 5.3.
During my research, I found out that the default hookInterval (100 Lua VM instructions) is far too low, as the cost of the deadline detection hook seems to be in the tens of Lua VM instructions by itself (plus a JNI escape in computer.realTime()!). I found that setting it to, say, 10000, improves performance in Lua scripts by about 60-70% (which is pretty good - faster performance means less deadline hitting, after all).
However, on very fast computers 10000 might still be too low and on very slow computers 10000 might be too high. For this reason, I wrote a simple "BogoMIPS"-esque calculator that tries to calculate a (very rough - for one, it still eats up time in the hook itself) count of executed instructions in a second, then sets the hookInterval to 0.005 times that (so a rough count of executed instructions in roughly 5 milliseconds, as that is still much better than the necessary precision).
tl;dr Boosts Lua script performance by 60-70%, looks pretty good. Should probably be forwarded to all repositories.
(If you're curious, the act of setting a debug.sethook() on VM instructions alone adds 50% of overhead. But I don't think we can quite get rid of that, for obvious reasons - Lua does not make any threading guarantees.)