# Reset chunk cache

**URL:** <https://forum.hdfgroup.org/t/reset-chunk-cache/12424>\
**Category:** HDF5 Library\
**Created:** [June 17, 2024, 9:34pm UTC](https://forum.hdfgroup.org/t/reset-chunk-cache/12424 "2024-06-17T21:34:32Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![apollo3zehn-h5](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/apollo3zehn-h5/32/1589_2.png) [@apollo3zehn-h5](https://forum.hdfgroup.org/u/apollo3zehn-h5)\
**Post date:** [June 17, 2024, 9:34pm UTC](https://forum.hdfgroup.org/t/reset-chunk-cache/12424/1 "2024-06-17T21:34:32Z")

</div>

Hi, I am working on a benchmark repository to compare the performance of independent HDF5 reading libraries to the performance of the native C library ([repository](https://github.com/Apollo3zehn/hdf5-benchmark), [web page](https://apollo3zehn.github.io/hdf5-benchmark/)).

I noticed the very high (too high) performance of the C library and suspect it to be related to the chunk cache which disturbs the benchmark which iterates very often to read the same dataset.

I want the chunk cache to be active during a single read operation since it is a performance relevant feature but I want it to be reset before the next loop iteration begins. So I want to simulate reading many different datasets although I have created only one to keep the file size small.

Is it enough to simply close the dataset and reopen it like this (the code below is for the thin C# wrapper `HDF.PInvoke.1.10`)?

```auto
_datasetId = H5D.open(_fileId, "chunked_btree2", _daplId);

var result = H5D.read(_datasetId, H5T.NATIVE_INT32, H5S.ALL, H5S.ALL, H5P.DEFAULT, _buffer);

if (H5I.is_valid(_datasetId) > 0)
    _ = H5D.close(_datasetId);

```

Or does this method not help to reset the chunk cache?

Thank you

---

<div class="post-metadata">

**Author:** ![nfortne2](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/nfortne2/32/1543_2.png) [@nfortne2](https://forum.hdfgroup.org/u/nfortne2)\
**Post date:** [June 18, 2024, 11:54pm UTC](https://forum.hdfgroup.org/t/reset-chunk-cache/12424/2 "2024-06-18T23:54:26Z")

</div>

Yes, closing the dataset ID (as long as it’s the last reference to the ID) will empty and close the associated chunk cache. I’m not sure if you’re considering disabling the cache for the benchmark, but if you are keep in mind that that might affect the performance, even with an initially empty cache. The size of the cache can also affect performance.

It’s also worth mentioning that cached pieces of metadata will not be evicted from the file’s metadata cache, so chunk index look ups will likely be faster on operations after the first, even if the dataset is closed between operations. To empty the metadata cache you’ll need to close the file.

---

<div class="post-metadata">

**Author:** ![dave.allured](https://forum.hdfgroup.org/letter_avatar_proxy/v4/letter/d/8e8cbc/32.png) [@dave.allured](https://forum.hdfgroup.org/u/dave.allured)\
**Post date:** [June 19, 2024, 2:23pm UTC](https://forum.hdfgroup.org/t/reset-chunk-cache/12424/3 "2024-06-19T14:23:29Z")

</div>

> [@apollo3zehn-h5](#):
>
> So I want to simulate reading many different datasets although I have created only one to keep the file size small.

I suggest don’t do this. Instead, dynamically generate a realistic large test file with many data sets and random data. Then delete it when the benchmark is finished. This way, you avoid cacheing effects and other uncertain internal library behavior.

You will still be subject to external filesystem cacheing, but at least that effect would be equal across test scenarios, if you are doing comparisons.

---

<div class="post-metadata">

**Author:** ![apollo3zehn-h5](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/apollo3zehn-h5/32/1589_2.png) [@apollo3zehn-h5](https://forum.hdfgroup.org/u/apollo3zehn-h5)\
**Post date:** [June 22, 2024, 8:21am UTC](https://forum.hdfgroup.org/t/reset-chunk-cache/12424/4 "2024-06-22T08:21:14Z")

</div>

Thanks! I do not want to disable the cache, only start withban empty one for each iterstion. I found a way to do this with my benchmark suite which allows me to do setup and cleaup work before and after each iteration. So for now I open and close the file for each iteration.

---

<div class="post-metadata">

**Author:** ![apollo3zehn-h5](https://forum.hdfgroup.org/user_avatar/forum.hdfgroup.org/apollo3zehn-h5/32/1589_2.png) [@apollo3zehn-h5](https://forum.hdfgroup.org/u/apollo3zehn-h5)\
**Post date:** [June 22, 2024, 8:53am UTC](https://forum.hdfgroup.org/t/reset-chunk-cache/12424/5 "2024-06-22T08:53:05Z")

</div>

Thanks to you as well! For now it works find for me if I open the file before the iteration and close it afterwards (as written above). The prerequisite for this is that the CPU running the benchmark has an `invariant TSC` clock available which is the case for the machines I use to run the benchmark. Without that precise timer it would be problematic to do setup and cleanup work before and after each iteration because of unreliable time measurement.

Yes, caching is affecting all benchmarks but I mainly want to benchmark the efficient handling of internal H5 structures + compression algorithms and not the read operation itself so I think the current setup is OK for my purposes.
