# Cache jitted function with jitclass argument

**URL:** <https://numba.discourse.group/t/cache-jitted-function-with-jitclass-argument/780>\
**Category:** Support: How do I do ...?\
**Created:** [June 30, 2021, 8:57pm UTC](https://numba.discourse.group/t/cache-jitted-function-with-jitclass-argument/780 "2021-06-30T20:57:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [June 30, 2021, 8:57pm UTC](https://numba.discourse.group/t/cache-jitted-function-with-jitclass-argument/780/1 "2021-06-30T20:57:23Z")

</div>

[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](https://github.com/numba/numba/issues/1875#issuecomment-477281710)

In the example, the bar() function constructs the Car jitclass and is cacheable. However, if I create the Car jitclass _outside_ the function, like

```python
@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](https://github.com/numba/numba/issues/4830) ?  
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](https://numba.pydata.org/numba-doc/latest/developer/caching.html) 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)

```python
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))

```

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [July 6, 2021, 9:39am UTC](https://numba.discourse.group/t/cache-jitted-function-with-jitclass-argument/780/2 "2021-07-06T09:39:32Z")

</div>

Hi @nelson2005

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

```auto
(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](https://github.com/numba/numba/issues/6522), unfortunately, it’s a bug.

---

<div class="post-metadata">

**Author:** ![nelson2005](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/nelson2005/32/47_2.png) [@nelson2005](https://numba.discourse.group/u/nelson2005)\
**Post date:** [July 6, 2021, 2:49pm UTC](https://numba.discourse.group/t/cache-jitted-function-with-jitclass-argument/780/3 "2021-07-06T14:49:15Z")

</div>

Thanks, sorry for the duplicate ☹

---

<div class="post-metadata">

**Author:** ![stuartarchibald](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/stuartarchibald/32/10_2.png) [@stuartarchibald](https://numba.discourse.group/u/stuartarchibald)\
**Post date:** [July 7, 2021, 9:40am UTC](https://numba.discourse.group/t/cache-jitted-function-with-jitclass-argument/780/4 "2021-07-07T09:40:32Z")

</div>

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](https://github.com/numba/numba/issues?q=is%3Aopen+is%3Aissue+label%3Acaching+label%3Ajitclass), for issues relating to specific features I’ve found it’s quite a good way to narrow things down.
