From f6ef03fe3cd8c98f819a6f979509bbd4991b0c4e Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sat, 17 May 2025 16:58:00 -0400 Subject: [PATCH 1/3] feat: cache result of ABIType.to_type_str --- eth_abi/grammar.py | 60 ++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/eth_abi/grammar.py b/eth_abi/grammar.py index 09e34f39ee..4a3dbcc544 100644 --- a/eth_abi/grammar.py +++ b/eth_abi/grammar.py @@ -224,7 +224,7 @@ class TupleType(ABIType): Represents the result of parsing a tuple type string e.g. "(int,bool)". """ - __slots__ = ("components",) + __slots__ = ("components", "_type_str") def __init__(self, components, arrlist=None, *, node=None): super().__init__(arrlist, node) @@ -235,15 +235,20 @@ def __init__(self, components, arrlist=None, *, node=None): tuple type's components. """ - def to_type_str(self): - arrlist = self.arrlist - - if isinstance(arrlist, tuple): - arrlist = "".join(repr(list(a)) for a in arrlist) - else: - arrlist = "" - - return f"({','.join(c.to_type_str() for c in self.components)}){arrlist}" + def to_type_str(self) -> str: + type_str = self._type_str + if type_str is None: + arrlist = self.arrlist + + if isinstance(arrlist, tuple): + arrlist = "".join(repr(list(a)) for a in arrlist) + else: + arrlist = "" + + type_str = self._type_str = ( + f"({','.join(c.to_type_str() for c in self.components)}){arrlist}" + ) + return type_str @property def item_type(self): @@ -276,7 +281,7 @@ class BasicType(ABIType): "ufixed128x19[][2]". """ - __slots__ = ("base", "sub") + __slots__ = ("base", "sub", "_type_str") def __init__(self, base, sub=None, arrlist=None, *, node=None): super().__init__(arrlist, node) @@ -292,21 +297,24 @@ def __init__(self, base, sub=None, arrlist=None, *, node=None): """ def to_type_str(self): - sub, arrlist = self.sub, self.arrlist - - if isinstance(sub, int): - sub = str(sub) - elif isinstance(sub, tuple): - sub = "x".join(str(s) for s in sub) - else: - sub = "" - - if isinstance(arrlist, tuple): - arrlist = "".join(repr(list(a)) for a in arrlist) - else: - arrlist = "" - - return self.base + sub + arrlist + type_str = self._type_str + if type_str is None: + sub, arrlist = self.sub, self.arrlist + + if isinstance(sub, int): + sub = str(sub) + elif isinstance(sub, tuple): + sub = "x".join(str(s) for s in sub) + else: + sub = "" + + if isinstance(arrlist, tuple): + arrlist = "".join(repr(list(a)) for a in arrlist) + else: + arrlist = "" + + type_str = self._type_str = self.base + sub + arrlist + return type_str @property def item_type(self): From 3d3dc9dc9afc6efb731572b40482ca47a8bac800 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sat, 17 May 2025 17:04:52 -0400 Subject: [PATCH 2/3] Update grammar.py --- eth_abi/grammar.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/eth_abi/grammar.py b/eth_abi/grammar.py index 4a3dbcc544..7cc08a76e4 100644 --- a/eth_abi/grammar.py +++ b/eth_abi/grammar.py @@ -1,5 +1,6 @@ import functools import re +from typing import Optional import parsimonious from parsimonious import ( @@ -235,6 +236,12 @@ def __init__(self, components, arrlist=None, *, node=None): tuple type's components. """ + self._type_str: Optional[str] = None + """ + The string representation of an ABI type. Once populated, + this will be equal to the type string from which it was created. + """ # NOTE: wait then why dont we just set it now from the init arg? + def to_type_str(self) -> str: type_str = self._type_str if type_str is None: @@ -296,6 +303,12 @@ def __init__(self, base, sub=None, arrlist=None, *, node=None): type. """ + self._type_str: Optional[str] = None + """ + The string representation of an ABI type. Once populated, + this will be equal to the type string from which it was created. + """ # NOTE: wait then why dont we just set it now from the init arg? + def to_type_str(self): type_str = self._type_str if type_str is None: From a6daeadecfa534f606194f27bf3b1deb89c20971 Mon Sep 17 00:00:00 2001 From: BobTheBuidler <70677534+BobTheBuidler@users.noreply.github.com> Date: Sat, 17 May 2025 17:05:22 -0400 Subject: [PATCH 3/3] Update grammar.py --- eth_abi/grammar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/eth_abi/grammar.py b/eth_abi/grammar.py index 7cc08a76e4..5d5c94e1fc 100644 --- a/eth_abi/grammar.py +++ b/eth_abi/grammar.py @@ -240,7 +240,7 @@ def __init__(self, components, arrlist=None, *, node=None): """ The string representation of an ABI type. Once populated, this will be equal to the type string from which it was created. - """ # NOTE: wait then why dont we just set it now from the init arg? + """ def to_type_str(self) -> str: type_str = self._type_str @@ -307,7 +307,7 @@ def __init__(self, base, sub=None, arrlist=None, *, node=None): """ The string representation of an ABI type. Once populated, this will be equal to the type string from which it was created. - """ # NOTE: wait then why dont we just set it now from the init arg? + """ def to_type_str(self): type_str = self._type_str