xonsh
xonsh copied to clipboard
Refactoring: commands cache
We need to refactor commands_cache:
-
Do not create one
_cmds_cachelist based on aliases and list of files from PATHs because any change will force rebuilding the cache. Implement layers approach instead of this. Aliases - first layer, files - second layer. Now you can update first or second layer without rebuilding everything. The get function just overlap layers on the fly to extract single row. -
Use list of files from Windows/System32 as a permanent layer and do not re-read it. The System32 diretory is huge and persistent.
For community
⬇️ Please click the 👍 reaction instead of leaving a +1 or 👍 comment
By the way, is there a place in the current xonsh code where a list of strings is split into a character-by-character tree so that you can see for each character whether there are branches that start with this (and previous) chars so that if there aren't you can stop early instead of continuing to search with each new char? You've mentioned something in the other issue, but from my shallow overview command cache just uses a dictionary, so would have to check the full string every time?
dict object uses hash function for search by key it's pretty close to prefix tree by timings
timings per search might be the same, but the number of searches will differ a lot since with a dict you wouldn't know that you don't need to search for the 111111 key because there is not a single key starting with 1, so you stopped searching 5 keystrokes ago
yep, nice feature for commands_cache
FYI You can use CharTrie from this pure Python trie library https://pypi.org/project/pygtrie/ library (archived by Google, but still maintained by someone else?) to test for every typed command whether it's a prefix of any existing file from PATH and if not stop checking on further keystrokes
It even has an example of typing-by-char and checking
You don't have Cython extensions in xonsh, right? Otherwise could check some faster native trie libs, but don't think that would make any noticeable difference