From b71f29df8f932b0029e112e46891958731ec03d4 Mon Sep 17 00:00:00 2001 From: Anders Eknert Date: Mon, 20 Jan 2025 16:14:01 +0100 Subject: [PATCH] perf: move config check out of loop 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 --- bundle/regal/main/main.rego | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/bundle/regal/main/main.rego b/bundle/regal/main/main.rego index 0a58dca1..b9fab8b0 100644 --- a/bundle/regal/main/main.rego +++ b/bundle/regal/main/main.rego @@ -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, @@ -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) }