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:
Sebastiaan van Stijn
2025-04-12 12:58:13 +02:00
parent 0462b5e318
commit 7eda35fd05

View File

@@ -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
}