Optimize access to Loc.filename

This commit is contained in:
Dennis Korpel 2025-02-13 15:28:34 +01:00 committed by The Dlang Bot
parent 842dd20ea5
commit d1df9a6552
3 changed files with 40 additions and 23 deletions

View file

@ -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

View file

@ -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
{

View file

@ -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