crystal-lang / crystal

The Crystal Programming Language
https://crystal-lang.org
Apache License 2.0
19.49k stars 1.62k forks source link

Array reallocation API #8209

Open jkthorne opened 5 years ago

jkthorne commented 5 years ago

I think there should be an API for resizing an array. This is useful when you know you are about to insert a large amount of data and you only want one allocation of memory and one memory copy.

This is a followup from a conversation on the array buffer refactor. https://github.com/crystal-lang/crystal/pull/8166#issuecomment-534233809

Pros: Gives more flexibility in memory allocations and the potential for performance.

Cons: More APIs to support and is a more advanced feature.

rdp commented 2 years ago

So you don't know the size when you first create it, I presume? Maybe make resize_to_capacity public?

beta-ziliani commented 2 years ago

Note: This is already addressed in #11485 (Array#ensure_capacity).