NickHugi / PyKotor

A Python library that can read and modify most file formats used by the game Knights of the Old Republic and its sequel.
GNU Lesser General Public License v3.0
11 stars 3 forks source link

Massively improve pathlib override performance #94

Closed th3w1zard1 closed 5 months ago

th3w1zard1 commented 5 months ago

EDIT: I forgot to merge some of the files, see #95 for code.

Been terrified to touch this class since the initial addition of CaseAwarePath since HoloPatcher for Linux/Mac relies on it so heavily but it's finally time to address this problem.

The overrides are fine but they do not do enough caching. Specifically fix_path_formatting which is designed to fix repeated slashes and mixed slashes. Uses precompiled regex but even that's too slow. The main problem is when loading an installation on the Toolset the function can be called hundreds of thousands of times.

Since many of those calls are probably the same paths, we can cache at the class level (ensuring compatibility with CaseAwarePath) by using the quick hack solution @lru_cache(max=10000).

Next was the regex. The regex was written to support network paths and SMB's, but I highly doubt anyone's using them (EDIT: actually pathlib doesn't even support network paths. TIL...). Saves another chunk of time by swapping to os.path.normpath(). This does change behavior of the pathlib overrides (see the changes made to the tests). If it's an issue we can address in another PR.