Skip to content

Commit

Permalink
Optimize lib.normalize
Browse files Browse the repository at this point in the history
lib.normalize is a pretty hot loop and was (and still is) the vast
majority of ammo's startup and staging time.

The main bottleneck in it was using path.parent, which is slow for
whatever reasons.

```
str(my_path).rsplit("/", 1)[0]
```

achieves the same thing as `my_path.parent`, but it's much faster. So do
that instead.
  • Loading branch information
cyberrumor committed Nov 15, 2024
1 parent 3222ce0 commit 601b9d7
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions ammo/lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,31 @@
"seq",
]

REPLACEMENTS = {
# Order matters
"data files": "Data Files",
"data": "Data",
# Order matters
"edit scripts": "Edit Scripts",
"scripts": "Scripts",
"dyndolod": "DynDOLOD",
"plugins": "Plugins",
"netscriptframework": "NetScriptFramework",
"skse": "SKSE",
"docs": "Docs",
"source": "Source",
}


def normalize(destination: Path, dest_prefix: Path) -> Path:
"""
Prevent folders with the same name but different case
from being created.
"""
path = destination.parent
file = destination.name
local_path = str(path).split(str(dest_prefix))[-1].lower()
for i in [
"NetScriptFramework",
"Data Files",
"Data",
"DynDOLOD",
"Plugins",
"SKSE",
"Edit Scripts",
"Docs",
"Scripts",
"Source",
]:
local_path = local_path.replace(i.lower(), i)

new_dest: Path = Path(dest_prefix / local_path.lstrip("/"))
result = new_dest / file
return result
# pathlib.parent is slow, get the parent with string manipulation instead.
path, file = str(destination).rsplit("/", 1)
prefix_lower = str(dest_prefix).lower()
local_path = path.lower().split(prefix_lower)[-1]
for key, value in REPLACEMENTS.items():
local_path = local_path.replace(key, value)
return dest_prefix / local_path.lstrip("/") / file

0 comments on commit 601b9d7

Please sign in to comment.