12 Commits

Author SHA1 Message Date
Anders Eknert 413903e8cc Enable modernize linter for golangci-lint (#8996)
Didn't know this was a thing now. That certainly helps! Also some
follow-up fixes from the previous modernize PR.

---------

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
Signed-off-by: Charlie Egan <charlie_egan@apple.com>
Co-authored-by: Charlie Egan <charlie_egan@apple.com>
2026-08-10 17:05:31 +01:00
Anders Eknert a3c28af2b6 perf: improved annotations parsing (#8210)
This wasn't really work I planned to do, and not driven by performance
requirements. Just stumbled upon the metadata parsing code and thought
it could be made better looking. And I think it is now, while also
performing a bit better. Almost all the remaining cost now is unmarshalling
YAML though, so I think this is about as good as it gets. Also added two
new benchmarks for this.

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
2026-01-13 10:40:44 +01:00
Anders Eknert e03ac2f200 Bump golangci-lint, more gocritic linters (#8052)
- Bump golangci-lint -> 2.6.2
- Fix all `deprecatedComment` "notices should be in a dedicated paragraph, separated from the rest" reports
- Enable `appendCombine` and fix all "appendCombine: can combine chain of X appends into one" notices
- Enable `preferFprint` and fix the few reported issues
- Fix various issues reported only once or twice, like `zeroByteRepeat`

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
2025-11-17 11:08:39 +01:00
Anders Eknert a1addb098e Intern common var values + some parser improvements (#8028)
While we can't use interned `Term`s in the parser (mutable), we
can use interned `Value`s (immutable) and put in those terms. Seeing
how often we would allocate a new `Value` for e.g. "input" or "data"
made me want to try interning for vars. Just like all things interning,
this is slightly experimental and clearly marked as such. Worth highlighting
here is that the majority of vars allocated in at least Regal is however
those generated in eval.go.. and their names don't lend themselves well
to interning beforehand. Perhaps we can find a way to change that later,
as those account for a million+ allocations in `regal lint bundle`. Oh well.

Also some general improvements in the parser code, reducing allocations often
simply by moving things around in order to not evaluate (potentially allocating)
code before it's needed.

Signed-off-by: Anders Eknert <anders@eknert.com>
2025-11-10 10:18:32 +01:00
Manuela Züger fea69a4ce4 Fix benchmark test which was introduced with PR 7970 (#7993)
Related to issue: #7433

Signed-off-by: Manuela Züger <manuela.zueger@ipt.ch>
2025-10-27 18:03:28 -05:00
Anders Eknert d3f34a3387 Modernize analyzer fixes (#7965)
Have done this some time in the past, but there was a few
new issues this would highlight now that we're on Go 1.24.

Mostly:
- Use `b.Loop()` in benchmarks
- Use `strings.SplitSeq` where possible
- Remove `omitempty` tag for types that can't be empty

Signed-off-by: Anders Eknert <anders@eknert.com>
2025-10-10 17:51:02 +02:00
Anders Eknert 738ec3630b perf: Don't invoke future parser for Rego v1 (#7909)
Just a little something that came up when testing `json/v2`.
Not a big issue, but also makes no sense to instantiate a new
parser when it won't be needed.

**Before/after**
```
BenchmarkParseSome/parse_some-16    162577  7317 ns/op   8088 B/op   53 allocs/op
BenchmarkParseSome/parse_some-16    135363  8754 ns/op	 10536 B/op  69 allocs/op
```

Signed-off-by: Anders Eknert <anders@eknert.com>
2025-09-18 20:05:31 +02:00
Ville Vesilehto 370e626335 ast: Parser recursion depth guard (#7568)
Add enter/leave helpers that bump a depth counter and fail with
ErrMaxParsingRecursionDepthExceeded once the limit (default 100k)
is reached. Every recursive parse helper now calls them.

Expose WithMaxRecursionDepth for callers that need higher limits.
If limit is set to 0 then recursion tracking is effectively disabled.

Tests utilise a much lower depth limit.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
2025-05-28 15:25:27 +02:00
Anders Eknert e43ef0a979 Use any in place of interface{} (#7566)
Earlier this evening I tried to run the Go
[modernize](https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize)
analyzer on OPA. That didn't go as planned:

- https://github.com/golang/go/issues/73661
- https://github.com/golang/go/issues/73663

While we wait for that to be fixed, I figured an old-fashioned
search-and-replace across the repo may work for at least the
`interface{}` to `any` conversion. That should help make it easier
to see the other fixes as applied by the modernize tool once it has
had those issues resolved.

Signed-off-by: Anders Eknert <anders@styra.com>
2025-05-12 13:57:48 +02:00
Anders Eknert 55e87e79ae Add perfsprint linter (#7334)
And update code to conform to the rule.

- Replace unnecessary fmt.Sprintf with string concatenation
- Replace fmt.Sprint with more efficient strconv.Itoa
- Replace static fmt.Errorf calls with more efficient errors.New

Thanks @srenatus for pushing me down this rabbit hole!

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-31 20:24:05 +01:00
Anders Eknert b942136a4a Use Go 1.22+ int ranges (#7328)
With "some" help from `golangci-lint run --fix ./...`

Signed-off-by: Anders Eknert <anders@styra.com>
2025-01-30 09:57:27 +01:00
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00