History

commands.lua: add cache for often queried constant properties

commands, properties, options, options info, never changes during runtime, so we can cache them to make the value instantly available.

Kacper Michajłow committed 1 year ago

Commit f751f211 parent d37521b1 file · +55 −21Browse files

Unified · Split

player/lua/commands.lua+55−21
@@ -56,21 +56,24 @@ local completion_pos · 55 unmodified lines
5656 local completion_append
5757 local last_text
5858 local last_cursor_position
59−local commands
59+
60+local cache = {}
61+
6062
6163 local function get_commands()
62− if not commands then
63− commands = mp.get_property_native("command-list")
64+ if cache["commands"] then
65+ return cache["commands"]
6466 end
6567
66− return commands
67−end
68−
69−local function help_command(param)
70− table.sort(get_commands(), function(c1, c2)
68+ cache["commands"] = mp.get_property_native("command-list")
69+ table.sort(cache["commands"], function(c1, c2)
7170 return c1.name < c2.name
7271 end)
7372
73+ return cache["commands"]
74+end
75+
76+local function help_command(param)
7477 local output = ""
7578 if param == "" then
7679 output = "Available commands:\n"
@@ -148,17 +151,21 @@ local function command_list() · 71 unmodified lines
148151 end
149152
150153 local function property_list()
151− local properties = mp.get_property_native("property-list")
154+ if cache["property-list"] then
155+ return cache["property-list"]
156+ end
157+
158+ cache["property-list"] = mp.get_property_native("property-list")
152159
153160 for _, sub_property in pairs({"video", "audio", "sub", "sub2"}) do
154− properties[#properties + 1] = "current-tracks/" .. sub_property
161+ table.insert(cache["property-list"], "current-tracks/" .. sub_property)
155162 end
156163
157164 for _, sub_property in pairs({"text", "text-primary"}) do
158− properties[#properties + 1] = "clipboard/" .. sub_property
165+ table.insert(cache["property-list"], "clipboard/" .. sub_property)
159166 end
160167
161− return properties
168+ return cache["property-list"]
162169 end
163170
164171 local function profile_list()
@@ -171,28 +178,49 @@ local function profile_list() · 6 unmodified lines
171178 return profiles
172179 end
173180
181+local function option_info(option, prop, default)
182+ local key = "option-info/" .. option
183+ cache[key] = cache[key] or mp.get_property_native(key, false)
184+
185+ if not cache[key] then
186+ return default
187+ end
188+
189+ if prop then
190+ if cache[key][prop] ~= nil then
191+ return cache[key][prop]
192+ end
193+ return default
194+ end
195+
196+ return cache[key]
197+end
198+
174199 local function list_option_list()
175− local opts = {}
200+ if cache["option-list"] then
201+ return cache["option-list"]
202+ end
203+ cache["option-list"] = {}
176204
177− -- Don"t log errors for renamed and removed properties.
205+ -- Don't log errors for renamed and removed properties.
178206 -- (Just mp.enable_messages("fatal") still logs them to the terminal.)
179207 local msg_level_backup = mp.get_property("msg-level")
180208 mp.set_property("msg-level", msg_level_backup == "" and "cplayer=no"
181209 or msg_level_backup .. ",cplayer=no")
182210
183211 for _, option in pairs(mp.get_property_native("options")) do
184− if mp.get_property("option-info/" .. option .. "/type", ""):find(" list$") then
185− opts[#opts + 1] = option
212+ if option_info(option, "type", ""):find(" list$") then
213+ table.insert(cache["option-list"], option)
186214 end
187215 end
188216
189217 mp.set_property("msg-level", msg_level_backup)
190218
191− return opts
219+ return cache["option-list"]
192220 end
193221
194222 local function list_option_action_list(option)
195− local type = mp.get_property("option-info/" .. option .. "/type")
223+ local type = option_info(option, "type")
196224
197225 if type == "Key/value list" then
198226 return {"add", "append", "set", "remove"}
@@ -222,10 +250,16 @@ local function list_option_value_list(option) · 23 unmodified lines
222250 end
223251
224252 local function has_file_argument(candidate_command)
253+ local key = "command-file/" .. candidate_command
254+ if cache[key] ~= nil then
255+ return cache[key]
256+ end
257+
225258 for _, command in pairs(get_commands()) do
226259 if command.name == candidate_command then
227− return command.args[1] and
228− (command.args[1].name == "filename" or command.args[1].name == "url")
260+ cache[key] = command.args[1] and
261+ (command.args[1].name == "filename" or command.args[1].name == "url")
262+ return cache[key]
229263 end
230264 end
231265 end
@@ -260,7 +294,7 @@ local function handle_file_completion(before_cur) · 28 unmodified lines
260294 end
261295
262296 local function handle_choice_completion(option, before_cur)
263− local info = mp.get_property_native("option-info/" .. option, {})
297+ local info = option_info(option, nil, {})
264298
265299 if info.type == "Flag" then
266300 return { "no", "yes" }, before_cur

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t