Cache jitted function with jitclass argument

[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))

Hi @nelson2005

I think this is because bar1's signature is based on a jitclass instance:

(Pdb) p bar1.signatures
[(instance.jitclass.Car#7f636452dac0<value:float64>,)]

and the hash (for cache purposes) is based on the key name, which changes each run as the instance changes.

There’s an open issue about this: Caching a function that takes in a jitclass instance · Issue #6522 · numba/numba · GitHub, unfortunately, it’s a bug.

Thanks, sorry for the duplicate :frowning:

No problem and no worries about duplicates, it’s good to ask. I found the issue by using the label filters on the issue tracker, I filtered with “caching” and “jitclass”: Issues · numba/numba · GitHub, for issues relating to specific features I’ve found it’s quite a good way to narrow things down.