Skip to content
New issue

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

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

Already on GitHub? # to your account

Roast v0.5.0, faster input conversion #1293

Merged
merged 1 commit into from
Dec 12, 2024
Merged

Roast v0.5.0, faster input conversion #1293

merged 1 commit into from
Dec 12, 2024

Conversation

anderseknert
Copy link
Member

Using the new ToOPAInput function from RoAST cuts the number of allocations related to input transformation in half.

This doesn't constitute a bottleneck for command line linting, but will help the language server process files as quickly as possible on changes.

Also convert a few bytes/string conversions to allocation-free variants. I don't think these matter much, but having them around will help remind us that's an option for when it does matter.

Using the new `ToOPAInput` function from RoAST cuts the number
of allocations related to input transformation in half.

This doesn't constitute a bottleneck for command line linting,
but will help the language server process files as quickly as
possible on changes.

Also convert a few bytes/string conversions to allocation-free
variants. I don't think these matter much, but having them around
will help remind us that's an option for when it does matter.

Signed-off-by: Anders Eknert <anders@styra.com>
@@ -133,7 +134,7 @@ func Plan(ctx context.Context, path, rego string, usePrint bool) (string, error)
{
URL: "/url",
Path: path,
Raw: []byte(rego),
Copy link
Member

Choose a reason for hiding this comment

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

So there's one direction where no copy or allocation is happening, and I believe it's string -> []byte. Random SO answer. So does StringToByteSlice actually do something for us? 🤔

Copy link
Member Author

@anderseknert anderseknert Dec 12, 2024

Choose a reason for hiding this comment

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

That's about slicing a string though? 🤔

From the same answer:

Strings in Go are immutable. So to change a string you need to make a new string. However, strings are closely related to byte slices, e.g. you can create a byte slice from a string with

foo := `here's my string`
fooBytes := []byte(foo)

I believe that will allocate a new array of bytes, because:

a string is in effect a read-only slice of bytes

according to the Go Blog (see Strings, bytes, runes and characters in Go). In general you can use a slice to change the contents of an underlying array, so to produce a usable byte slice from a string you would have to make a copy to keep the user from changing what's supposed to be immutable.

But even with that in mind, it seems like in many situations these things don't show up in benchmarks at all in either direction. Perhaps the compiler can tell no modification happens and can optimize from there? I don't know. These don't really matter in the places where used in Regal, as all the hot spots are in OPA anyway... so I'm ambivalent.

@anderseknert anderseknert merged commit 36a72d4 into main Dec 12, 2024
5 checks passed
@anderseknert anderseknert deleted the bump-roast branch December 12, 2024 12:20
charlieegan3 pushed a commit to charlieegan3/regal that referenced this pull request Jan 6, 2025
Using the new `ToOPAInput` function from RoAST cuts the number
of allocations related to input transformation in half.

This doesn't constitute a bottleneck for command line linting,
but will help the language server process files as quickly as
possible on changes.

Also convert a few bytes/string conversions to allocation-free
variants. I don't think these matter much, but having them around
will help remind us that's an option for when it does matter.

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

Successfully merging this pull request may close these issues.

2 participants