Skip to content

Commit b2a8bfe

Browse files
committed
update caching & benchmark
1 parent f6a25cc commit b2a8bfe

File tree

2 files changed

+38
-14
lines changed

2 files changed

+38
-14
lines changed

bench.lua

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ end
1919
require"talisman.globals"
2020
require"big-num.omeganum"
2121

22-
local B = to_big
23-
2422
--- @param desc string
2523
--- @param n number
2624
--- @param fn fun()
@@ -29,21 +27,29 @@ local function bench(desc, n, fn)
2927

3028
local t = os.clock()
3129
local m = collectgarbage("count")
30+
local c = Big.created_instances
31+
3232
for i=1, n do fn() end
33+
3334
local td = os.clock() - t
3435
local md = collectgarbage("count") - m
36+
local cd = Big.created_instances - c
3537

36-
print(string.format('%10s - %6.2f ms; %6.2fMB; %.1f ops', desc, td * 1000, md / 1024, n/td))
38+
print(string.format(
39+
'%10s - %7.2f ms; %6.2fMB; %6.0f inst/n; %11.2f bytes/n; %.1f ops',
40+
desc, td * 1000, md / 1024, cd / n, md / n * 1024, n/td
41+
))
3742

3843
collectgarbage("collect")
3944
collectgarbage("restart")
4045
end
4146

42-
local y = B(111)
47+
local y = Big:create(111)
4348
local n
4449

4550
bench("raw", 100000000, function() end)
4651

52+
n = y
4753
bench("add", 100000, function() n = n + y end)
4854

4955
n = y
@@ -59,4 +65,7 @@ n = y
5965
bench("tetrate", 100000, function() n = n:tetrate(4) end)
6066

6167
n = y
62-
bench("arrow", 100000, function() n = n:tetrate(4, 4) end)
68+
bench("arrow", 100000, function() n = n:arrow(3, 3) end)
69+
70+
n = Big:create(2.001)
71+
bench("arrow2", 10, function() n = n:arrow(200, 2.001) end)

big-num/omeganum.lua

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ local TalismanOmega_sizeof = assert(ffi.sizeof("struct TalismanOmega"))
3131
--- @operator mod(t.Omega|number): t.Omega
3232
--- @operator pow(t.Omega|number): t.Omega
3333
--- @operator unm(): t.Omega
34-
local Big = {}
34+
local Big = {
35+
created_instances = 0
36+
}
3537
;(Big).array = {} -- lsp hack, assign without recognized by lsp
3638

3739
OmegaMeta = {
@@ -50,13 +52,18 @@ _G.Big = Big
5052
local B = {}
5153

5254
-- prevent multiple allocation of same number at a frame
55+
--- @class _t.OmegaCache
56+
--- @field list table<number, t.Omega> persisting list
57+
--- @field current table<number, t.Omega> created omegas in this frame, move to old on next frame
58+
--- @field old table<number, t.Omega> created omegas in previous frame, cleared on next frame
5359
local caches = {
54-
--- @type table<number, t.Omega>
5560
list = {},
56-
--- @type table<number, number>
57-
frames = {}
61+
current = {},
62+
currentcount = {},
5863
}
5964

65+
Big.caches = caches
66+
6067
-- prevent overrides
6168
local type = type
6269
local _math = math
@@ -100,6 +107,8 @@ end
100107
--- @param noNormalize? boolean
101108
--- @return t.Omega
102109
function Big:new(arr, sign, noNormalize)
110+
Big.created_instances = Big.created_instances + 1
111+
103112
--- @type t.Omega
104113
local obj = TalismanOmega() --- @diagnostic disable-line
105114
obj.asize = 1
@@ -116,12 +125,12 @@ function Big:create(input, sign)
116125
if obj then return obj end
117126
if input ~= input then return B.NaN end
118127

119-
local obj = Big:new({input}, input < 0 and -1 or 1)
120-
caches.frames[input] = (caches.frames[input] or 0) + 1
121-
if caches.frames[input] > 100 then
122-
caches.frames[input] = nil
128+
obj = caches.current[input] or Big:new({input}, input < 0 and -1 or 1)
129+
caches.currentcount[input] = (caches.currentcount[input] or 0) + 1
130+
if caches.currentcount[input] > 50 then
123131
caches.list[input] = obj
124132
end
133+
125134
return obj
126135
elseif type(input) == "string" then
127136
return Big:parse(input)
@@ -1618,9 +1627,15 @@ end
16181627

16191628
if love then
16201629

1630+
local nextclear = 0
16211631
local update = love.update
16221632
function love.update(...)
1623-
caches.frames = {}
1633+
if G.TIMERS.REAL > nextclear or G.TIMERS.REAL < nextclear - 2 then
1634+
caches.current = {}
1635+
caches.currentcount = {}
1636+
nextclear = G.TIMERS.REAL + 1
1637+
end
1638+
16241639
return update(...)
16251640
end
16261641

0 commit comments

Comments
 (0)