Improved performance of determining line numbers during VM code generation

Change-Id: Ic1723ce7eb760d6b64ca5f694d94283334918899
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/533805
Reviewed-by: Brian Osman <brianosman@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
Commit-Queue: John Stiles <johnstiles@google.com>
This commit is contained in:
Ethan Nicholas 2022-04-26 16:06:54 -04:00 committed by SkCQ
parent 6bcfd35fda
commit b642184a32
2 changed files with 27 additions and 1 deletions

View File

@ -63,6 +63,7 @@
#include <algorithm>
#include <cstddef>
#include <functional>
#include <iterator>
#include <string_view>
#include <utility>
@ -330,6 +331,8 @@ private:
const std::function <Value(skvm::F32 x, skvm::F32 y)>& float_comp,
const std::function <Value(skvm::I32 x, skvm::I32 y)>& int_comp);
void determineLineOffsets();
//
// Global state for the lifetime of the generator:
//
@ -338,6 +341,9 @@ private:
SkVMDebugTrace* fDebugTrace;
int fTraceHookID = -1;
SkVMCallbacks* fCallbacks;
// contains the position of each newline in the source, plus a zero at the beginning and the
// total source length at the end as sentinels
std::vector<int> fLineOffsets;
struct Slot {
skvm::Val val;
@ -415,6 +421,7 @@ void SkVMGenerator::writeProgram(SkSpan<skvm::Val> uniforms,
const FunctionDefinition& function,
SkSpan<skvm::Val> arguments,
SkSpan<skvm::Val> outReturn) {
this->determineLineOffsets();
fConditionMask = fLoopMask = fBuilder->splat(0xffff'ffff);
this->setupGlobals(uniforms, device);
@ -427,6 +434,17 @@ void SkVMGenerator::writeProgram(SkSpan<skvm::Val> uniforms,
}
}
void SkVMGenerator::determineLineOffsets() {
SkASSERT(fLineOffsets.empty());
fLineOffsets.push_back(0);
for (size_t i = 0; i < fProgram.fSource->length(); ++i) {
if ((*fProgram.fSource)[i] == '\n') {
fLineOffsets.push_back(i);
}
}
fLineOffsets.push_back(fProgram.fSource->length());
}
void SkVMGenerator::setupGlobals(SkSpan<skvm::Val> uniforms, skvm::Coord device) {
if (fDebugTrace) {
// Copy the program source into the debug info so that it will be written in the trace file.
@ -691,7 +709,12 @@ size_t SkVMGenerator::createSlot(const std::string& name,
// TODO(skia:13058): remove this and track positions directly
int SkVMGenerator::getLine(Position pos) {
if (pos.valid()) {
return pos.line(*fProgram.fSource);
// Binary search within fLineOffets to find the line.
SkASSERT(fLineOffsets.size() >= 2);
SkASSERT(fLineOffsets[0] == 0);
SkASSERT(fLineOffsets.back() == (int)fProgram.fSource->length());
return std::distance(fLineOffsets.begin(), std::upper_bound(fLineOffsets.begin(),
fLineOffsets.end(), pos.startOffset()));
} else {
return -1;
}

View File

@ -6,9 +6,12 @@
{ include: ["<__algorithm/remove_if.h>", "private", "<algorithm>", "public"] },
{ include: ["<__algorithm/sort.h>", "private", "<algorithm>", "public"] },
{ include: ["<__algorithm/stable_sort.h>", "private", "<algorithm>", "public"] },
{ include: ["<__algorithm/upper_bound.h>", "private", "<algorithm>", "public"] },
{ include: ["<__functional/function.h>", "private", "<functional>", "public"] },
{ include: ["<__iterator/distance.h>", "private", "<iterator>", "public"] },
{ include: ["<__locale>", "private", "<locale>", "public"] },
{ include: ["<__memory/shared_ptr.h>", "private", "<memory>", "public"] },