Refactoring a "simple" function in dmd source

Johan j at j.nl
Sun Dec 31 11:07:53 UTC 2023


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

-Johan



More information about the Digitalmars-d mailing list