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

Rule: avoidable-count #1151

Open
anderseknert opened this issue Sep 28, 2024 · 3 comments
Open

Rule: avoidable-count #1151

anderseknert opened this issue Sep 28, 2024 · 3 comments

Comments

@anderseknert
Copy link
Member

These two are equivalent, as -1 means "to the end of the string". Avoiding the count call is however slightly more efficient, and should be preferred if performance is a concern.

r1 := substring(s, 3, count(s))

r2 := substring(s, 3, -1)

r1

+-------------------------------------------+------------+
| samples                                   |      56862 |
| ns/op                                     |      19973 |
| B/op                                      |      12022 |
| allocs/op                                 |        218 |

r2

+-------------------------------------------+------------+
| samples                                   |      70899 |
| ns/op                                     |      16716 |
| B/op                                      |      10157 |
| allocs/op                                 |        177 |
@anderseknert
Copy link
Member Author

As @srenatus found some time back, the caching of numbers in OPA doesn't work as it was expected. Perhaps this issue would be less relevant if it did, and if that'll be the case later, we won't have to do this.

@anderseknert
Copy link
Member Author

Coming back to this, I think it's too small to motivate a rule of its own. However, perhaps we could have a rule collect smaller optimizations within the scope of a single rule? Wouldn't necessarily all have to be about performance either, but cover any non-optimal use of built-ins like platform-dependent sprintf args, etc.

@anderseknert
Copy link
Member Author

Rule name builtin-nitpicks? :)

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

No branches or pull requests

1 participant