digitalmars.D.learn - Does to!(string)(char[]) do any memory allocation on conversion?
- Marc (2/2) Dec 25 2017 Does to!(string)(char[]) do any memory allocation on conversion
- Temtaime (3/5) Dec 25 2017 It is translated to idup.
- Mengu (6/8) Dec 25 2017 yes, it is allocating memory. you can test such cases with @nogc
- Temtaime (2/10) Dec 25 2017 dup allocates memory too
- Adam D. Ruppe (4/6) Dec 25 2017 nogc is really conservative and thus gives a lot of false
- aliak (7/9) Dec 25 2017 As said it calls idup, which calls _trustedDup which seems to
- Jonathan M Davis (15/24) Dec 27 2017 assumeUnique just casts to immutable and should be used with extreme car...
Does to!(string)(char[]) do any memory allocation on conversion or is this similar to a cast or what else?
Dec 25 2017
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.
Dec 25 2017
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
Dec 25 2017
On Monday, 25 December 2017 at 14:37:01 UTC, Mengu wrote:On Monday, 25 December 2017 at 14:12:32 UTC, Marc wrote:dup allocates memory tooDoes 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
Dec 25 2017
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)
Dec 25 2017
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
Dec 25 2017
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: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 DavisDoes 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
Dec 27 2017