From d1df9a6552a596d310155aec16b5fedf7350973b Mon Sep 17 00:00:00 2001 From: Dennis Korpel Date: Thu, 13 Feb 2025 15:28:34 +0100 Subject: [PATCH] Optimize access to Loc.filename --- compiler/src/dmd/e2ir.d | 7 +++--- compiler/src/dmd/location.d | 47 ++++++++++++++++++++++++------------ compiler/src/dmd/timetrace.d | 9 ++++--- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/compiler/src/dmd/e2ir.d b/compiler/src/dmd/e2ir.d index fd40184be1..89e41526c0 100644 --- a/compiler/src/dmd/e2ir.d +++ b/compiler/src/dmd/e2ir.d @@ -6953,10 +6953,11 @@ elem* buildArrayIndexError(ref IRState irs, Loc loc, elem* index, elem* length) /// Returns: elem representing a C-string (char*) to the filename elem* locToFileElem(const ref IRState irs, Loc loc) { elem* efile; - if (loc.filename) + + if (auto fname = loc.filename) { - const len = strlen(loc.filename); - Symbol* s = toStringSymbol(loc.filename, len, 1); + const len = strlen(fname); + Symbol* s = toStringSymbol(fname, len, 1); efile = el_ptr(s); } else diff --git a/compiler/src/dmd/location.d b/compiler/src/dmd/location.d index 785122b0dc..b6cc6bc05d 100644 --- a/compiler/src/dmd/location.d +++ b/compiler/src/dmd/location.d @@ -95,7 +95,19 @@ nothrow: */ extern (C++) const(char)* filename() const @nogc { - return SourceLoc(this).filename.ptr; // _filename; + if (this.index == 0) + return null; + + const i = fileTableIndex(this.index); + if (locFileTable[i].substitutions.length > 0) + { + const si = locFileTable[i].getSubstitutionIndex(this.index - locFileTable[i].startIndex); + const fname = locFileTable[i].substitutions[si].filename; + if (fname.length > 0) + return fname.ptr; + } + + return locFileTable[i].filename.ptr; } extern (C++) const(char)* toChars( @@ -379,7 +391,23 @@ struct BaseLoc if (substitutions.length == 0) return loc; - const offset = loc.fileOffset; + const i = getSubstitutionIndex(loc.fileOffset); + if (substitutions[i].filename.length > 0) + loc.filename = substitutions[i].filename; + loc.linnum += substitutions[i].startLine; + return loc; + } + + /// Resolve an offset into this file to a filename + line + column + private SourceLoc getSourceLoc(uint offset) @nogc + { + const i = getLineIndex(offset); + const sl = SourceLoc(filename, cast(int) (i + startLine), cast(int) (1 + offset - lines[i]), offset); + return substitute(sl); + } + + private size_t getSubstitutionIndex(uint offset) @nogc + { size_t lo = 0; size_t hi = substitutions.length + -1; size_t mid = 0; @@ -389,12 +417,7 @@ struct BaseLoc if (substitutions[mid].startIndex <= offset) { if (mid == substitutions.length - 1 || substitutions[mid + 1].startIndex > offset) - { - if (substitutions[mid].filename.length > 0) - loc.filename = substitutions[mid].filename; - loc.linnum += substitutions[mid].startLine; - return loc; - } + return mid; lo = mid + 1; } @@ -406,14 +429,6 @@ struct BaseLoc assert(0); } - /// Resolve an offset into this file to a filename + line + column - private SourceLoc getSourceLoc(uint offset) @nogc - { - const i = getLineIndex(offset); - const sl = SourceLoc(filename, cast(int) (i + startLine), cast(int) (1 + offset - lines[i]), offset); - return substitute(sl); - } - /// Binary search the index in `this.lines` corresponding to `offset` private size_t getLineIndex(uint offset) @nogc { diff --git a/compiler/src/dmd/timetrace.d b/compiler/src/dmd/timetrace.d index a09d2756f8..2c9c89aee7 100644 --- a/compiler/src/dmd/timetrace.d +++ b/compiler/src/dmd/timetrace.d @@ -382,13 +382,14 @@ private struct TimeTraceProfiler void writeLocation(Loc loc) { - if (loc.filename()) + SourceLoc sl = SourceLoc(loc); + if (sl.filename.length > 0) { - writeEscapeJSONString(buf, loc.filename().toDString()); - if (loc.linnum()) + writeEscapeJSONString(buf, sl.filename); + if (sl.line) { buf.writeByte(':'); - buf.print(loc.linnum()); + buf.print(sl.line); } } else