Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add caches to hot functions in utils.py that are called from multiple locations #79

Merged
merged 3 commits into from
Oct 11, 2023

Conversation

AlexWaygood
Copy link
Collaborator

@AlexWaygood AlexWaygood commented Oct 11, 2023

This gives around a 16% 24% speedup when running sphinx-lint on 7 ~large .rst files in CPython.

Benchmark script, that needs to be run from a directory that has a clone of CPython in it
import sys
from sphinxlint.__main__ import main


files = [f"cpython/Doc/library/{module}.rst" for module in ("os", "typing", "sqlite3", "stdtypes", "argparse", "enum")]
files.append("cpython/Doc/reference/datamodel.rst")
args = ["foo"] + files

def test():
    main(args)

If the benchmark script is saved as benchmark.py, run the benchmark script using python -m timeit -s "from benchmark import test" "test()".

Part of #76

@AlexWaygood AlexWaygood marked this pull request as draft October 11, 2023 09:18
@AlexWaygood AlexWaygood marked this pull request as ready for review October 11, 2023 10:52
@AlexWaygood
Copy link
Collaborator Author

I temporarily converted this back to a draft, as I got worried that my benchmark script was unreliable, since timeit doesn't clear caches in between runs of the benchmark. I wrote a more reliable benchmark script, however, and it still shows a clear speedup.

New benchmark script
import statistics
import subprocess
import sys


files = [f"cpython/Doc/library/{module}.rst" for module in ("os", "typing", "sqlite3", "stdtypes", "argparse", "enum")]
files.append("cpython/Doc/reference/datamodel.rst")
argv = ["foo"] + files

script = f"""\
import time
from sphinxlint.__main__ import main
t0 = time.perf_counter()
main({argv})
t1 = time.perf_counter() - t0
raise SystemExit(t1)"""


command = [sys.executable, "-c", "; ".join(script.splitlines())]
timings = [
    float(subprocess.run(command, capture_output=True, text=True).stderr.strip())
    for _ in range(5)
]
print(statistics.mean(timings))

On main:

1.9073304200079293

With this PR branch:

1.685813060007058

@hugovk
Copy link
Collaborator

hugovk commented Oct 11, 2023

A 6% improvement on an 8-core macOS M2 (the basic benchmark script limits the number of files so multiprocessing isn't used):

  • main: 0.8339760334005405
  • PR: 0.7873101998004131

A 17% improvement when adjusting the benchmark script to run on all 293 Doc/library/*.rst (uses multiprocessing):

  • main: 1.5562639416013553
  • PR: 1.2917625166002835

Copy link
Collaborator

@hugovk hugovk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice improvement! Almost too easy!

@hugovk hugovk merged commit dc0514d into sphinx-contrib:main Oct 11, 2023
@AlexWaygood AlexWaygood deleted the caches branch October 11, 2023 14:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants