Skip to content

Commit 80956fb

Browse files
authored
perf: some Rego micro-optimizations (#1549)
And simplify invalid-metadata-attribute rule. Doesn't show much on our benchmarks as most of these locations aren't hit by them. Signed-off-by: Anders Eknert <anders@styra.com>
1 parent 7b64a08 commit 80956fb

9 files changed

Lines changed: 20 additions & 28 deletions

File tree

bundle/regal/ast/ast.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ ref_to_string(ref) := ref[0].value if {
257257
_ref_part_to_string(0, part) := part.value
258258
_ref_part_to_string(i, part) := _format_part(part) if i > 0
259259

260-
_format_part(part) := sprintf(".%s", [part.value]) if {
260+
_format_part(part) := concat("", [".", part.value]) if {
261261
part.type == "string"
262262
regex.match(`^[a-zA-Z_][a-zA-Z1-9_]*$`, part.value)
263263
} else := sprintf(`["%v"]`, [part.value]) if {

bundle/regal/lsp/codelens/codelens.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ _rule_lens(filename, rule, command, title) := {
6262
"command": command,
6363
"arguments": [json.marshal({
6464
"target": filename,
65-
"path": sprintf("%s.%s", [
65+
"path": concat(".", [
6666
ast.ref_to_string(input["package"].path),
6767
ast.ref_static_to_string(rule.head.ref),
6868
]),

bundle/regal/lsp/completion/location/location.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ word_at(line, col) := word if {
9191
word := {
9292
"offset_before": count(word_before),
9393
"offset_after": count(word_after),
94-
"text": sprintf("%s%s", [word_before, word_after]),
94+
"text": concat("", [word_before, word_after]),
9595
}
9696
}
9797

@@ -110,7 +110,7 @@ ref_at(line, col) := word if {
110110
word := {
111111
"offset_before": count(word_before),
112112
"offset_after": count(word_after),
113-
"text": sprintf("%s%s", [word_before, word_after]),
113+
"text": concat("", [word_before, word_after]),
114114
}
115115
}
116116

bundle/regal/lsp/completion/providers/commonrule/commonrule.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ items contains item if {
3232
},
3333
"textEdit": {
3434
"range": location.from_start_of_line_to_position(position),
35-
"newText": sprintf("%s ", [label]),
35+
"newText": concat("", [label, " "]),
3636
},
3737
}
3838
}

bundle/regal/lsp/completion/providers/packagename/packagename.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ items contains item if {
3232
"detail": "suggested package name based on directory structure",
3333
"textEdit": {
3434
"range": location.word_range(word, position),
35-
"newText": sprintf("%s\n\n", [suggestion]),
35+
"newText": concat("", [suggestion, "\n\n"]),
3636
},
3737
}
3838
}

bundle/regal/lsp/completion/providers/rulerefs/rulerefs.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ _other_package_refs contains ref if {
5757
_rule_ref_suggestions contains pkg_ref if {
5858
some ref in _current_package_refs
5959

60-
pkg_ref := trim_prefix(ref, sprintf("%s.", [_current_file_package]))
60+
pkg_ref := trim_prefix(ref, concat("", [_current_file_package, "."]))
6161
}
6262

6363
# from imported packages
@@ -68,7 +68,7 @@ _rule_ref_suggestions contains pkg_ref if {
6868
startswith(ref, imported_package)
6969

7070
prefix := regex.replace(imported_package, `\.[^\.]+$`, "")
71-
pkg_ref := trim_prefix(ref, sprintf("%s.", [prefix]))
71+
pkg_ref := trim_prefix(ref, concat("", [prefix, "."]))
7272
}
7373

7474
# from any other package

bundle/regal/rules/bugs/impossible-not/impossible_not.rego

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ report contains violation if {
7373
# note that the "not" isn't present in the AST, so we'll add it manually to the text
7474
# in the location to try and make it clear where the issue is (as opposed to just
7575
# printing the ref)
76-
"text": sprintf("not %s", [_to_string(negated.ref)]),
76+
"text": concat(" ", ["not", _to_string(negated.ref)]),
7777
}})
7878

7979
violation := result.fail(rego.metadata.chain(), loc)
@@ -102,7 +102,7 @@ aggregate_report contains violation if {
102102
# note that the "not" isn't present in the AST, so we'll add it manually to the text
103103
# in the location to try and make it clear where the issue is (as opposed to just
104104
# printing the ref)
105-
"text": sprintf("not %s", [_to_string(negated.ref)]),
105+
"text": concat(" ", ["not", _to_string(negated.ref)]),
106106
}})
107107

108108
violation := result.fail(rego.metadata.chain(), loc)

bundle/regal/rules/bugs/invalid-metadata-attribute/invalid_metadata_attribute.rego

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,16 @@ report contains violation if {
1010

1111
startswith(trim_space(block[0].text), "METADATA")
1212

13-
text := _block_to_string(block)
14-
attributes := object.keys(yaml.unmarshal(text))
13+
text := concat("\n", [entry.text |
14+
some i, entry in block
15+
i > 0
16+
])
1517

16-
some attribute in attributes
18+
some attribute in object.keys(yaml.unmarshal(text))
1719
not attribute in ast.comments.metadata_attributes
1820

19-
violation := result.fail(
20-
rego.metadata.chain(),
21-
result.location(_find_line(block, attribute)),
22-
)
21+
violation := result.fail(rego.metadata.chain(), result.location([line |
22+
some line in block
23+
startswith(trim_space(line.text), concat("", [attribute, ":"]))
24+
][0]))
2325
}
24-
25-
_block_to_string(block) := concat("\n", [entry.text |
26-
some i, entry in block
27-
i > 0
28-
])
29-
30-
_find_line(block, attribute) := [line |
31-
some line in block
32-
startswith(trim_space(line.text), sprintf("%s:", [attribute]))
33-
][0]

bundle/regal/rules/imports/prefer-package-imports/prefer_package_imports.rego

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ aggregate_report contains violation if {
5151

5252
violation := result.fail(rego.metadata.chain(), {"location": object.union(util.to_location_no_text(location), {
5353
"file": entry.aggregate_source.file,
54-
"text": sprintf("import %s", [concat(".", array.concat(["data"], path))]),
54+
"text": concat("", ["import data.", concat(".", path)]),
5555
})})
5656
}
5757

0 commit comments

Comments
 (0)