Skip to content

Commit 9013c6f

Browse files
committed
Speed up the type dispatching
1 parent 0a63cf4 commit 9013c6f

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

clifford/numba/_multivector.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,23 @@ def layout_type(self):
4444
return LayoutType()
4545

4646

47-
@numba.extending.typeof_impl.register(MultiVector)
48-
def _typeof_MultiVector(val: MultiVector, c) -> MultiVectorType:
49-
val._numba_type_ = MultiVectorType(dtype=val.value.dtype)
50-
return val._numba_type_
47+
# The docs say we should use register a function to determine the numba type
48+
# with `@numba.extending.typeof_impl.register(MultiVector)`, but this is way
49+
# too slow (https://github.com/numba/numba/issues/5839). Instead, we use the
50+
# undocumented `_numba_type_` attribute, and use our own cache. In future
51+
# this may need to be a weak cache, but for now the objects are tiny anyway.
52+
_cache = {}
53+
54+
@property
55+
def _numba_type_(self):
56+
dt = self.value.dtype
57+
try:
58+
return _cache[dt]
59+
except KeyError:
60+
ret = _cache[dt] = MultiVectorType(dtype=dt)
61+
return ret
62+
63+
MultiVector._numba_type_ = _numba_type_
5164

5265

5366
@numba.extending.register_model(MultiVectorType)

0 commit comments

Comments
 (0)