DRuntime: Move Functions from `rt/lifetime.d` to `core.lifetime.d` as Templates
Teodor Dutu
teodor.dutu at gmail.com
Wed Jun 21 03:54:16 UTC 2023
Hi,
I am working on converting [`_d_newarray{U,iT,T}` to a
template](https://github.com/dlang/dmd/pull/15299).
`_d_newarrayU()` calls `__setArrayAllocLength()`, which is
[defined in
`rt/lifetime.d`](https://github.com/dlang/dmd/blob/17c3f994e845ff0b63d7b5f6443fe5a7fdc08609/druntime/src/rt/lifetime.d#L265). Due to how DRuntime is built, `rt/lifetime.d` cannot be imported into `core/lifetime.d` (or `core/internal/lifetime.d`).
To circumvent this, I also moved `__setArrayAllocLength()` along
with other functions in that call stack such as `__arrayStart()`
to `core/lifetime.d`. To avoid some linker errors caused by the
fact that `core/lifetime.d` is not compiled into the
`libdruntime.a` as it's only meant to contain templates, I
converted `__setArrayAllocLength()` and the other functions to
["empty" templates (with no template
arguments)](https://github.com/dlang/dmd/blob/bde3a7f0d6dd043b7981c8b7cc839607c53ed251/druntime/src/core/lifetime.d#L3130) just to see if they work. They do.
So my approach now is to convert these new templates to "real"
templates, that use their template argument instead of
`TypeInfo`. This will help use `TypeInfo` less (and hopefully at
some point not use it at all in the runtime) but may lead to a
lot of template bloating. To reduce this, it's worth noting that
`__setArrayAllocLength()` only uses `TypeInfo` to place it at the
end of the allocated array. Therefore, the template
implementation can be split into a small template (that only
handles this part) and a larger non-template function that does
everything else (which is common for all types).
Do you agree with this approach or do you have other suggestions?
Thanks,
Teo
More information about the Digitalmars-d
mailing list