Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to!string always allocates a new string #10560

Open
dlangBugzillaToGithub opened this issue Sep 1, 2024 · 0 comments
Open

to!string always allocates a new string #10560

dlangBugzillaToGithub opened this issue Sep 1, 2024 · 0 comments

Comments

@dlangBugzillaToGithub
Copy link

schveiguy (@schveiguy) reported this on 2024-09-01T18:55:47Z

Transfered from https://issues.dlang.org/show_bug.cgi?id=24739

Description

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
@LightBender LightBender removed the P1 label Dec 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants