Performance tweaks in core/timer.lua #337
Merged
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.
The definition of call_timers() in core/timer.lua is moved out of
run_to_time() to avoid generation of the non-compileable bytecode
UCLO (bytecode 51 in the current version of LuaJIT).
Wrapping the call to ffi.C.get_time_ns() in run() with tonumber()
avoids the allocation of a cdata object that is difficult for the sink
optimizer to remove. Here is the IR of a typical trace without tonumber():
0036 > p32 UREFC timer.lua:22 #0
0037 > udt ULOAD 0036
0038 > p32 EQ 0037 [0x4123cfa0]
0039 r15 u64 CALLXS 0x408c78
0040 rax > cdt CNEWI +12 0039
0041 > fun EQ 0035 timer.lua:37
The allocation of the uint64_t cdata object (CNEWI +12) at 0040 is not sunk. With tonumber():
0050 > p32 UREFC timer.lua:22 #0
0051 > udt ULOAD 0050
0052 > p32 EQ 0051 [0x40465fa0]
0053 rax u64 CALLXS 0x408c78
0054 {sink} cdt CNEWI +12 0053
0055 > fun EQ 0049 tonumber
0056 xmm7 num CONV 0053 num.u64
0057 > fun EQ 0035 timer.lua:37
The allocation at 0054 is trivially sunk due to tonumber() acting directly on the return value of the call to get_time_ns().