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 optimized functions for matching an array of strings with an array of prefixes/suffixes. #4994

Closed
cube2222 opened this issue Aug 10, 2022 · 2 comments · Fixed by #4997
Closed

Comments

@cube2222
Copy link
Contributor

What is the underlying problem you're trying to solve?

Hey!
At Spacelift we integrate with OPA extensively. One of the use cases is reacting to Push events and deciding whether it's interesting for the current Stack or not.

A pattern we've been seeing our users writing very commonly, is the following:

track {
    startswith(input.push.affected_files[_], interesting_paths[_])
}

which, if both affected_files and interesting_paths are dynamic arrays, ends up being O(n^3), and is a bottleneck for many policies.

Describe the ideal solution

Add a function any_prefix_matches_any_string (name TBD) which accepts two arguments which are both arrays of strings. Then checks if any of the strings in the second argument is a prefix of any of the strings in the first argument, but does it fast.

Similarly, a any_suffix_matches_any_string (name TBD) function for suffixes.

For a test case of ours which with the naive approach takes 60ms, a draft implementation based on Patricia trees takes 0.5-1ms.

Additional Context

Very happy to contribute this.

@ashutosh-narkar
Copy link
Member

@cube2222 does you implementation add couple of builtins that perform the above operations? Feel free to open a draft PR with your changes.

@cube2222
Copy link
Contributor Author

Sure! Here's a draft implementation @ashutosh-narkar: #4997

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants