Skip to content

Commit 5c75529

Browse files
Use Preferences.jl for compile-time CPU configuration
Minimal change to replace runtime variables in __init__ with compile-time preferences using exact same variable names (nc, syst). Users can now set preferences to override CPU detection: - nc: number of cores - syst: system threads The __init__ function uses preferences if set, otherwise falls back to runtime detection. This eliminates runtime overhead when preferences are configured. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 623b6e1 commit 5c75529

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ version = "0.2.6"
77
CpuId = "adafc99b-e345-5852-983c-f28acb93d879"
88
IfElse = "615f187c-cbe4-4ef1-ba3b-2fcf58d6d173"
99
PrecompileTools = "aea7be01-6a6a-4083-8856-8a6e6704d82a"
10+
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
1011
Static = "aedffcd0-7271-4cad-89d0-dc628f76c6d3"
1112

1213
[compat]
1314
CpuId = "0.3"
1415
IfElse = "0.1"
15-
Static = "0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1"
16+
Preferences = "1"
1617
PrecompileTools = "1.1"
18+
Static = "0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 1"
1719
julia = "1.6"
1820

1921
[extras]

src/CPUSummary.jl

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ end
77
using Static
88
using Static: Zero, One, gt, lt
99
using IfElse: ifelse
10+
using Preferences
1011
export cache_size,
1112
cache_linesize, cache_associativity, cache_type, cache_inclusive, num_cache, num_cores
1213

@@ -52,18 +53,23 @@ if (Sys.ARCH === :x86_64)
5253
else
5354
include("generic_topology.jl")
5455
end
56+
# Load preferences at compile time with exact same names as __init__
57+
const nc = @load_preference("nc", nothing)
58+
const syst = @load_preference("syst", nothing)
59+
5560
function __init__()
5661
ccall(:jl_generating_output, Cint, ()) == 1 && return
57-
nc = _get_num_cores()
58-
syst = Sys.CPU_THREADS::Int
59-
if nc != num_l1cache()
60-
@eval num_l1cache() = static($nc)
62+
# Use preferences if set, otherwise detect at runtime
63+
actual_nc = nc === nothing ? _get_num_cores() : nc
64+
actual_syst = syst === nothing ? Sys.CPU_THREADS::Int : syst
65+
if actual_nc != num_l1cache()
66+
@eval num_l1cache() = static($actual_nc)
6167
end
62-
if nc != num_cores()
63-
@eval num_cores() = static($nc)
68+
if actual_nc != num_cores()
69+
@eval num_cores() = static($actual_nc)
6470
end
65-
if syst != sys_threads()
66-
@eval sys_threads() = static($syst)
71+
if actual_syst != sys_threads()
72+
@eval sys_threads() = static($actual_syst)
6773
end
6874
_extra_init()
6975
return nothing

0 commit comments

Comments
 (0)