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
56local completion_append56local completion_append
57local last_text57local last_text
58local last_cursor_position58local last_cursor_position
59local commands59
60local cache = {}
61
60 62
61local function get_commands()63local function get_commands()
62 if not commands then64 if cache["commands"] then
63 commands = mp.get_property_native("command-list")65 return cache["commands"]
64 end66 end
65 67
66 return commands68 cache["commands"] = mp.get_property_native("command-list")
67end69 table.sort(cache["commands"], function(c1, c2)
68
69local function help_command(param)
70 table.sort(get_commands(), function(c1, c2)
71 return c1.name < c2.name70 return c1.name < c2.name
72 end)71 end)
73 72
73 return cache["commands"]
74end
75
76local function help_command(param)
74 local output = ""77 local output = ""
75 if param == "" then78 if param == "" then
76 output = "Available commands:\n"79 output = "Available commands:\n"
@@ -148,17 +151,21 @@ local function command_list() · 71 unmodified lines
148end151end
149 152
150local function property_list()153local 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")
152 159
153 for _, sub_property in pairs({"video", "audio", "sub", "sub2"}) do160 for _, sub_property in pairs({"video", "audio", "sub", "sub2"}) do
154 properties[#properties + 1] = "current-tracks/" .. sub_property161 table.insert(cache["property-list"], "current-tracks/" .. sub_property)
155 end162 end
156 163
157 for _, sub_property in pairs({"text", "text-primary"}) do164 for _, sub_property in pairs({"text", "text-primary"}) do
158 properties[#properties + 1] = "clipboard/" .. sub_property165 table.insert(cache["property-list"], "clipboard/" .. sub_property)
159 end166 end
160 167
161 return properties168 return cache["property-list"]
162end169end
163 170
164local function profile_list()171local function profile_list()
@@ -171,28 +178,49 @@ local function profile_list() · 6 unmodified lines
171 return profiles178 return profiles
172end179end
173 180
181local 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]
197end
198
174local function list_option_list()199local function list_option_list()
175 local opts = {}200 if cache["option-list"] then
201 return cache["option-list"]
202 end
203 cache["option-list"] = {}
176 204
177 -- Don"t log errors for renamed and removed properties.205 -- Don't log errors for renamed and removed properties.
178 -- (Just mp.enable_messages("fatal") still logs them to the terminal.)206 -- (Just mp.enable_messages("fatal") still logs them to the terminal.)
179 local msg_level_backup = mp.get_property("msg-level")207 local msg_level_backup = mp.get_property("msg-level")
180 mp.set_property("msg-level", msg_level_backup == "" and "cplayer=no"208 mp.set_property("msg-level", msg_level_backup == "" and "cplayer=no"
181 or msg_level_backup .. ",cplayer=no")209 or msg_level_backup .. ",cplayer=no")
182 210
183 for _, option in pairs(mp.get_property_native("options")) do211 for _, option in pairs(mp.get_property_native("options")) do
184 if mp.get_property("option-info/" .. option .. "/type", ""):find(" list$") then212 if option_info(option, "type", ""):find(" list$") then
185 opts[#opts + 1] = option213 table.insert(cache["option-list"], option)
186 end214 end
187 end215 end
188 216
189 mp.set_property("msg-level", msg_level_backup)217 mp.set_property("msg-level", msg_level_backup)
190 218
191 return opts219 return cache["option-list"]
192end220end
193 221
194local function list_option_action_list(option)222local function list_option_action_list(option)
195 local type = mp.get_property("option-info/" .. option .. "/type")223 local type = option_info(option, "type")
196 224
197 if type == "Key/value list" then225 if type == "Key/value list" then
198 return {"add", "append", "set", "remove"}226 return {"add", "append", "set", "remove"}
@@ -222,10 +250,16 @@ local function list_option_value_list(option) · 23 unmodified lines
222end250end
223 251
224local function has_file_argument(candidate_command)252local 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
225 for _, command in pairs(get_commands()) do258 for _, command in pairs(get_commands()) do
226 if command.name == candidate_command then259 if command.name == candidate_command then
227 return command.args[1] and260 cache[key] = command.args[1] and
228 (command.args[1].name == "filename" or command.args[1].name == "url")261 (command.args[1].name == "filename" or command.args[1].name == "url")
262 return cache[key]
229 end263 end
230 end264 end
231end265end
@@ -260,7 +294,7 @@ local function handle_file_completion(before_cur) · 28 unmodified lines
260end294end
261 295
262local function handle_choice_completion(option, before_cur)296local function handle_choice_completion(option, before_cur)
263 local info = mp.get_property_native("option-info/" .. option, {})297 local info = option_info(option, nil, {})
264 298
265 if info.type == "Flag" then299 if info.type == "Flag" then
266 return { "no", "yes" }, before_cur300 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