Software /
code /
prosody
Annotate
util/gc.lua @ 10933:f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Mon, 15 Jun 2020 14:16:10 +0100 |
child | 10935:2d57c49bfa12 |
rev | line source |
---|---|
10933
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 local array = require "util.array"; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 local set = require "util.set"; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
3 |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 local known_options = { |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
5 incremental = set.new { "mode", "threshold", "speed", "step_size" }; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
6 generational = set.new { "mode", "minor_threshold", "major_threshold" }; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
7 }; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
8 |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
9 if _VERSION ~= "5.4" then |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 known_options.generational = nil; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
11 known_options.incremental:remove("step_size"); |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
12 end |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
13 |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
14 local function configure(user, defaults) |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 local mode = user.mode or defaults.mode or "incremental"; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
16 if not known_options[mode] then |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
17 return nil, "GC mode not supported on ".._VERSION..": "..mode; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
18 end |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
19 |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
20 for k, v in pairs(user) do |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
21 if not known_options[mode]:contains(k) then |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
22 return nil, "Unknown GC parameter: "..k; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
23 elseif k ~= "mode" and type(v) ~= "number" then |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
24 return nil, "parameter '"..k.."' should be a number"; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
25 end |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
26 end |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
27 |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
28 if mode == "incremental" then |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
29 if _VERSION == "Lua 5.4" then |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
30 collectgarbage(mode, |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
31 user.threshold or defaults.threshold, |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
32 user.speed or defaults.speed, |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
33 user.step_size or defaults.step_size |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
34 ); |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
35 else |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
36 collectgarbage("setpause", user.threshold or defaults.threshold); |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
37 collectgarbage("setstepmul", user.speed or defaults.speed); |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
38 end |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
39 elseif mode == "generational" then |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
40 collectgarbage(mode, |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
41 user.minor_threshold or defaults.minor_threshold, |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 user.major_threshold or defaults.major_threshold |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 ); |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
44 end |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
45 return true; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 end |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
47 |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
48 return { |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
49 configure = configure; |
f59bc81245b3
util.gc: New module for configuring the Lua garbage collector
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
50 }; |