libvips / lua-vips

Lua binding for the libvips image processing library
MIT License
125 stars 10 forks source link

Replace `bitops` module with pure-Lua `isbitset` function #72

Closed RiskoZoSlovenska closed 5 months ago

RiskoZoSlovenska commented 5 months ago

This PR replaces the bitops module — used only to check whether certain bits are set — with a pure-Lua function that does just that.

I've benchmarked this change: With bitops (current behaviour):

Lua 5.2: 0.1465 ± 0.00022
Lua 5.4: 0.1287 ± 0.00023
LuaJIT: 0.0925 ± 0.00014

With isbitset (this PR):

Lua 5.2: 0.1518 ± 0.00029
Lua 5.4: 0.1296 ± 0.00021
LuaJIT: 0.0935 ± 0.00016
The benchmark Using vips 8.15.1, running on an Intel i3-4010U. ```lua local vips = require("vips") local Image = vips.Image local hasBit = pcall(require, "vips.bitops") print(hasBit and "Using bitops" or "Using custom") local origImg = Image.black(500, 500) local samples = 1000 local iter = 500 local sum = 0 local times = {} for i = 1, samples do local img = origImg local start = os.clock() for _ = 1, iter do img = img:sin() end local stop = os.clock() - start collectgarbage() collectgarbage() times[i] = stop sum = sum + stop end local avg = sum / samples local diffsum = 0 for _, time in ipairs(times) do diffsum = diffsum + (avg - time)^2 end local stdev = math.sqrt(diffsum / (samples - 1)) print("Average: " .. avg) print("Stdev: " .. stdev) print("Sterr: " .. stdev / math.sqrt(samples)) ```

Calling operations is only 1-4% slower; such slowdown will most likely not be noticeable in practice.

rolandlo commented 5 months ago

Thanks a lot for the PR!

Here are my results: With libvips 8.15.1 on a Intel i7-1165G7 (on Linux)

With bitops:

Lua 5.4:  0.03824 ± 0.00100
Lua 5.3:  0.03920 ± 0.00191
Lua 5.2:  0.04106 ± 0.00240
Lua 5.1:  0.03863 ± 0.00208
LuaJIT:   0.02207 ± 0.00193

With isbitset:

Lua 5.4:  0.03624 ± 0.00281
Lua 5.3:  0.03932 ± 0.00349
Lua 5.2:  0.03953 ± 0.00121
Lua 5.1:  0.03877 ± 0.00137
LuaJIT:   0.02069 ± 0.00076

So the performance is completely fine.