Refactoring a "simple" function in dmd source

Siarhei Siamashka siarhei.siamashka at gmail.com
Sun Dec 31 13:02:27 UTC 2023


On Sunday, 31 December 2023 at 11:07:53 UTC, Johan wrote:
> On Sunday, 31 December 2023 at 08:25:33 UTC, Walter Bright 
> wrote:
>> 1. return an InputRange rather than an array of lines
>> 2. no longer do any memory allocation (yay!)
>> 3. no more leaking memory
>> 4. stops processing the source text when the desired line is 
>> found
>> 5. support Unicode line endings
>> 6. uses Voldemort return value
>> 7. is pure, @safe, @nogc and all that good stuff
>> 8. much faster
>> 9. completely self-contained
>>
>> https://github.com/dlang/dmd/pull/15972
>>
>> and that doesn't even include an earlier refactor:
>>
>> https://github.com/dlang/dmd/pull/15969
>>
>> I eyeballed std.string.splitLines() when doing this refactor, 
>> and noticed that it also allocated memory unnecessarily.
>>
>> It's more lines of code, but the lines are simpler, and 
>> there's not a clever thing in it!
>
> Nice. Even better if you'd have used std.string.splitLines !

Or more like 
https://dlang.org/library/std/string/line_splitter.html for 
having no allocations and `@nogc` compatibility.

The listed benefits are nice and very much welcome, but it's a 
copy-paste job from Phobos into the DMD codebase. Raising a 
question again about the reasons why DMD itself isn't using 
Phobos like any normal applications do.

Also whenever game developers are complaining in this forum about 
poor `@nogc` support, they are being told that the GC is totally 
fine and they are just unreasonably biased against it. All of 
this while the DMD compiler itself is doing fancy hackish stunts 
with memory allocation. Games are latency sensitive. Compilers 
are not.

> That would have solved your point 9; self-contained == not 
> maintained (as proven by the long standing Unicode line ending 
> bug).

Regarding copy-pasting code into multiple places and maintaining 
it separately, LDC developers fixed 
https://issues.dlang.org/show_bug.cgi?id=24290 for themselves, 
but haven't contributed the fix back to DMD.


More information about the Digitalmars-d mailing list