prealloc
prealloc copied to clipboard
Map preallocation
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:
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:
+1 for map preallocation checking
It should be possible at least to detect map→map copies?