mirror of
https://github.com/moby/moby.git
synced 2026-01-11 18:51:37 +00:00
profiles/apparmor: IsLoaded: optimize
- Use a bufio.Scanner to read the profiles
- Use strings.Cut
Before/After:
BenchmarkIsLoaded-10 2258 508049 ns/op 244266 B/op 10004 allocs/op
BenchmarkIsLoaded-10 5680 208703 ns/op 4264 B/op 4 allocs/op
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
@@ -103,20 +103,17 @@ func isLoaded(name string, fileName string) (bool, error) {
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
r := bufio.NewReader(file)
|
||||
for {
|
||||
p, err := r.ReadString('\n')
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if strings.HasPrefix(p, name+" ") {
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
if prefix, _, ok := strings.Cut(scanner.Text(), " "); ok && prefix == name {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := scanner.Err(); err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
return false, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user