Skip to content

Commit 4e3362b

Browse files
authored
module loader: Utilize CPU better (hashicorp#391)
1 parent 18e794b commit 4e3362b

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

internal/terraform/module/module_loader.go

+28-5
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ type moduleLoader struct {
2323
}
2424

2525
func newModuleLoader() *moduleLoader {
26-
nonPrioParallelism := 2 * runtime.NumCPU()
27-
prioParallelism := 1 * runtime.NumCPU()
28-
26+
p := loaderParallelism(runtime.NumCPU())
2927
plc, lc := int64(0), int64(0)
3028
ml := &moduleLoader{
3129
queue: newModuleOpsQueue(),
3230
logger: defaultLogger,
33-
nonPrioParallelism: int64(nonPrioParallelism),
34-
prioParallelism: int64(prioParallelism),
31+
nonPrioParallelism: p.NonPriority,
32+
prioParallelism: p.Priority,
3533
opsToDispatch: make(chan ModuleOperation, 1),
3634
loadingCount: &lc,
3735
prioLoadingCount: &plc,
@@ -40,6 +38,31 @@ func newModuleLoader() *moduleLoader {
4038
return ml
4139
}
4240

41+
type parallelism struct {
42+
NonPriority, Priority int64
43+
}
44+
45+
func loaderParallelism(cpu int) parallelism {
46+
// Cap utilization for powerful machines
47+
if cpu >= 4 {
48+
return parallelism{
49+
NonPriority: int64(3),
50+
Priority: int64(1),
51+
}
52+
}
53+
if cpu == 3 {
54+
return parallelism{
55+
NonPriority: int64(2),
56+
Priority: int64(1),
57+
}
58+
}
59+
60+
return parallelism{
61+
NonPriority: 1,
62+
Priority: 1,
63+
}
64+
}
65+
4366
func (ml *moduleLoader) SetLogger(logger *log.Logger) {
4467
ml.logger = logger
4568
}

0 commit comments

Comments
 (0)