[edit]
I should have specified I’m using python 3.7.1 on windows with numba 0.50.1
[/edit]
I’m looking at a variant of @stuartarchibald 's example here
In the example, the bar() function constructs the Car jitclass and is cacheable. However, if I create the Car jitclass outside the function, like
@njit(cache=True)
def bar1(car):
return car.twice
car = Car(1.)
bar1(car)
bar1() is never loaded from cache, although a new .nbc cache entry is created with each invocation, implying that the types are somehow different each time. Perhaps it’s related to this ?
Is there any workaround? I’ve got a couple of hundred files that take maybe half and hour to compile on each program invocation and I think being able to load them from cache would improve the runtime substantially.
I read the caching notes and didn’t notice anything in particular that I was violating.
Full example below (I’d also welcome tips on better ways to measure loading from cache/compilation timing)
from numba import jitclass, njit, float64
def compile_time(func) -> float:
res = 0.0
meta = func.overloads[func.signatures[0]].metadata
if meta is None: # loaded from cache
return 'loaded from cache'
for key1, val1 in func.overloads[func.signatures[0]].metadata['pipeline_times']['nopython'].items():
res += val1.init + val1.run + val1.finalize
return res
@jitclass([('value', float64)])
class Car(object):
def __init__(self, value):
self.value = value
@property
def twice(self):
return self.value * 2
@njit(cache=True)
def bar(y):
return Car(y).twice # car is created in function, this loads from cache on repeated runs
bar(1.)
print("bar compile time", compile_time(bar))
@njit(cache=True)
def bar1(car):
return car.twice
car = Car(1.)
bar1(car) # car is created outside function, this recompiles on every run
print("bar1 compile time", compile_time(bar1))