Optimize Allocator.allocAdvancedBla to create less duplicate code for similar types. - #16332
Conversation
… type into a function that only depends on comptime size and alignment. This reduces comptime code duplication because e.g. `alloc(u32, )` and `alloc(i32, )` now use the same function `allocWithBla(4, 4, )` under the hood.
|
Wouldn't it also be possible to remove the generic |
Then I would need to do the |
Right, make sense. One could have |
|
That would actually save an additional 45 kB. |
…ignment. Also optimized `create` to directly call the new function.
|
I also decided to make |
|
Nice work. |
|
Perf data point for building the self-hosted compiler: |
Motivation
Take a look at the following code:
This is allocating two pieces of memory with the exact same size and alignment. Yet under the hood this generates two different functions, increasing the binary size and compile time. And now in the compiler there is 102 of these plus 125 calls to
alloc.Overall
800 kBof zig's (debug) binary are wasted for allocator functions. That's~2%of the total size (measuring only zig functions).In my own project it's even worse. Almost
10%of the (debug) binary (excluding C functions) is filled with allocator functions.Solution
Essentially I put all the common functionality from
allocAdvancedWithRetAddrinto a new function that is only generic over size and alignment:This reduces the amount of code that is duplicated when two types with same size+align are allocated.
Results
The zig debug binary size was reduced by
503 kB.In my own project the debug binary was reduced by
175 kB(~4-5%of the zig functions)In my project the compile time decreased by
~0.4seconds (~3%) (Note that this comes purely from the fact that there is less duplicate code to compile)Best case improvement
The compile-time of this best-case example was reduced from 2.3 s to 1.3 s and the size of the binary decreased from 1 MB to 550 kB.