Skip to content

Commit

Permalink
perf: Stop caching properties on Object methods
Browse files Browse the repository at this point in the history
Suprisingly benchmarks show faster execution
and reduction of memory usage without caching.
  • Loading branch information
pawamoy committed Jul 7, 2023
1 parent 96fd45b commit 15bdd74
Showing 1 changed file with 21 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/griffe/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def __init__(
def __bool__(self) -> bool:
return bool(self.value)

@cached_property
@property
def lines(self) -> list[str]:
"""Returns the lines of the docstring.
Expand Down Expand Up @@ -550,7 +550,7 @@ def attributes(self) -> dict[str, Attribute]:
"""
return {name: member for name, member in self.all_members.items() if member.kind is Kind.ATTRIBUTE} # type: ignore[misc]

@cached_property
@property
def module(self) -> Module:
"""Return the parent module of this object.
Expand All @@ -566,7 +566,7 @@ def module(self) -> Module:
return self.parent.module
raise ValueError(f"Object {self.name} does not have a parent module")

@cached_property
@property
def package(self) -> Module:
"""Return the absolute top module (the package) of this object.
Expand All @@ -578,7 +578,7 @@ def package(self) -> Module:
module = module.parent # type: ignore[assignment] # always a module
return module

@cached_property
@property
def filepath(self) -> Path | list[Path]:
"""Return the file path where this object was defined.
Expand All @@ -587,7 +587,7 @@ def filepath(self) -> Path | list[Path]:
"""
return self.module.filepath

@cached_property
@property
def relative_package_filepath(self) -> Path:
"""Return the file path where this object was defined, relative to the top module path.
Expand Down Expand Up @@ -616,7 +616,7 @@ def relative_package_filepath(self) -> Path:
raise ValueError
return self.filepath.relative_to(package_path.parent.parent)

@cached_property
@property
def relative_filepath(self) -> Path:
"""Return the file path where this object was defined, relative to the current working directory.
Expand All @@ -639,7 +639,7 @@ def relative_filepath(self) -> Path:
except ValueError:
return self.filepath

@cached_property
@property
def path(self) -> str:
"""Return the dotted path of this object.
Expand All @@ -650,7 +650,7 @@ def path(self) -> str:
"""
return self.canonical_path

@cached_property
@property
def canonical_path(self) -> str:
"""Return the full dotted path of this object.
Expand All @@ -663,7 +663,7 @@ def canonical_path(self) -> str:
return self.name
return ".".join((self.parent.path, self.name))

@cached_property
@property
def modules_collection(self) -> ModulesCollection:
"""Return the modules collection attached to this object or its parents.
Expand All @@ -679,7 +679,7 @@ def modules_collection(self) -> ModulesCollection:
raise ValueError("no modules collection in this object or its parents")
return self.parent.modules_collection

@cached_property
@property
def lines_collection(self) -> LinesCollection:
"""Return the lines collection attached to this object or its parents.
Expand All @@ -695,7 +695,7 @@ def lines_collection(self) -> LinesCollection:
raise ValueError("no lines collection in this object or its parents")
return self.parent.lines_collection

@cached_property
@property
def lines(self) -> list[str]:
"""Return the lines containing the source of this object.
Expand All @@ -717,7 +717,7 @@ def lines(self) -> list[str]:
return lines
return lines[self.lineno - 1 : self.endlineno]

@cached_property
@property
def source(self) -> str:
"""Return the source code of this object.
Expand Down Expand Up @@ -934,7 +934,7 @@ def parent(self, value: Module | Class) -> None:
self._parent = value
self._update_target_aliases()

@cached_property
@property
def path(self) -> str:
"""Return the dotted path / import path of this object.
Expand All @@ -943,7 +943,7 @@ def path(self) -> str:
"""
return ".".join((self.parent.path, self.name)) # type: ignore[union-attr] # we assume there's always a parent

@cached_property
@property
def modules_collection(self) -> ModulesCollection:
"""Return the modules collection attached to the alias parents.
Expand Down Expand Up @@ -1232,7 +1232,7 @@ def resolved(self) -> bool:
"""
return self._target is not None

@cached_property
@property
def wildcard(self) -> str | None:
"""Return the module on which the wildcard import is performed (if any).
Expand Down Expand Up @@ -1307,7 +1307,7 @@ def filepath(self) -> Path | list[Path]:
raise BuiltinModuleError(self.name)
return self._filepath

@cached_property
@property
def imports_future_annotations(self) -> bool:
"""Tell whether this module import future annotations.
Expand All @@ -1320,7 +1320,7 @@ def imports_future_annotations(self) -> bool:
and self.members["annotations"].target_path == "__future__.annotations" # type: ignore[union-attr]
)

@cached_property
@property
def is_init_module(self) -> bool:
"""Tell if this module is an `__init__.py` module.
Expand All @@ -1334,7 +1334,7 @@ def is_init_module(self) -> bool:
except BuiltinModuleError:
return False

@cached_property
@property
def is_package(self) -> bool:
"""Tell if this module is a package (top module).
Expand All @@ -1343,7 +1343,7 @@ def is_package(self) -> bool:
"""
return not bool(self.parent) and self.is_init_module

@cached_property
@property
def is_subpackage(self) -> bool:
"""Tell if this module is a subpackage.
Expand All @@ -1352,7 +1352,7 @@ def is_subpackage(self) -> bool:
"""
return bool(self.parent) and self.is_init_module

@cached_property
@property
def is_namespace_package(self) -> bool:
"""Tell if this module is a namespace package (top folder, no `__init__.py`).
Expand All @@ -1364,7 +1364,7 @@ def is_namespace_package(self) -> bool:
except BuiltinModuleError:
return False

@cached_property
@property
def is_namespace_subpackage(self) -> bool:
"""Tell if this module is a namespace subpackage.
Expand Down

0 comments on commit 15bdd74

Please sign in to comment.