# Understanding Functiong Caching

**URL:** <https://numba.discourse.group/t/understanding-functiong-caching/2852>\
**Category:** Support: How do I do ...?\
**Created:** [December 27, 2024, 12:42pm UTC](https://numba.discourse.group/t/understanding-functiong-caching/2852 "2024-12-27T12:42:53Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seanlaw](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/seanlaw/32/506_2.png) [@seanlaw](https://numba.discourse.group/u/seanlaw)\
**Post date:** [December 27, 2024, 12:42pm UTC](https://numba.discourse.group/t/understanding-functiong-caching/2852/1 "2024-12-27T12:42:53Z")

</div>

I have a simple `numba` example where I

1. Define a numba function
2. Enable caching via `enable_caching()` (instead of `cache=True`)
3. Call the function
4. Print the contents of the cache directory

```auto
rom numba import njit
from os import listdir
import pathlib

PYCACHEDIR = ' __pycache__'

@njit
def add(a, b):
    return a + b

if __name__ == " __main__":
    add.enable_caching()
    add(4, 5)
    print(listdir(PYCACHEDIR))

```

As expected, this prints out:

```auto
['test.add-9.py312.nbi', 'test.add-9.py312.1.nbc']

```

However, if I:

1. Manually clear/delete the cache
2. Call the `add` function again
3. Print the contents of the cache directory

```auto
    [f.unlink() for f in pathlib.Path(PYCACHEDIR).glob("*nb*") if f.is_file()]
    print(listdir(PYCACHEDIR))
    add(4, 5)
    print(listdir(PYCACHEDIR))

```

Then nothing gets printed as the cache directory is empty! Since I had originally called `add.enable_cache()`, I would’ve expected the function to be cached again (after having manually cleared/deleted the cache) once I call the function again. Instead, it almost appears as if the writing to the `PYCACHEDIR` is disabled and, perhaps, the `add` function is either:

1. Not cached at all (which would be undesirable)
2. or cached elsewhere (but where?!)

Can anybody tell me what is happening here?

* * *

The full script is as follows:

```auto
from numba import njit
from os import listdir
import pathlib

PYCACHEDIR = ' __pycache__'

@njit
def add(a, b):
    return a + b

if __name__ == " __main__":
    add.enable_caching()
    add(4, 5)
    print(listdir(PYCACHEDIR))
    [f.unlink() for f in pathlib.Path(PYCACHEDIR).glob("*nb*") if f.is_file()]
    print(listdir(PYCACHEDIR))
    add(4, 5)
    print(listdir(PYCACHEDIR))

```

---

<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:** [December 30, 2024, 5:33am UTC](https://numba.discourse.group/t/understanding-functiong-caching/2852/2 "2024-12-30T05:33:43Z")

</div>

Perhaps I’m not thinking about this correctly, but I’d expect a disk cache save/load only the first time a signature is compiled. It doesn’t make sense for numba to continually check if some outside process is changing the cache files.

After the first usage, the function is memory-cached and won’t be reloaded from disk. Setting the NUMBA\_DEBUG\_CACHE environment variable prints out some useful information, and setting a breakpoint inside numba/core/caching.py might be interesting for finding the location of the logic that shortcuts already-saved file caching.

Apologies for the lack of links, I’m responding using a cell phone which is a struggle.

EDIT: I ran a quick test, changed '4’ to ‘4.0’ in the second call to add() and got new cache files as expected. That lines up with my comments above.

---

<div class="post-metadata">

**Author:** ![guilherme](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/guilherme/32/111_2.png) [@guilherme](https://numba.discourse.group/u/guilherme)\
**Post date:** [January 2, 2025, 1:45pm UTC](https://numba.discourse.group/t/understanding-functiong-caching/2852/3 "2025-01-02T13:45:05Z")

</div>

I think @nelson2005 answer is correct. After you have a dispatcher object, it will only redirect the call to the generated function. Hence, caching won’t be regenerated.

---

<div class="post-metadata">

**Author:** ![seanlaw](https://yyz2.discourse-cdn.com/free1/user_avatar/numba.discourse.group/seanlaw/32/506_2.png) [@seanlaw](https://numba.discourse.group/u/seanlaw)\
**Post date:** [January 3, 2025, 4:07am UTC](https://numba.discourse.group/t/understanding-functiong-caching/2852/4 "2025-01-03T04:07:50Z")

</div>

Ahh, gotcha. It looks like explicitly calling the `.recompile()` method will cause the deleted files to be regenerated:

```auto
from numba import njit
from os import listdir
import pathlib

PYCACHEDIR = ' __pycache__'

@njit
def add(a, b):
    return a + b

if __name__ == " __main__":
    add.enable_caching()
    add(4, 5)
    print(listdir(PYCACHEDIR))
    [f.unlink() for f in pathlib.Path(PYCACHEDIR).glob("*nb*") if f.is_file()]
    print(listdir(PYCACHEDIR))
    add.recompile()
    add(4, 5)
    print(listdir(PYCACHEDIR))

```
