Skip to content

Commit

Permalink
perf: move config check out of loop
Browse files Browse the repository at this point in the history
In main.rego there are two places where we extract a relative path from
the input policy file. This was however done in a loop despite only using
variables from outside of the loop. Turns out there was close to a million
allocations hiding there, lol.

**BenchmarkRegalLintingItself-10 Before**
```
1875658375 ns/op    3539151104 B/op    70020076 allocs/op
```
**BenchmarkRegalLintingItself-10 After**
```
1821361667 ns/op    3499082704 B/op    69062781 allocs/op
```

Signed-off-by: Anders Eknert <[email protected]>
  • Loading branch information
anderseknert committed Jan 20, 2025
1 parent 193224a commit 9022d3a
Showing 1 changed file with 5 additions and 10 deletions.
15 changes: 5 additions & 10 deletions bundle/regal/main/main.rego
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ lint.aggregate.violations := aggregate_report if {
}

_rules_to_run[category] contains title if {
file_name_relative_to_root := trim_prefix(input.regal.file.name, concat("", [config.path_prefix, "/"]))

some category, title
config.merged_config.rules[category][title]

config.for_rule(category, title).level != "ignore"

file_name_relative_to_root := trim_prefix(input.regal.file.name, concat("", [config.path_prefix, "/"]))

not config.excluded_file(
category,
title,
Expand Down Expand Up @@ -105,20 +105,15 @@ report contains violation if {

# Check custom rules
report contains violation if {
file_name_relative_to_root := trim_prefix(input.regal.file.name, concat("", [config.path_prefix, "/"]))

some category, title

violation := data.custom.regal.rules[category][title].report[_]

config.for_rule(category, title).level != "ignore"

file_name_relative_to_root := trim_prefix(input.regal.file.name, concat("", [config.path_prefix, "/"]))

not config.excluded_file(
category,
title,
file_name_relative_to_root,
)

not config.excluded_file(category, title, file_name_relative_to_root)
not _ignored(violation, ast.ignore_directives)
}

Expand Down

0 comments on commit 9022d3a

Please sign in to comment.