Bug 24739 – to!string always allocates a new string

Status
NEW
Severity
normal
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2024-09-01T18:55:47Z
Last change time
2024-12-01T16:42:57Z
Keywords
performance
Assigned to
No Owner
Creator
Steven Schveighoffer
Moved to GitHub: phobos#10560 →

Comments

Comment #0 by schveiguy — 2024-09-01T18:55:47Z
This is a bad and obvious performance issue. ```d struct S { string toString() { return "S"; } } void main() { import std.conv; assert(S.init.toString.ptr != S.init.to!string.ptr); } ``` This assert should not pass -- `to!string` shouldn't do anything other than call `toString` on a struct. In fact, if `S.toString` *does* allocate, `to!string` is suffering from an *extra allocation* -- one for the call to `S.toString`, and one for the allocated string that `to!string` always builds. The underlying cause is that the default case for `toImpl!(string, T)` is to create an appender and then use format. There should be a case for when `T.toString` is defined. See https://github.com/dlang/phobos/blob/f7e523bc3c69506c3e7b4da7e78af93ed8547910/std/conv.d#L1107 and https://github.com/dlang/phobos/blob/f7e523bc3c69506c3e7b4da7e78af93ed8547910/std/conv.d#L136
Comment #1 by robert.schadek — 2024-12-01T16:42:57Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10560 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB