Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ExcludeLocationFile JSON marshalling option #6398

Merged

Conversation

anderseknert
Copy link
Member

Repeating the name of the file in each location is often redundant, and large AST trees carry thousands of these attributes. Providing an option to have them removed at least for serialization (as anything more would be a breaking change) seems like a good compromise.

For reference, see StyraInc/regal#408

@anderseknert
Copy link
Member Author

The difference in Regal linting the Kics repo:

main

1700 files linted. 14233 violations found in 1696 files.
regal lint --enable-all kics/assets  191.61s user 7.18s system 610% cpu 32.553 total

without file

1700 files linted. 14233 violations found in 1696 files.
wegal lint --enable-all kics/assets  169.34s user 5.96s system 610% cpu 28.737 total

22 seconds less spent in the processor, and 4 seconds less of wallclock time. Quite an improvement!

Copy link
Contributor

@johanfylling johanfylling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

Just one question: can't the AST being marshaled be constructed from source coming from multiple files? Is the idea here that the user either knows that all source is coming from the same module, or the user just doesn't care?

@@ -96,15 +96,32 @@ func (loc *Location) Compare(other *Location) int {

func (loc *Location) MarshalJSON() ([]byte, error) {
// structs are used here to preserve the field ordering of the original Location struct
if loc.JSONOptions.MarshalOptions.ExcludeLocationFile {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing something like

data := struct {
	File string `json:"file,omitempty"`
	Row  int    `json:"row"`
	Col  int    `json:"col"`
	Text []byte `json:"text,omitempty"`
}{
	Row: loc.Row,
	Col: loc.Col,
}

if !loc.JSONOptions.MarshalOptions.ExcludeLocationFile {
	data.File = loc.File
}

would eliminate some redundancy here without changing the behavior, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that, and I could swear I had empty "" show up still, but now that I try again, it seems to work. If CI is happy too, then let's have this merged :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had edited code in the wrong location 😬 It doesn't work as I'm still seeing tests fail with "file": "". Not sure why omitempty doesn't bite.

Repeating the name of the file in each location is often redundant,
and large AST trees carry thousands of these attributes. Providing
an option to have them removed at least for serialization (as anything
more would be a breaking change) seems like a good compromise.

For reference, see StyraInc/regal#408

Signed-off-by: Anders Eknert <anders@styra.com>
@anderseknert
Copy link
Member Author

Just one question: can't the AST being marshaled be constructed from source coming from multiple files?

The AST is always parsed from a single file. I guess you could add elements to that programmatically, but then it'd be your responsibility to ensure they follow the same conventions for JSON marshalling.

@anderseknert anderseknert merged commit 69a4f3d into open-policy-agent:main Nov 10, 2023
24 checks passed
@anderseknert anderseknert deleted the location-skip-file branch November 10, 2023 14:37
anderseknert added a commit to StyraInc/regal that referenced this pull request Nov 10, 2023
As we now can exclude that: open-policy-agent/opa#6398

About a 10% reduction in eval time linting the Kics repo 🚀

We will still add a "file" attribute to locations reported in violations,
but we'll source that from `input.regal.file.name` instead, and won't have
to worry about `walk` traversing thousands of redundant nodes.

Signed-off-by: Anders Eknert <anders@styra.com>
anderseknert added a commit to StyraInc/regal that referenced this pull request Nov 10, 2023
As we now can exclude that: open-policy-agent/opa#6398

About a 10% reduction in eval time linting the Kics repo 🚀

We will still add a "file" attribute to locations reported in violations,
but we'll source that from `input.regal.file.name` instead, and won't have
to worry about `walk` traversing thousands of redundant nodes.

Signed-off-by: Anders Eknert <anders@styra.com>
anderseknert added a commit to StyraInc/regal that referenced this pull request Nov 10, 2023
As we now can exclude that: open-policy-agent/opa#6398

About a 10% reduction in eval time linting the Kics repo 🚀

We will still add a "file" attribute to locations reported in violations,
but we'll source that from `input.regal.file.name` instead, and won't have
to worry about `walk` traversing thousands of redundant nodes.

Signed-off-by: Anders Eknert <anders@styra.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants