Skip to content

Commit 5180fd6

Browse files
Merge pull request #350 from Blosc/FrancescAlted/cpu-info-cache
Implement a cache for cpu_info
2 parents 2cf8850 + 24dfd0b commit 5180fd6

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/blosc2/core.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88
# Avoid checking the name of type annotations at run time
99
from __future__ import annotations
1010

11+
import contextlib
1112
import copy
1213
import ctypes
1314
import ctypes.util
15+
import json
1416
import math
1517
import os
1618
import pathlib
1719
import pickle
1820
import platform
1921
import sys
2022
from dataclasses import asdict
23+
from functools import lru_cache
2124
from typing import TYPE_CHECKING
2225

2326
import cpuinfo
@@ -1149,7 +1152,7 @@ def linux_cache_size(cache_level: int, default_size: int) -> int:
11491152
return cache_size
11501153

11511154

1152-
def get_cpu_info():
1155+
def _get_cpu_info():
11531156
cpu_info = cpuinfo.get_cpu_info()
11541157
# cpuinfo does not correctly retrieve the cache sizes for Apple Silicon, so do it manually
11551158
if platform.system() == "Darwin":
@@ -1167,6 +1170,32 @@ def get_cpu_info():
11671170
return cpu_info
11681171

11691172

1173+
def write_cached_cpu_info(cpu_info_dict: dict[str, any]) -> None:
1174+
with open(pathlib.Path.home() / ".blosc2-cpuinfo.json", "w") as f:
1175+
json.dump(cpu_info_dict, f, indent=4)
1176+
1177+
1178+
def read_cached_cpu_info() -> dict:
1179+
try:
1180+
with open(pathlib.Path.home() / ".blosc2-cpuinfo.json") as f:
1181+
return json.load(f)
1182+
except (FileNotFoundError, json.JSONDecodeError):
1183+
return {}
1184+
1185+
1186+
@lru_cache(maxsize=1)
1187+
def get_cpu_info() -> dict:
1188+
cached_info = read_cached_cpu_info()
1189+
if cached_info:
1190+
return cached_info
1191+
1192+
cpu_info_dict = _get_cpu_info()
1193+
with contextlib.suppress(OSError):
1194+
# In case cpu info cannot be stored, will need to be recomputed in the next process
1195+
write_cached_cpu_info(cpu_info_dict)
1196+
return cpu_info_dict
1197+
1198+
11701199
def get_blocksize() -> int:
11711200
"""Get the internal blocksize to be used during compression.
11721201

0 commit comments

Comments
 (0)