#Convention: allocators

1 messages · Page 1 of 1 (latest)

thorny smelt
#

Quick question about convention:
With the 0.15.0 change for ArrayList to not keep its allocator as a field, is it a good idea to modify my code such that allocators are always provided? Are there any dangers to doing this?

sterile echo
#

usually you'd want every function that does allocations to take in a std.mem.Allocator argument; this has a few benefits, such as making allocation points obvious, and reducing the amount of std.mem.Allocator stored in the program

thorny smelt
#

what I did before was store *std.mem.Allocator and dereference every time I needed it

#

the idea of passing it everytime is a little annoying but I get the idea behind it

sterile echo
thorny smelt
#

anyway the context of all of this was that I was implementing a forth in zig (I had 0.14.0 as zls wasn't updated to 0.15.0 yet), and when updating zig and zls everything broke

#

which meant I had to fix everything

#

and that gave me the idea of removing the allocator field

stiff harbor
thorny smelt
vernal moon
#

it's inited inside the parse function, and deinited at the end, meaning there's really only ever a single one

#

the main point of unmanaged datastructures is to avoid storing multiple copies of the same allocator, which wastes memory and makes it harder to tell what allocator is being used where

sterile echo
vernal moon
#

mhm

#

the only real thing u lose out on by doing the "put the allocator on a local singleton that only exists for the duration of a function" thing that std.zig.Parse does is the explicitness - it's harder to see what functions allocate