Improve cross-reference resolution performance in the C++ domain (#11892)

Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
This commit is contained in:
Rouslan Korneychuk 2024-01-18 16:10:54 -05:00 committed by GitHub
parent 1785fc9352
commit d69d19103f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 41 additions and 5 deletions

View File

@ -24,6 +24,9 @@ Features added
* #11803: autodoc: Use an overriden ``__repr__()`` function in an enum, * #11803: autodoc: Use an overriden ``__repr__()`` function in an enum,
if defined. Patch by Shengyu Zhang. if defined. Patch by Shengyu Zhang.
* #11892: Improved performance when resolving cross references in cpp domain.
Patch by Rouslan Korneychuk.
Bugs fixed Bugs fixed
---------- ----------

View File

@ -149,8 +149,12 @@ class ASTIdentifier(ASTBaseBase):
assert len(identifier) != 0 assert len(identifier) != 0
self.identifier = identifier self.identifier = identifier
def __eq__(self, other: Any) -> bool: # ASTBaseBase already implements this method,
return type(other) is ASTIdentifier and self.identifier == other.identifier # but specialising it here improves performance
def __eq__(self, other: object) -> bool:
if type(other) is not ASTIdentifier:
return NotImplemented
return self.identifier == other.identifier
def is_anon(self) -> bool: def is_anon(self) -> bool:
return self.identifier[0] == '@' return self.identifier[0] == '@'
@ -1448,6 +1452,10 @@ class ASTDeclaration(ASTBaseBase):
# set by CObject._add_enumerator_to_parent # set by CObject._add_enumerator_to_parent
self.enumeratorScopedSymbol: Symbol | None = None self.enumeratorScopedSymbol: Symbol | None = None
# the cache assumes that by the time get_newest_id is called, no
# further changes will be made to this object
self._newest_id_cache: str | None = None
def clone(self) -> ASTDeclaration: def clone(self) -> ASTDeclaration:
return ASTDeclaration(self.objectType, self.directiveType, return ASTDeclaration(self.objectType, self.directiveType,
self.declaration.clone(), self.semicolon) self.declaration.clone(), self.semicolon)
@ -1474,7 +1482,9 @@ class ASTDeclaration(ASTBaseBase):
return id_ return id_
def get_newest_id(self) -> str: def get_newest_id(self) -> str:
return self.get_id(_max_id, True) if self._newest_id_cache is None:
self._newest_id_cache = self.get_id(_max_id, True)
return self._newest_id_cache
def _stringify(self, transform: StringifyTransform) -> str: def _stringify(self, transform: StringifyTransform) -> str:
res = transform(self.declaration) res = transform(self.declaration)

View File

@ -615,6 +615,13 @@ class ASTIdentifier(ASTBase):
assert len(identifier) != 0 assert len(identifier) != 0
self.identifier = identifier self.identifier = identifier
# ASTBaseBase already implements this method,
# but specialising it here improves performance
def __eq__(self, other: object) -> bool:
if type(other) is not ASTIdentifier:
return NotImplemented
return self.identifier == other.identifier
def _stringify(self, transform: StringifyTransform) -> str: def _stringify(self, transform: StringifyTransform) -> str:
return transform(self.identifier) return transform(self.identifier)
@ -4018,6 +4025,10 @@ class ASTDeclaration(ASTBase):
# set by CPPObject._add_enumerator_to_parent # set by CPPObject._add_enumerator_to_parent
self.enumeratorScopedSymbol: Symbol | None = None self.enumeratorScopedSymbol: Symbol | None = None
# the cache assumes that by the time get_newest_id is called, no
# further changes will be made to this object
self._newest_id_cache: str | None = None
def clone(self) -> ASTDeclaration: def clone(self) -> ASTDeclaration:
templatePrefixClone = self.templatePrefix.clone() if self.templatePrefix else None templatePrefixClone = self.templatePrefix.clone() if self.templatePrefix else None
trailingRequiresClasueClone = self.trailingRequiresClause.clone() \ trailingRequiresClasueClone = self.trailingRequiresClause.clone() \
@ -4083,7 +4094,9 @@ class ASTDeclaration(ASTBase):
return ''.join(res) return ''.join(res)
def get_newest_id(self) -> str: def get_newest_id(self) -> str:
return self.get_id(_max_id, True) if self._newest_id_cache is None:
self._newest_id_cache = self.get_id(_max_id, True)
return self._newest_id_cache
def _stringify(self, transform: StringifyTransform) -> str: def _stringify(self, transform: StringifyTransform) -> str:
res = [] res = []

View File

@ -88,7 +88,7 @@ class NoOldIdError(Exception):
class ASTBaseBase: class ASTBaseBase:
def __eq__(self, other: Any) -> bool: def __eq__(self, other: object) -> bool:
if type(self) is not type(other): if type(self) is not type(other):
return False return False
try: try:
@ -145,6 +145,11 @@ class ASTGnuAttribute(ASTBaseBase):
self.name = name self.name = name
self.args = args self.args = args
def __eq__(self, other: object) -> bool:
if type(other) is not ASTGnuAttribute:
return NotImplemented
return self.name == other.name and self.args == other.args
def _stringify(self, transform: StringifyTransform) -> str: def _stringify(self, transform: StringifyTransform) -> str:
res = [self.name] res = [self.name]
if self.args: if self.args:
@ -204,6 +209,11 @@ class ASTAttributeList(ASTBaseBase):
def __init__(self, attrs: list[ASTAttribute]) -> None: def __init__(self, attrs: list[ASTAttribute]) -> None:
self.attrs = attrs self.attrs = attrs
def __eq__(self, other: object) -> bool:
if type(other) is not ASTAttributeList:
return NotImplemented
return self.attrs == other.attrs
def __len__(self) -> int: def __len__(self) -> int:
return len(self.attrs) return len(self.attrs)