- LuaJIT将具有常数键的表访问从循环中提取出来,因此不再(那么)需要以局部变量缓存模块函数。
- LuaJIT从循环中提取常量分支,因此可以在循环中使用
if/else
或and/or
来限定循环内核。 - LuaJIT会将函数内联(除非把
...
和select()
与非常量索引一起使用),因此使用复合函数函数限定循环内核是可行的。 - 乘法和加法比访问内存的开销要小,所以将这些运算的结果存储在临时变量里可能会降低性能(寄存器溢出更多)
- 使用
if/else
语句和使用and/or
表达式没有区别——它们会产生相同的破坏管道的分支代码(所以要避免在紧密循环中使用非常数and/or
运算符的表达式)。 - 在x86上除法比乘法慢4倍,所以当除以一个常数的时候,(由于常数表达式是折叠的)将
x / c
转换为x * (1 / c)
——LuaJIT已经为2的幂常量做了这件事,其语义是等价的。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('zip') | |
require('xml_parse') | |
local excel = zip_open('excel/test.xlsx') | |
if not excel then return end | |
local flag | |
flag = excel:find('xl/workbook.xml') | |
if flag then | |
local str = excel:read('*a') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local punct_space = lexer.punct + lexer.space | |
-- Handles flanking delimiters as described in | |
-- https://github.github.com/gfm/#emphasis-and-strong-emphasis in the cases | |
-- where simple delimited ranges are not sufficient. | |
local function flanked_range(s, not_inword) | |
local fl_char = lexer.any - s - lexer.space | |
local left_fl = lpeg.B(punct_space - s) * s * #fl_char + | |
s * #(fl_char - lexer.punct) | |
local right_fl = lpeg.B(lexer.punct) * s * #(punct_space - s) + |
这是一个Lua习语和技巧的集合,对于随意阅读代码的人来说,可能不会立即看到。
逻辑 | |
---|---|
not a == not b |
两者都满足或都不满足 |
数字 | |
math.min(math.max(x, min), max) |
把x限制在[min, max]区间内 |
x ~= x |
一个数是NaN |