-
Notifications
You must be signed in to change notification settings - Fork 10
perf: Cache redundant work in transform phase #40
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
Open
danceratopz
wants to merge
14
commits into
SamWilsn:master
Choose a base branch
from
danceratopz:cache-optimizations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b90f62e
Fix glob patterns in template packaging
danceratopz 0d59b5c
Add test extras with pytest-cov dependency
danceratopz f748bc6
Include test extras in tox environments
danceratopz 2373f68
Add pytest and coverage configuration
danceratopz 6f0caf1
Add development section to README
danceratopz 17b9061
Add comprehensive test suite
danceratopz 3d700a3
feat(perf): Cache entry_points() at module level in HTMLVisitor.
danceratopz 5ef3f2f
feat(perf): Cache entry_points() at module level in Loader.
danceratopz 08e6670
feat(perf): Cache dataclasses.fields() at class level in PythonNode.
danceratopz c15b457
feat(perf): Cache _BoundsVisitor results to eliminate double tree tra…
danceratopz d958681
feat(perf): Cache file lines in TextSource.line() to avoid repeated f…
danceratopz 76406a9
feat(perf): Cache Jinja2 environments in HTML and listing plugins.
danceratopz 35837a1
feat(perf): Share loaded renderers across HTMLVisitor instances.
danceratopz 45b27bf
Fix test pollution of shared renderer cache
danceratopz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ | |
| from abc import ABC, abstractmethod | ||
| from os.path import commonpath | ||
| from pathlib import PurePath | ||
| from typing import Dict, Final, FrozenSet, Iterator, Set, Tuple | ||
| from typing import Dict, Final, FrozenSet, Iterator, Optional, Set, Tuple | ||
|
|
||
| from jinja2 import Environment, PackageLoader, select_autoescape | ||
|
|
||
|
|
@@ -32,6 +32,20 @@ | |
| from docc.settings import PluginSettings | ||
| from docc.source import Source | ||
|
|
||
| # Module-level cache for Jinja2 environment | ||
| _LISTING_ENV: Optional[Environment] = None | ||
|
|
||
|
|
||
| def _get_listing_env() -> Environment: | ||
| """Get cached Jinja2 environment for listing templates.""" | ||
| global _LISTING_ENV | ||
| if _LISTING_ENV is None: | ||
| _LISTING_ENV = Environment( | ||
| loader=PackageLoader("docc.plugins.listing"), | ||
| autoescape=select_autoescape(), | ||
| ) | ||
| return _LISTING_ENV | ||
|
|
||
|
Comment on lines
+35
to
+48
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we keep one common cache, perhaps in the HTML plugin? |
||
|
|
||
| class Listable(ABC): | ||
| """ | ||
|
|
@@ -207,10 +221,7 @@ def render_html( | |
|
|
||
| entries.sort() | ||
|
|
||
| env = Environment( | ||
| loader=PackageLoader("docc.plugins.listing"), | ||
| autoescape=select_autoescape(), | ||
| ) | ||
| env = _get_listing_env() | ||
| template = env.get_template("listing.html") | ||
| parser = html.HTMLParser(context) | ||
| parser.feed(template.render(context=context, entries=entries)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,16 @@ | |
| import dataclasses | ||
| import typing | ||
| from dataclasses import dataclass, fields | ||
| from typing import Iterable, Literal, Optional, Sequence, Union | ||
| from typing import ( | ||
| Any, | ||
| ClassVar, | ||
| Dict, | ||
| Iterable, | ||
| Literal, | ||
| Optional, | ||
| Sequence, | ||
| Union, | ||
| ) | ||
|
|
||
| from docc.document import BlankNode, ListNode, Node, Visit, Visitor | ||
| from docc.plugins.search import Content, Searchable | ||
|
|
@@ -31,12 +40,24 @@ class PythonNode(Node): | |
| Base implementation of Node operations for Python nodes. | ||
| """ | ||
|
|
||
| # Class-level cache for dataclass fields (populated lazily) | ||
| _fields_cache: ClassVar[ | ||
| Dict[type[Any], tuple[dataclasses.Field[Any], ...]] | ||
| ] = {} | ||
|
|
||
| @classmethod | ||
| def _get_fields(cls) -> tuple[dataclasses.Field[Any], ...]: | ||
| """Get cached dataclass fields for this class.""" | ||
| if cls not in cls._fields_cache: | ||
| cls._fields_cache[cls] = tuple(fields(cls)) | ||
| return cls._fields_cache[cls] | ||
|
Comment on lines
+43
to
+53
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| @property | ||
| def children(self) -> Iterable[Node]: | ||
| """ | ||
| Child nodes belonging to this node. | ||
| """ | ||
| for field in fields(self): | ||
| for field in self._get_fields(): | ||
| value = getattr(self, field.name) | ||
|
|
||
| if field.type == Node: | ||
|
|
@@ -51,7 +72,7 @@ def replace_child(self, old: Node, new: Node) -> None: | |
| """ | ||
| Replace the old node with the given new node. | ||
| """ | ||
| for field in fields(self): | ||
| for field in self._get_fields(): | ||
| value = getattr(self, field.name) | ||
| if value == old: | ||
| assert isinstance(new, field.type) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use
lru_cacheor evencacheinstead?