This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Anthony Sottile
Recipients Anthony Sottile, benjamin.peterson, christian.heimes, dtorp, georg.brandl, mark.dickinson, meador.inge, python-dev, rhettinger, serhiy.storchaka, tim.peters
Date 2020-03-15.20:46:19
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1584305180.37.0.151176456507.issue9685@roundup.psfhosted.org>
In-reply-to
Content
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
History
Date User Action Args
2020-03-15 20:46:20Anthony Sottilesetrecipients: + Anthony Sottile, tim.peters, georg.brandl, rhettinger, mark.dickinson, dtorp, christian.heimes, benjamin.peterson, meador.inge, python-dev, serhiy.storchaka
2020-03-15 20:46:20Anthony Sottilesetmessageid: <1584305180.37.0.151176456507.issue9685@roundup.psfhosted.org>
2020-03-15 20:46:20Anthony Sottilelinkissue9685 messages
2020-03-15 20:46:19Anthony Sottilecreate