Thread overview
Does to!(string)(char[]) do any memory allocation on conversion?
Dec 25
Marc
Dec 25
Temtaime
Dec 25
Mengu
Dec 25
Temtaime
Dec 25
aliak
December 25
Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else?
December 25
On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote:
> Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else?

It is translated to idup.
So yes, it allocates memory.
December 25
On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote:
> Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else?

yes, it is allocating memory. you can test such cases with @nogc [0].

you can get a char[] via .dup of a string and then you can cast(string) if you don't want to allocate any memory.

[0] https://dlang.org/spec/attribute.html#nogc
December 25
On Monday, 25 December 2017 at 14:37:01 UTC, Mengu wrote:
> On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote:
>> Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else?
>
> yes, it is allocating memory. you can test such cases with @nogc [0].
>
> you can get a char[] via .dup of a string and then you can cast(string) if you don't want to allocate any memory.
>
> [0] https://dlang.org/spec/attribute.html#nogc

dup allocates memory too
December 25
On Monday, 25 December 2017 at 14:37:01 UTC, Mengu wrote:
> yes, it is allocating memory. you can test such cases with @nogc [0].

nogc is really conservative and thus gives a lot of false positives. I'd just compare instr.ptr is outstr.ptr here and see if it changes (it will tho)
December 25
On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote:
> Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else?

As said it calls idup, which calls _trustedDup which seems to call _dup which does memory allocation -> https://github.com/dlang/druntime/blob/v2.077.1/src/object.d#L3863

I think you can use assumeUnique to avoid allocs though. See code gen here:

https://godbolt.org/g/44pLpL

December 27
On Monday, December 25, 2017 15:00:19 aliak via Digitalmars-d-learn wrote:
> On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote:
> > Does to!(string)(char[]) do any memory allocation on conversion
> > or is this similar to a cast or what else?
>
> As said it calls idup, which calls _trustedDup which seems to call _dup which does memory allocation -> https://github.com/dlang/druntime/blob/v2.077.1/src/object.d#L3863
>
> I think you can use assumeUnique to avoid allocs though. See code gen here:
>
> https://godbolt.org/g/44pLpL

assumeUnique just casts to immutable and should be used with extreme care as casting from mutable or const to immutable runs a serious risk of subtle bugs if done incorrectly. If you do it, the reference being cast really needs to be the only reference to that object.

Almost always, the better approach is to construct an object with a pure function that is able to implicitly convert its return value to immutable, because the compiler is able to prove that the mutable object was not passed into the function and therefore that it is unique.

In the case of strings, to!string is good to use, but it will result in a new string being allocated if it's not given an immutable string. std.conv.to generally tries to ensure that conversions are done in a safe manner, and casting mutability is risky business and best to be avoided in general.

- Jonathan M Davis