stable-diffusion-webui
stable-diffusion-webui copied to clipboard
sort embeddings by name (case insensitive)
Describe what this pull request is trying to achieve.
The way we currently load embedding files from disk and store them in memory as word_embeddings is not sorted, making searching through the UI needlessly difficult if you have a large set of files and don't already know the exact name of one you want to use. This PR alphabetically sorts the list of embeddings immediately after they have all been read from disk, making visual navigation much easier.
Additional notes and description of your changes
I noticed that the surrounding code is calling self.word_embeddings.clear() on instead of just re-instantiating it like self.word_embeddings = {}; so, I assume (but have not verified) that other objects may be retaining a reference to it. To play it safe, my change sorts the contents into a second local dict, clears the main one, and then updates the main one with the contents of the sorted dict.
In my manual testing through various web UI functionalities, this seems to be perfectly compatible. A happy side effect is that when the list of embeddings is printed to standard out at startup or when loading models, they're sorted now too.
Environment this was tested in
- OS: Linux
- Browser: Firefox
- Graphics card: NVIDIA GeForce RTX 3060 Ti 8GB
Screenshots or videos of your changes
Before this change. Booo unsorted contents! :-1:

After this change. Yay sorted content! :tada:

On second thought, I'm taking this back to draft to rewrite it using a plain old dict because I didn't realize recent versions of Python preserve dict order and are more performant than OrderedDict. (Alas, I've been supporting years-old Python code elsewhere for too long…)
As mentioned above, I have dropped the OrderedDict and stuck with a plain Python dict. Little did I know that native dicts have been guaranteeing order of insertion in the CPython implementation since 3.7. Quote from Guido for anyone who may be curious: https://mail.python.org/pipermail/python-dev/2017-December/151283.html