[Libre-soc-bugs] [Bug 1094] insndb instruction database visitor-walker is needed
bugzilla-daemon at libre-soc.org
bugzilla-daemon at libre-soc.org
Sun Jun 18 19:26:27 BST 2023
https://bugs.libre-soc.org/show_bug.cgi?id=1094
--- Comment #158 from Jacob Lifshay <programmerjake at gmail.com> ---
(In reply to Dmitry Selyutin from comment #156)
> (In reply to Dmitry Selyutin from comment #155)
> > I think we can leave the short form, but internally we can implement
> > everything as callables. Holy cow that's really powerful!
>
>
> Nah, implementing everything as callables loses one benefit: we can have
> hash lookup with types, with callables we're limited to linear search. It's
> not that it's critical (who's crazy to establish thousands of hooks?), but
> it hurts the beauty of the code. I'm currently checking something more
> graceful.
if you're worried about speed, you could cache lookups -- something like:
class Visitor:
def __init__(self):
self.__cache = {}
def run_hook(self, v):
t = v if isinstance(v, type) else type(v)
f = self.__cache.get(t)
if f is None:
for name, check in self.hooks():
if check(t):
self.__cache[t] = f = getattr(self, name)
break
if f is None:
raise TypeError("no hooks for type", t)
return f(v)
--
You are receiving this mail because:
You are on the CC list for the bug.
More information about the libre-soc-bugs
mailing list