Anthony Sottile <asott...@umich.edu> added the comment:

hit this today unfortunately -- I'm working with some pretty complex (and 
nested) `typing.NamedTuple` objects and the lack of caching here results in 
quite the slowdown (in my macro benchmarks it's the difference between a render 
pass taking 180ms and 25ms)

the class in question is being used as a cache key:

https://github.com/asottile/babi/blob/1be4e80eddc1bff0eb8047cc89337fdf006ad148/babi/highlight.py#L217

with the hash cached:

```
μs      event
123744  startup
27833   kEND5
2859    ^X
```

without the hash cached:

```
μs      event
122575  startup
180415  kEND5
3725    ^X
```

(my hash cache of course being slower than it could be in C)

```
@@ -214,10 +214,21 @@ class Region(NamedTuple):
     scope: Scope
 
 
+_state_hash_cache = {}
+
+
 class State(NamedTuple):
     entries: Tuple['Entry', ...]
     while_stack: Tuple[Tuple['WhileRule', int], ...]
 
+    def __hash__(self):
+        k = id(self)
+        try:
+            return _state_hash_cache[k]
+        except KeyError:
+            ret = _state_hash_cache[k] = super().__hash__()
+            return ret
+
     @classmethod
     def root(cls, entry: 'Entry') -> 'State':
         return cls((entry,), ())
```

___


I get that this is a pretty extreme case and it's unlikely to change the 
resolution of this issue, but figured I'd point it out in case anyone else is 
hitting similar issues

----------
nosy: +Anthony Sottile

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue9685>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to