struct Location size

Patrick Schluter Patrick.Schluter at bbox.fr
Tue May 9 11:57:04 UTC 2023


On Tuesday, 9 May 2023 at 00:24:44 UTC, Walter Bright wrote:
> This PR https://github.com/dlang/dmd/pull/15199 reduces its 
> size by 8 bytes, resulting in about 20Mb of memory savings 
> compiling druntime, according to @dkorpel.
>
> It is currently:
>
> struct Loc
> {
>   uint linnum; // line number, starting from 1
>   ushort charnum; // utf8 code unit index relative to start of 
> line, starting from 1
>   ushort fileIndex; // index into filenames[], starting from 1 
> (0 means no filename)
> }
>
> which is 8 bytes.
>
> If it was 4 bytes, it could still store 4 billion unique 
> locations, which ought to be enough for everyone. I toyed 
> around with various encodings:
>
>  6 bits for column - 1..64
> 15 bits for line - 1..32768
> 11 bits for file - 2047
>
> I also thought of schemes like having the sign bit set an 
> alternate encoding scheme.
>
> So, for great glory, can anyone come up with a clever scheme 
> that uses only 32 bits?

Does it concern also C files handled by ImportC? If yes, be aware 
that there's no limit in C in line length and that the 
pre-processor can generate humongous lines.
In that case splitting line/column is a bad idea and file offset 
is just enough.


More information about the Digitalmars-d mailing list