Bug 1542106 - Cache the last (line number, offset => column) mapping returned and use it to optimize a subsequent lookup that's further along in the same line. r=arai

Differential Revision: https://phabricator.services.mozilla.com/D26270

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Jeff Walden 2019-04-05 22:56:12 +00:00
Родитель 5327fe7f3e
Коммит b2ffd2f110
2 изменённых файлов: 34 добавлений и 3 удалений

Просмотреть файл

@ -690,11 +690,26 @@ uint32_t GeneralTokenStreamChars<Unit, AnyCharsAccess>::computeColumn(
const TokenStreamAnyChars& anyChars = anyCharsAccess();
const Unit* begin =
this->sourceUnits.codeUnitPtrAt(anyChars.lineStart(lineToken));
uint32_t lineNumber = anyChars.srcCoords.lineNumber(lineToken);
uint32_t beginOffset;
uint32_t partialCols;
if (lineNumber == lastLineForColumn_ && lastOffsetForColumn_ <= offset) {
beginOffset = lastOffsetForColumn_;
partialCols = lastColumn_;
} else {
beginOffset = anyChars.lineStart(lineToken);
partialCols = 0;
}
const Unit* begin = this->sourceUnits.codeUnitPtrAt(beginOffset);
const Unit* end = this->sourceUnits.codeUnitPtrAt(offset);
auto partialCols = AssertedCast<uint32_t>(ComputeColumn(begin, end));
partialCols += AssertedCast<uint32_t>(ComputeColumn(begin, end));
lastLineForColumn_ = lineNumber;
lastOffsetForColumn_ = offset;
lastColumn_ = partialCols;
return (lineToken.isFirstLine() ? anyChars.options_.column : 0) + partialCols;
}

Просмотреть файл

@ -1941,6 +1941,22 @@ class GeneralTokenStreamChars : public SpecializedTokenStreamCharsBase<Unit> {
return static_cast<TokenStreamSpecific*>(this);
}
private:
// Computing accurate column numbers requires linearly iterating through all
// source units in the line to account for multi-unit code points; on long
// lines requiring many column computations, this becomes quadratic.
//
// However, because usually we need columns for advancing offsets through
// scripts, caching the last ((line number, offset) => relative column)
// mapping -- in similar manner to how |SourceUnits::lastIndex_| is used to
// cache (offset => line number) mappings -- lets us avoid re-iterating
// through the line prefix in most cases.
mutable uint32_t lastLineForColumn_ = UINT32_MAX;
mutable uint32_t lastOffsetForColumn_ = UINT32_MAX;
mutable uint32_t lastColumn_ = 0;
protected:
uint32_t computeColumn(LineToken lineToken, uint32_t offset) const;
void computeLineAndColumn(uint32_t offset, uint32_t* line,
uint32_t* column) const;