regal icon indicating copy to clipboard operation
regal copied to clipboard

Rule: `avoidable-count`

Open anderseknert opened this issue 1 year ago • 1 comments

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 avatar Sep 28 '24 19:09 anderseknert

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 avatar Sep 28 '24 19:09 anderseknert

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 avatar Oct 27 '24 15:10 anderseknert

Rule name builtin-nitpicks? :)

anderseknert avatar Oct 27 '24 15:10 anderseknert

With numbers now interned in OPA, there is no longer a need for this 🎉

anderseknert avatar Jan 13 '25 14:01 anderseknert