Misc rewrite perf improvements (#6500)

* Make UOp a normal class and use __slots__

* Use __slots__ in UPat

* Cache dtypes.{min,max}

* Use faster iterables in ops.py

* extend is a lot faster than nested listcomp

Co-authored-by: Roelof van Dijk <3604013+roelofvandijk@users.noreply.github.com>

---------

Co-authored-by: Roelof van Dijk <3604013+roelofvandijk@users.noreply.github.com>
This commit is contained in:
Tim Becker
2024-09-12 20:31:50 -07:00
committed by GitHub
parent 8c4cab8d6e
commit 7c078191ce
3 changed files with 25 additions and 21 deletions

View File

@@ -62,10 +62,12 @@ class dtypes:
return tuple(dtypes.as_const(x, dtype) for x in val)
return int(val) if dtypes.is_int(dtype) else float(val) if dtypes.is_float(dtype) else bool(val)
@staticmethod
@functools.lru_cache(None)
def min(dtype:DType):
if dtypes.is_int(dtype): return 0 if dtypes.is_unsigned(dtype) else -2**(dtype.itemsize*8-1)
return -float("inf") if dtypes.is_float(dtype) else False
@staticmethod
@functools.lru_cache(None)
def max(dtype:DType):
if dtypes.is_int(dtype): return (2**(dtype.itemsize*8-(0 if dtypes.is_unsigned(dtype) else 1)))-1
return float("inf") if dtypes.is_float(dtype) else True