Skip to content

Commit 15bdd74

Browse files
committed
perf: Stop caching properties on Object methods
Suprisingly benchmarks show faster execution and reduction of memory usage without caching.
1 parent 96fd45b commit 15bdd74

1 file changed

Lines changed: 21 additions & 21 deletions

File tree

src/griffe/dataclasses.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __init__(
125125
def __bool__(self) -> bool:
126126
return bool(self.value)
127127

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

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

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

581-
@cached_property
581+
@property
582582
def filepath(self) -> Path | list[Path]:
583583
"""Return the file path where this object was defined.
584584
@@ -587,7 +587,7 @@ def filepath(self) -> Path | list[Path]:
587587
"""
588588
return self.module.filepath
589589

590-
@cached_property
590+
@property
591591
def relative_package_filepath(self) -> Path:
592592
"""Return the file path where this object was defined, relative to the top module path.
593593
@@ -616,7 +616,7 @@ def relative_package_filepath(self) -> Path:
616616
raise ValueError
617617
return self.filepath.relative_to(package_path.parent.parent)
618618

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

642-
@cached_property
642+
@property
643643
def path(self) -> str:
644644
"""Return the dotted path of this object.
645645
@@ -650,7 +650,7 @@ def path(self) -> str:
650650
"""
651651
return self.canonical_path
652652

653-
@cached_property
653+
@property
654654
def canonical_path(self) -> str:
655655
"""Return the full dotted path of this object.
656656
@@ -663,7 +663,7 @@ def canonical_path(self) -> str:
663663
return self.name
664664
return ".".join((self.parent.path, self.name))
665665

666-
@cached_property
666+
@property
667667
def modules_collection(self) -> ModulesCollection:
668668
"""Return the modules collection attached to this object or its parents.
669669
@@ -679,7 +679,7 @@ def modules_collection(self) -> ModulesCollection:
679679
raise ValueError("no modules collection in this object or its parents")
680680
return self.parent.modules_collection
681681

682-
@cached_property
682+
@property
683683
def lines_collection(self) -> LinesCollection:
684684
"""Return the lines collection attached to this object or its parents.
685685
@@ -695,7 +695,7 @@ def lines_collection(self) -> LinesCollection:
695695
raise ValueError("no lines collection in this object or its parents")
696696
return self.parent.lines_collection
697697

698-
@cached_property
698+
@property
699699
def lines(self) -> list[str]:
700700
"""Return the lines containing the source of this object.
701701
@@ -717,7 +717,7 @@ def lines(self) -> list[str]:
717717
return lines
718718
return lines[self.lineno - 1 : self.endlineno]
719719

720-
@cached_property
720+
@property
721721
def source(self) -> str:
722722
"""Return the source code of this object.
723723
@@ -934,7 +934,7 @@ def parent(self, value: Module | Class) -> None:
934934
self._parent = value
935935
self._update_target_aliases()
936936

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

946-
@cached_property
946+
@property
947947
def modules_collection(self) -> ModulesCollection:
948948
"""Return the modules collection attached to the alias parents.
949949
@@ -1232,7 +1232,7 @@ def resolved(self) -> bool:
12321232
"""
12331233
return self._target is not None
12341234

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

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

1323-
@cached_property
1323+
@property
13241324
def is_init_module(self) -> bool:
13251325
"""Tell if this module is an `__init__.py` module.
13261326
@@ -1334,7 +1334,7 @@ def is_init_module(self) -> bool:
13341334
except BuiltinModuleError:
13351335
return False
13361336

1337-
@cached_property
1337+
@property
13381338
def is_package(self) -> bool:
13391339
"""Tell if this module is a package (top module).
13401340
@@ -1343,7 +1343,7 @@ def is_package(self) -> bool:
13431343
"""
13441344
return not bool(self.parent) and self.is_init_module
13451345

1346-
@cached_property
1346+
@property
13471347
def is_subpackage(self) -> bool:
13481348
"""Tell if this module is a subpackage.
13491349
@@ -1352,7 +1352,7 @@ def is_subpackage(self) -> bool:
13521352
"""
13531353
return bool(self.parent) and self.is_init_module
13541354

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

1367-
@cached_property
1367+
@property
13681368
def is_namespace_subpackage(self) -> bool:
13691369
"""Tell if this module is a namespace subpackage.
13701370

0 commit comments

Comments
 (0)