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

Replace bitops module with pure-Lua isbitset function #72

Merged
merged 1 commit into from
Mar 28, 2024

Conversation

RiskoZoSlovenska
Copy link
Contributor

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.

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
Copy link
Collaborator

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.

@rolandlo rolandlo merged commit 24e7485 into libvips:master Mar 28, 2024
15 checks passed
@RiskoZoSlovenska RiskoZoSlovenska deleted the custom-band branch March 28, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants