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

refact: avoid mem alloc with disabled decision log #632

Conversation

regeda
Copy link
Contributor

@regeda regeda commented Jan 6, 2025

Benchmark results.

goos: darwin
goarch: arm64
pkg: github.com/open-policy-agent/opa-envoy-plugin/internal
cpu: Apple M2 Pro

Before:

BenchmarkCheck-12             15540             80774 ns/op           55005 B/op       1090 allocs/op

After:

BenchmarkCheck-12             15696             80721 ns/op           54839 B/op       1087 allocs/op

srenatus
srenatus previously approved these changes Jan 6, 2025
Copy link
Collaborator

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thanks!

txn, err := store.NewTransaction(ctx, params)
if err != nil {
noopCloser := func(ctx context.Context, err error) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit

Suggested change
noopCloser := func(ctx context.Context, err error) error {
noopCloser := func(context.Context, error) error {

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this function literal is simple enough that it won't leak to the heap, but you could try to rewrite it a package level function, e.g.

func noopCloser(ctx context.Context, err error) error {
    return nil // no-op default
}

and then reference that instead. Measure and see if it makes any difference :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made this change. However, there are no significant changes from the benchmark perspective.

@@ -21,15 +20,10 @@ func (e *internalError) Error() string {
}

// LogDecision - Logs a decision log event
func LogDecision(ctx context.Context, manager *plugins.Manager, info *server.Info, result *envoyauth.EvalResult, err error) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Technically this is a breaking change. Someone could be using this function from elsewhere.

The "fix" would be to add another static function here and keep the old one as-is. But I think it's very unlikely that someone cares for this function, so let's not be too timid about it. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe it's worth to move decisionlog package under the internal directory.

Copy link
Collaborator

Choose a reason for hiding this comment

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

We will technically be releasing a major version (v1.0.0) of plugin in the next weeks so I'm also not too worried about breaking change... before we release v1 of plugin I'll try to take a look at what code we want to support as importable in the future for long-term.

Copy link
Contributor

Choose a reason for hiding this comment

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

Skipper is using the function. It would have been nice to have a note in the release note mentioning it's a breaking change. Still since it's 1.0.0 breaking changes were expected.

@regeda regeda force-pushed the avoid-memalloc-with-disabled-decisionlog branch from d68c4ca to d07f08e Compare January 6, 2025 10:30
Copy link
Collaborator

@srenatus srenatus left a comment

Choose a reason for hiding this comment

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

Thank you!

Copy link
Collaborator

@tjons tjons left a comment

Choose a reason for hiding this comment

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

LGTM! Thanks for your contribution

@@ -21,15 +20,10 @@ func (e *internalError) Error() string {
}

// LogDecision - Logs a decision log event
func LogDecision(ctx context.Context, manager *plugins.Manager, info *server.Info, result *envoyauth.EvalResult, err error) error {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We will technically be releasing a major version (v1.0.0) of plugin in the next weeks so I'm also not too worried about breaking change... before we release v1 of plugin I'll try to take a look at what code we want to support as importable in the future for long-term.

@tjons tjons merged commit e114232 into open-policy-agent:main Jan 6, 2025
8 checks passed
pratimsc pushed a commit to pratimsc/opa-envoy-plugin that referenced this pull request Jan 6, 2025
@regeda regeda deleted the avoid-memalloc-with-disabled-decisionlog branch January 7, 2025 20:28
Pushpalanka added a commit to zalando/skipper that referenced this pull request Feb 5, 2025
Address breaking change from open-policy-agent/opa-envoy-plugin#632

Signed-off-by: Pushpalanka Jayawardhana <[email protected]>
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.

5 participants