mirror of
https://github.com/opencontainers/runc.git
synced 2025-10-13 11:14:00 +08:00
Refactor enum map range to slice range
grep -r "range map" showw 3 parts use map to range enum types, use slice instead can get better performance and less memory usage. Signed-off-by: Peng Gao <peng.gao.dut@gmail.com>
This commit is contained in:
@@ -223,17 +223,21 @@ func (l *LinuxFactory) Type() string {
|
|||||||
// This is a low level implementation detail of the reexec and should not be consumed externally
|
// This is a low level implementation detail of the reexec and should not be consumed externally
|
||||||
func (l *LinuxFactory) StartInitialization() (err error) {
|
func (l *LinuxFactory) StartInitialization() (err error) {
|
||||||
var pipefd, rootfd int
|
var pipefd, rootfd int
|
||||||
for k, v := range map[string]*int{
|
for _, pair := range []struct {
|
||||||
"_LIBCONTAINER_INITPIPE": &pipefd,
|
k string
|
||||||
"_LIBCONTAINER_STATEDIR": &rootfd,
|
v *int
|
||||||
|
}{
|
||||||
|
{"_LIBCONTAINER_INITPIPE", &pipefd},
|
||||||
|
{"_LIBCONTAINER_STATEDIR", &rootfd},
|
||||||
} {
|
} {
|
||||||
s := os.Getenv(k)
|
|
||||||
|
s := os.Getenv(pair.k)
|
||||||
|
|
||||||
i, err := strconv.Atoi(s)
|
i, err := strconv.Atoi(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to convert %s=%s to int", k, s)
|
return fmt.Errorf("unable to convert %s=%s to int", pair.k, s)
|
||||||
}
|
}
|
||||||
*v = i
|
*pair.v = i
|
||||||
}
|
}
|
||||||
var (
|
var (
|
||||||
pipe = os.NewFile(uintptr(pipefd), "pipe")
|
pipe = os.NewFile(uintptr(pipefd), "pipe")
|
||||||
|
40
update.go
40
update.go
@@ -155,33 +155,39 @@ other options are ignored.
|
|||||||
r.CPU.Mems = &val
|
r.CPU.Mems = &val
|
||||||
}
|
}
|
||||||
|
|
||||||
for opt, dest := range map[string]*uint64{
|
for _, pair := range []struct {
|
||||||
"cpu-period": r.CPU.Period,
|
opt string
|
||||||
"cpu-quota": r.CPU.Quota,
|
dest *uint64
|
||||||
"cpu-share": r.CPU.Shares,
|
}{
|
||||||
|
|
||||||
|
{"cpu-period", r.CPU.Period},
|
||||||
|
{"cpu-quota", r.CPU.Quota},
|
||||||
|
{"cpu-share", r.CPU.Shares},
|
||||||
} {
|
} {
|
||||||
if val := context.String(opt); val != "" {
|
if val := context.String(pair.opt); val != "" {
|
||||||
var err error
|
var err error
|
||||||
*dest, err = strconv.ParseUint(val, 10, 64)
|
*pair.dest, err = strconv.ParseUint(val, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid value for %s: %s", opt, err)
|
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for _, pair := range []struct {
|
||||||
for opt, dest := range map[string]*uint64{
|
opt string
|
||||||
"kernel-memory": r.Memory.Kernel,
|
dest *uint64
|
||||||
"kernel-memory-tcp": r.Memory.KernelTCP,
|
}{
|
||||||
"memory": r.Memory.Limit,
|
{"kernel-memory", r.Memory.Kernel},
|
||||||
"memory-reservation": r.Memory.Reservation,
|
{"kernel-memory-tcp", r.Memory.KernelTCP},
|
||||||
"memory-swap": r.Memory.Swap,
|
{"memory", r.Memory.Limit},
|
||||||
|
{"memory-reservation", r.Memory.Reservation},
|
||||||
|
{"memory-swap", r.Memory.Swap},
|
||||||
} {
|
} {
|
||||||
if val := context.String(opt); val != "" {
|
if val := context.String(pair.opt); val != "" {
|
||||||
v, err := units.RAMInBytes(val)
|
v, err := units.RAMInBytes(val)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("invalid value for %s: %s", opt, err)
|
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
|
||||||
}
|
}
|
||||||
*dest = uint64(v)
|
*pair.dest = uint64(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user