prealloc icon indicating copy to clipboard operation
prealloc copied to clipboard

Map preallocation

Open scop opened this issue 4 years ago • 3 comments
trafficstars

Preallocation of maps can be useful, as shown by PR #23, would be nice to have prealloc suggest that too.

Just wondering how smart it can be made as things are not as straightforward as with slices because there's key uniqueness at play. Cases where the optimal preallocated capacity can be easily determined beforehand are more rare with maps, and in that sense suggesting it could have a worse signal to noise ratio. Maybe it should be made optional if it can't be made "smart enough" :thinking:

scop avatar Oct 23 '21 07:10 scop

Hey there, thanks for the issue! I'm not sure if we can realistically determine whether or not a given range or for loop with have duplicate key insertions into a map (we simply don't have access to that information using the AST). We could potentially suggest capacity hints, but as you mentioned, I think this may end up having more false positives than anything.

Happy to entertain contributions if you feel you can come up with something, feel free to experiment/open a PR :smile:

alexkohler avatar Oct 26 '21 11:10 alexkohler

+1 for map preallocation checking

Antonboom avatar Dec 16 '21 19:12 Antonboom

It should be possible at least to detect map→map copies?

Porges avatar May 25 '22 20:05 Porges