Unsolicited Programming Suggestion (#UPS): always consider if you've got the right tool for the problem. Example: I was writing a #Python app that needed to import a #Lua file containing a data structure – a tree built using tables, like this:
DATA = {
["node1.1"] = {
["node2.1"] = some_value,
["node2.2"] = other_value,
},
["node1.2"] = {
etc. etc. The structure had several thousand nodes.
My first attempt: oh, there is this luadata Python module, I can use it!
… parsing my Lua file took several seconds. Not microseconds. Not milliseconds. Seconds. On my Ryzen 7 9700X.
Then I noticed that structure is essentially the same as the Python dict I'm going to build, modulo syntactic details, so maybe just use regexp to convert the syntax to pythonic:
{
"node1.1" : {
"node2.1" : some_value,
"node2.2" : other_value
},
and then parse it using ast.literal_eval() (for security reasons I didn't want to simply use eval instead of ast's parser, as the Lua file came from external source). Wow, much faster, ~0.2 s, but still…
Then it dawned on me – convert to #JSON and use json.loads(). Apparently, json's parser is more efficient than ast's parser because JSON syntax is much simpler than generalized Python literal syntax. Got down to 20 ms, something I can live with.