mm218.dev icon indicating copy to clipboard operation
mm218.dev copied to clipboard

posts/2023-08-29-allocations/index

Open utterances-bot opened this issue 2 years ago • 2 comments

Mike Mahoney - Pre-allocating vectors is for nerds

Or rather: growing objects is inefficient. But it’s maybe not as big a deal as I’d believed.

https://www.mm218.dev/posts/2023-08-29-allocations/index.html

utterances-bot avatar Aug 29 '23 21:08 utterances-bot

Over on Mastodon, June Choe points to the commits in R associated with this change https://fosstodon.org/@yjunechoe/110975018204561319

@MikeMahoney218 "Always pre-allocate your vectors" is a bit of an outdated dogma. Since v3.4, R already over-allocates memory by a small factor (x1.05) every time a vector needs to grow!

  • https://github.com/wch/r-source/commit/12aa371f88e5ece5015246e4f4b3e0b2b7f21639

  • https://github.com/wch/r-source/commit/37388191f5493825923b98246ec02e9b13ab1770

(I learned this from a Hadley tweet a long time ago but can't seem to locate the original discussion)

mikemahoney218 avatar Aug 29 '23 21:08 mikemahoney218

Hi Mike. I tried an even "devilish" approach using c and unlist and you can see that the results are similar https://gist.github.com/jrosell/ebb604e8c35a8665e2380ef3ea952464

I get the idea from @JosiahParry video about loops https://youtu.be/TdbweYvwnss?si=ho3NKWtBF-KW60HX

jrosell avatar Aug 30 '23 07:08 jrosell