Skip to content

Commit 36012fd

Browse files
rparolinclaude
andcommitted
refactor(cuda.core): build advise reverse-lookup eagerly at module load (N4)
Per Leo's review on PR #1775 (_managed_memory_ops.pyx:23), drop the lazy-init plumbing for the enum→alias reverse lookup table. The forward table _MANAGED_ADVICE_ALIASES has six entries; building the inverse at module load via a dict comprehension is the same data without the mutable-global pattern, the `if None` check, or the `global` declaration inside the function body. Forward lookup table (_MANAGED_ADVICE_ALIASES) is preserved as the source of truth — explicit alias→CUDA-name mapping, grep-friendly, no implicit naming-convention coupling. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 6204c57 commit 36012fd

1 file changed

Lines changed: 6 additions & 9 deletions

File tree

cuda_core/cuda/core/_memory/_managed_memory_ops.pyx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,12 @@ cdef dict _MANAGED_ADVICE_ALLOWED_LOCTYPES = {
4747
"unset_accessed_by": _DEVICE_HOST_ONLY,
4848
}
4949

50-
# Lazily cached: maps driver.CUmem_advise enum value → string alias.
51-
cdef dict _ADVICE_ENUM_TO_ALIAS = None
50+
# Reverse lookup: enum value → alias. Built once at module load.
51+
cdef dict _ADVICE_ENUM_TO_ALIAS = {
52+
getattr(driver.CUmem_advise, attr_name): alias
53+
for alias, attr_name in _MANAGED_ADVICE_ALIASES.items()
54+
if hasattr(driver.CUmem_advise, attr_name)
55+
}
5256

5357

5458
cdef tuple _normalize_managed_advice(object advice):
@@ -65,13 +69,6 @@ cdef tuple _normalize_managed_advice(object advice):
6569
return alias, getattr(driver.CUmem_advise, attr_name)
6670

6771
if isinstance(advice, driver.CUmem_advise):
68-
global _ADVICE_ENUM_TO_ALIAS
69-
if _ADVICE_ENUM_TO_ALIAS is None:
70-
_ADVICE_ENUM_TO_ALIAS = {}
71-
for alias, attr_name in _MANAGED_ADVICE_ALIASES.items():
72-
enum_val = getattr(driver.CUmem_advise, attr_name, None)
73-
if enum_val is not None:
74-
_ADVICE_ENUM_TO_ALIAS[enum_val] = alias
7572
alias = _ADVICE_ENUM_TO_ALIAS.get(advice)
7673
if alias is None:
7774
raise ValueError(f"Unsupported advice value: {advice!r}")

0 commit comments

Comments
 (0)