ericvsmith / dataclasses

Apache License 2.0
584 stars 53 forks source link

Cache the value of the `__hahs__` #156

Closed hackaugusto closed 4 years ago

hackaugusto commented 4 years ago

I was modeling a toy blockchain using dataclasses and I had something roughly like this:

from dataclasses import dataclass

@dataclass(eq=True, frozen=True)
class Transaction:
  pass

@dataclass(eq=True, frozen=True)
class Block:
  parent: Optional[Block]
  transactions: List[Transaction]

The problem is that the cost of hash(block) increases linearly with the length of the parent chain. What do you think about caching the result of the hash function? It will not fix the problem in general (this is a toy example anyways), but it will alleviate the problem for a toy script like the one I was writing.

Let me know if I should open another issue in the python issue tracker, and if the PR makes sense.Thanks!

ericvsmith commented 4 years ago

Please open an issue on the Python bug tracker, or bring it up on the python-ideas mailing list. My first impression is that this is too specialized to be added to dataclasses. I'd suggest writing your own __hash__ function.

I'm going to close this, since this tracker is just for backport issues, not new features.