[Issue 24523] New: writeln doesn't memoize its templates

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Apr 26 16:24:31 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=24523

          Issue ID: 24523
           Summary: writeln doesn't memoize its templates
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: phobos
          Assignee: nobody at puremagic.com
          Reporter: crazymonkyyy at gmail.com

copy and pasted from the open d discord some messages:

----

as far as I understand, writeln is a variadic template, so for each set of
arguments you get a new instance of writeln in object code

--
I would expect a trivail optiomization here with how templates auto memoize
--
on practice, this generates a lot of garbage code
https://godbolt.org/z/9Gx6ja5vo
void main()
{
    import std.stdio : writeln;
    writeln("hello");
    writeln(123);
    writeln(123, 456);
}
--
it dups the template argument list 3 times

--
... why isnt writeln writen in such a way to auto memoize simplier write calls
its one line of code difference
--
wat?
--

```d
    void writeln(S...)(S args)
    {
        write(args, '\n');
    }
```
given grims code, write(int,int,string) and write(int,string) are both being
compiled
when it chould be 
```d
void writeln(T...)(T args){
  static foreach(a;args){write(a);}
  write("\n");
}
```
and only compile write(int) and write(string)
---
I can't believe it, but monkyyy is actually correct on this one
https://godbolt.org/z/edf46vcqv
import std.stdio;

void smartWriteln(T...)(T args)
{
    static foreach(a; args) { write(a);}
    write("\n");
}

void a()
{
    writeln(123, 345, 789);
    smartWriteln(123, 345, 789);
}

---


etc.
writeln should be cleaned up and made to memoize arguments

--


More information about the Digitalmars-d-bugs mailing list