mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-22 22:16:16 +02:00
use byte indexed locations for PosIdx
we now keep not a table of all positions, but a table of all origins and their sizes. position indices are now direct pointers into the virtual concatenation of all parsed contents. this slightly reduces memory usage and time spent in the parser, at the cost of not being able to report positions if the total input size exceeds 4GiB. this limit is not unique to nix though, rustc and clang also limit their input to 4GiB (although at least clang refuses to process inputs that are larger, we will not). this new 4GiB limit probably will not cause any problems for quite a while, all of nixpkgs together is less than 100MiB in size and already needs over 700MiB of memory and multiple seconds just to parse. 4GiB worth of input will easily take multiple minutes and over 30GiB of memory without even evaluating anything. if problems *do* arise we can probably recover the old table-based system by adding some tracking to Pos::Origin (or increasing the size of PosIdx outright), but for time being this looks like more complexity than it's worth. since we now need to read the entire input again to determine the line/column of a position we'll make unsafeGetAttrPos slightly lazy: mostly the set it returns is only used to determine the file of origin of an attribute, not its exact location. the thunks do not add measurable runtime overhead. notably this change is necessary to allow changing the parser since apparently nothing supports nix's very idiosyncratic line ending choice of "anything goes", making it very hard to calculate line/column positions in the parser (while byte offsets are very easy).
This commit is contained in:
parent
855fd5a1bb
commit
5d9fdab3de
13 changed files with 150 additions and 85 deletions
|
@ -949,12 +949,11 @@ void EvalState::mkThunk_(Value & v, Expr * expr)
|
||||||
|
|
||||||
void EvalState::mkPos(Value & v, PosIdx p)
|
void EvalState::mkPos(Value & v, PosIdx p)
|
||||||
{
|
{
|
||||||
auto pos = positions[p];
|
auto origin = positions.originOf(p);
|
||||||
if (auto path = std::get_if<SourcePath>(&pos.origin)) {
|
if (auto path = std::get_if<SourcePath>(&origin)) {
|
||||||
auto attrs = buildBindings(3);
|
auto attrs = buildBindings(3);
|
||||||
attrs.alloc(sFile).mkString(path->path.abs());
|
attrs.alloc(sFile).mkString(path->path.abs());
|
||||||
attrs.alloc(sLine).mkInt(pos.line);
|
makePositionThunks(*this, p, attrs.alloc(sLine), attrs.alloc(sColumn));
|
||||||
attrs.alloc(sColumn).mkInt(pos.column);
|
|
||||||
v.mkAttrs(attrs);
|
v.mkAttrs(attrs);
|
||||||
} else
|
} else
|
||||||
v.mkNull();
|
v.mkNull();
|
||||||
|
|
|
@ -212,11 +212,10 @@ static Flake readFlake(
|
||||||
{
|
{
|
||||||
auto flakePath = rootDir / CanonPath(resolvedRef.subdir) / "flake.nix";
|
auto flakePath = rootDir / CanonPath(resolvedRef.subdir) / "flake.nix";
|
||||||
|
|
||||||
|
// NOTE evalFile forces vInfo to be an attrset because mustBeTrivial is true.
|
||||||
Value vInfo;
|
Value vInfo;
|
||||||
state.evalFile(flakePath, vInfo, true);
|
state.evalFile(flakePath, vInfo, true);
|
||||||
|
|
||||||
expectType(state, nAttrs, vInfo, state.positions.add(Pos::Origin(rootDir), 1, 1));
|
|
||||||
|
|
||||||
Flake flake {
|
Flake flake {
|
||||||
.originalRef = originalRef,
|
.originalRef = originalRef,
|
||||||
.resolvedRef = resolvedRef,
|
.resolvedRef = resolvedRef,
|
||||||
|
|
|
@ -33,33 +33,16 @@ namespace nix {
|
||||||
|
|
||||||
static void initLoc(YYLTYPE * loc)
|
static void initLoc(YYLTYPE * loc)
|
||||||
{
|
{
|
||||||
loc->first_line = loc->last_line = 1;
|
loc->first_line = loc->last_line = 0;
|
||||||
loc->first_column = loc->last_column = 1;
|
loc->first_column = loc->last_column = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
|
static void adjustLoc(YYLTYPE * loc, const char * s, size_t len)
|
||||||
{
|
{
|
||||||
loc->stash();
|
loc->stash();
|
||||||
|
|
||||||
loc->first_line = loc->last_line;
|
|
||||||
loc->first_column = loc->last_column;
|
loc->first_column = loc->last_column;
|
||||||
|
loc->last_column += len;
|
||||||
for (size_t i = 0; i < len; i++) {
|
|
||||||
switch (*s++) {
|
|
||||||
case '\r':
|
|
||||||
if (*s == '\n') { /* cr/lf */
|
|
||||||
i++;
|
|
||||||
s++;
|
|
||||||
}
|
|
||||||
/* fall through */
|
|
||||||
case '\n':
|
|
||||||
++loc->last_line;
|
|
||||||
loc->last_column = 1;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
++loc->last_column;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -583,6 +583,39 @@ std::string ExprLambda::showNamePos(const EvalState & state) const
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Position table. */
|
||||||
|
|
||||||
|
Pos PosTable::operator[](PosIdx p) const
|
||||||
|
{
|
||||||
|
auto origin = resolve(p);
|
||||||
|
if (!origin)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
const auto offset = origin->offsetOf(p);
|
||||||
|
|
||||||
|
Pos result{0, 0, origin->origin};
|
||||||
|
auto lines = this->lines.lock();
|
||||||
|
auto linesForInput = (*lines)[origin->offset];
|
||||||
|
|
||||||
|
if (linesForInput.empty()) {
|
||||||
|
auto source = result.getSource().value_or("");
|
||||||
|
const char * begin = source.data();
|
||||||
|
for (Pos::LinesIterator it(source), end; it != end; it++)
|
||||||
|
linesForInput.push_back(it->data() - begin);
|
||||||
|
if (linesForInput.empty())
|
||||||
|
linesForInput.push_back(0);
|
||||||
|
}
|
||||||
|
// as above: the first line starts at byte 0 and is always present
|
||||||
|
auto lineStartOffset = std::prev(
|
||||||
|
std::upper_bound(linesForInput.begin(), linesForInput.end(), offset));
|
||||||
|
|
||||||
|
result.line = 1 + (lineStartOffset - linesForInput.begin());
|
||||||
|
result.column = 1 + (offset - *lineStartOffset);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* Symbol table. */
|
/* Symbol table. */
|
||||||
|
|
||||||
size_t SymbolTable::totalSize() const
|
size_t SymbolTable::totalSize() const
|
||||||
|
|
|
@ -7,7 +7,6 @@
|
||||||
#include "value.hh"
|
#include "value.hh"
|
||||||
#include "symbol-table.hh"
|
#include "symbol-table.hh"
|
||||||
#include "error.hh"
|
#include "error.hh"
|
||||||
#include "chunked-vector.hh"
|
|
||||||
#include "position.hh"
|
#include "position.hh"
|
||||||
#include "eval-error.hh"
|
#include "eval-error.hh"
|
||||||
#include "pos-idx.hh"
|
#include "pos-idx.hh"
|
||||||
|
|
|
@ -24,20 +24,15 @@ struct ParserLocation
|
||||||
int last_line, last_column;
|
int last_line, last_column;
|
||||||
|
|
||||||
// backup to recover from yyless(0)
|
// backup to recover from yyless(0)
|
||||||
int stashed_first_line, stashed_first_column;
|
int stashed_first_column, stashed_last_column;
|
||||||
int stashed_last_line, stashed_last_column;
|
|
||||||
|
|
||||||
void stash() {
|
void stash() {
|
||||||
stashed_first_line = first_line;
|
|
||||||
stashed_first_column = first_column;
|
stashed_first_column = first_column;
|
||||||
stashed_last_line = last_line;
|
|
||||||
stashed_last_column = last_column;
|
stashed_last_column = last_column;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unstash() {
|
void unstash() {
|
||||||
first_line = stashed_first_line;
|
|
||||||
first_column = stashed_first_column;
|
first_column = stashed_first_column;
|
||||||
last_line = stashed_last_line;
|
|
||||||
last_column = stashed_last_column;
|
last_column = stashed_last_column;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -276,7 +271,7 @@ inline Expr * ParserState::stripIndentation(const PosIdx pos,
|
||||||
|
|
||||||
inline PosIdx ParserState::at(const ParserLocation & loc)
|
inline PosIdx ParserState::at(const ParserLocation & loc)
|
||||||
{
|
{
|
||||||
return positions.add(origin, loc.first_line, loc.first_column);
|
return positions.add(origin, loc.first_column);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -438,7 +438,7 @@ Expr * parseExprFromBuf(
|
||||||
.symbols = symbols,
|
.symbols = symbols,
|
||||||
.positions = positions,
|
.positions = positions,
|
||||||
.basePath = basePath,
|
.basePath = basePath,
|
||||||
.origin = {origin},
|
.origin = positions.addOrigin(origin, length),
|
||||||
.rootFS = rootFS,
|
.rootFS = rootFS,
|
||||||
.s = astSymbols,
|
.s = astSymbols,
|
||||||
};
|
};
|
||||||
|
|
|
@ -6,6 +6,7 @@ namespace nix {
|
||||||
|
|
||||||
class PosIdx
|
class PosIdx
|
||||||
{
|
{
|
||||||
|
friend struct LazyPosAcessors;
|
||||||
friend class PosTable;
|
friend class PosTable;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
|
@ -7,6 +7,7 @@
|
||||||
#include "chunked-vector.hh"
|
#include "chunked-vector.hh"
|
||||||
#include "pos-idx.hh"
|
#include "pos-idx.hh"
|
||||||
#include "position.hh"
|
#include "position.hh"
|
||||||
|
#include "sync.hh"
|
||||||
|
|
||||||
namespace nix {
|
namespace nix {
|
||||||
|
|
||||||
|
@ -17,66 +18,69 @@ public:
|
||||||
{
|
{
|
||||||
friend PosTable;
|
friend PosTable;
|
||||||
private:
|
private:
|
||||||
// must always be invalid by default, add() replaces this with the actual value.
|
uint32_t offset;
|
||||||
// subsequent add() calls use this index as a token to quickly check whether the
|
|
||||||
// current origins.back() can be reused or not.
|
|
||||||
mutable uint32_t idx = std::numeric_limits<uint32_t>::max();
|
|
||||||
|
|
||||||
// Used for searching in PosTable::[].
|
Origin(Pos::Origin origin, uint32_t offset, size_t size):
|
||||||
explicit Origin(uint32_t idx)
|
offset(offset), origin(origin), size(size)
|
||||||
: idx(idx)
|
{}
|
||||||
, origin{std::monostate()}
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
const Pos::Origin origin;
|
const Pos::Origin origin;
|
||||||
|
const size_t size;
|
||||||
|
|
||||||
Origin(Pos::Origin origin)
|
uint32_t offsetOf(PosIdx p) const
|
||||||
: origin(origin)
|
|
||||||
{
|
{
|
||||||
|
return p.id - 1 - offset;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Offset
|
|
||||||
{
|
|
||||||
uint32_t line, column;
|
|
||||||
};
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<Origin> origins;
|
using Lines = std::vector<uint32_t>;
|
||||||
ChunkedVector<Offset, 8192> offsets;
|
|
||||||
|
|
||||||
public:
|
std::map<uint32_t, Origin> origins;
|
||||||
PosTable()
|
mutable Sync<std::map<uint32_t, Lines>> lines;
|
||||||
: offsets(1024)
|
|
||||||
{
|
|
||||||
origins.reserve(1024);
|
|
||||||
}
|
|
||||||
|
|
||||||
PosIdx add(const Origin & origin, uint32_t line, uint32_t column)
|
const Origin * resolve(PosIdx p) const
|
||||||
{
|
{
|
||||||
const auto idx = offsets.add({line, column}).second;
|
if (p.id == 0)
|
||||||
if (origins.empty() || origins.back().idx != origin.idx) {
|
return nullptr;
|
||||||
origin.idx = idx;
|
|
||||||
origins.push_back(origin);
|
|
||||||
}
|
|
||||||
return PosIdx(idx + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
Pos operator[](PosIdx p) const
|
|
||||||
{
|
|
||||||
if (p.id == 0 || p.id > offsets.size())
|
|
||||||
return {};
|
|
||||||
const auto idx = p.id - 1;
|
const auto idx = p.id - 1;
|
||||||
/* we want the last key <= idx, so we'll take prev(first key > idx).
|
/* we want the last key <= idx, so we'll take prev(first key > idx).
|
||||||
this is guaranteed to never rewind origin.begin because the first
|
this is guaranteed to never rewind origin.begin because the first
|
||||||
key is always 0. */
|
key is always 0. */
|
||||||
const auto pastOrigin = std::upper_bound(
|
const auto pastOrigin = origins.upper_bound(idx);
|
||||||
origins.begin(), origins.end(), Origin(idx), [](const auto & a, const auto & b) { return a.idx < b.idx; });
|
return &std::prev(pastOrigin)->second;
|
||||||
const auto origin = *std::prev(pastOrigin);
|
}
|
||||||
const auto offset = offsets[idx];
|
|
||||||
return {offset.line, offset.column, origin.origin};
|
public:
|
||||||
|
Origin addOrigin(Pos::Origin origin, size_t size)
|
||||||
|
{
|
||||||
|
uint32_t offset = 0;
|
||||||
|
if (auto it = origins.rbegin(); it != origins.rend())
|
||||||
|
offset = it->first + it->second.size;
|
||||||
|
// +1 because all PosIdx are offset by 1 to begin with, and
|
||||||
|
// another +1 to ensure that all origins can point to EOF, eg
|
||||||
|
// on (invalid) empty inputs.
|
||||||
|
if (2 + offset + size < offset)
|
||||||
|
return Origin{origin, offset, 0};
|
||||||
|
return origins.emplace(offset, Origin{origin, offset, size}).first->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
PosIdx add(const Origin & origin, size_t offset)
|
||||||
|
{
|
||||||
|
if (offset > origin.size)
|
||||||
|
return PosIdx();
|
||||||
|
return PosIdx(1 + origin.offset + offset);
|
||||||
|
}
|
||||||
|
|
||||||
|
Pos operator[](PosIdx p) const;
|
||||||
|
|
||||||
|
Pos::Origin originOf(PosIdx p) const
|
||||||
|
{
|
||||||
|
if (auto o = resolve(p))
|
||||||
|
return o->origin;
|
||||||
|
return std::monostate{};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2524,6 +2524,54 @@ static RegisterPrimOp primop_unsafeGetAttrPos(PrimOp {
|
||||||
.fun = prim_unsafeGetAttrPos,
|
.fun = prim_unsafeGetAttrPos,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// access to exact position information (ie, line and colum numbers) is deferred
|
||||||
|
// due to the cost associated with calculating that information and how rarely
|
||||||
|
// it is used in practice. this is achieved by creating thunks to otherwise
|
||||||
|
// inaccessible primops that are not exposed as __op or under builtins to turn
|
||||||
|
// the internal PosIdx back into a line and column number, respectively. exposing
|
||||||
|
// these primops in any way would at best be not useful and at worst create wildly
|
||||||
|
// indeterministic eval results depending on parse order of files.
|
||||||
|
//
|
||||||
|
// in a simpler world this would instead be implemented as another kind of thunk,
|
||||||
|
// but each type of thunk has an associated runtime cost in the current evaluator.
|
||||||
|
// as with black holes this cost is too high to justify another thunk type to check
|
||||||
|
// for in the very hot path that is forceValue.
|
||||||
|
static struct LazyPosAcessors {
|
||||||
|
PrimOp primop_lineOfPos{
|
||||||
|
.arity = 1,
|
||||||
|
.fun = [] (EvalState & state, PosIdx pos, Value * * args, Value & v) {
|
||||||
|
v.mkInt(state.positions[PosIdx(args[0]->integer)].line);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
PrimOp primop_columnOfPos{
|
||||||
|
.arity = 1,
|
||||||
|
.fun = [] (EvalState & state, PosIdx pos, Value * * args, Value & v) {
|
||||||
|
v.mkInt(state.positions[PosIdx(args[0]->integer)].column);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
Value lineOfPos, columnOfPos;
|
||||||
|
|
||||||
|
LazyPosAcessors()
|
||||||
|
{
|
||||||
|
lineOfPos.mkPrimOp(&primop_lineOfPos);
|
||||||
|
columnOfPos.mkPrimOp(&primop_columnOfPos);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator()(EvalState & state, const PosIdx pos, Value & line, Value & column)
|
||||||
|
{
|
||||||
|
Value * posV = state.allocValue();
|
||||||
|
posV->mkInt(pos.id);
|
||||||
|
line.mkApp(&lineOfPos, posV);
|
||||||
|
column.mkApp(&columnOfPos, posV);
|
||||||
|
}
|
||||||
|
} makeLazyPosAccessors;
|
||||||
|
|
||||||
|
void makePositionThunks(EvalState & state, const PosIdx pos, Value & line, Value & column)
|
||||||
|
{
|
||||||
|
makeLazyPosAccessors(state, pos, line, column);
|
||||||
|
}
|
||||||
|
|
||||||
/* Dynamic version of the `?' operator. */
|
/* Dynamic version of the `?' operator. */
|
||||||
static void prim_hasAttr(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
static void prim_hasAttr(EvalState & state, const PosIdx pos, Value * * args, Value & v)
|
||||||
{
|
{
|
||||||
|
|
|
@ -51,4 +51,6 @@ void prim_importNative(EvalState & state, const PosIdx pos, Value * * args, Valu
|
||||||
*/
|
*/
|
||||||
void prim_exec(EvalState & state, const PosIdx pos, Value * * args, Value & v);
|
void prim_exec(EvalState & state, const PosIdx pos, Value * * args, Value & v);
|
||||||
|
|
||||||
|
void makePositionThunks(EvalState & state, const PosIdx pos, Value & line, Value & column);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -151,7 +151,7 @@ namespace nix {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(PrimOpTest, unsafeGetAttrPos) {
|
TEST_F(PrimOpTest, unsafeGetAttrPos) {
|
||||||
state.corepkgsFS->addFile(CanonPath("foo.nix"), "{ y = \"x\"; }");
|
state.corepkgsFS->addFile(CanonPath("foo.nix"), "\n\r\n\r{ y = \"x\"; }");
|
||||||
|
|
||||||
auto expr = "builtins.unsafeGetAttrPos \"y\" (import <nix/foo.nix>)";
|
auto expr = "builtins.unsafeGetAttrPos \"y\" (import <nix/foo.nix>)";
|
||||||
auto v = eval(expr);
|
auto v = eval(expr);
|
||||||
|
@ -165,10 +165,12 @@ namespace nix {
|
||||||
|
|
||||||
auto line = v.attrs->find(createSymbol("line"));
|
auto line = v.attrs->find(createSymbol("line"));
|
||||||
ASSERT_NE(line, nullptr);
|
ASSERT_NE(line, nullptr);
|
||||||
ASSERT_THAT(*line->value, IsIntEq(1));
|
state.forceValue(*line->value, noPos);
|
||||||
|
ASSERT_THAT(*line->value, IsIntEq(4));
|
||||||
|
|
||||||
auto column = v.attrs->find(createSymbol("column"));
|
auto column = v.attrs->find(createSymbol("column"));
|
||||||
ASSERT_NE(column, nullptr);
|
ASSERT_NE(column, nullptr);
|
||||||
|
state.forceValue(*column->value, noPos);
|
||||||
ASSERT_THAT(*column->value, IsIntEq(3));
|
ASSERT_THAT(*column->value, IsIntEq(3));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -110,8 +110,8 @@ TEST_F(ValuePrintingTests, vLambda)
|
||||||
.up = nullptr,
|
.up = nullptr,
|
||||||
.values = { }
|
.values = { }
|
||||||
};
|
};
|
||||||
PosTable::Origin origin((std::monostate()));
|
PosTable::Origin origin = state.positions.addOrigin(std::monostate(), 1);
|
||||||
auto posIdx = state.positions.add(origin, 1, 1);
|
auto posIdx = state.positions.add(origin, 0);
|
||||||
auto body = ExprInt(0);
|
auto body = ExprInt(0);
|
||||||
auto formals = Formals {};
|
auto formals = Formals {};
|
||||||
|
|
||||||
|
@ -558,8 +558,8 @@ TEST_F(ValuePrintingTests, ansiColorsLambda)
|
||||||
.up = nullptr,
|
.up = nullptr,
|
||||||
.values = { }
|
.values = { }
|
||||||
};
|
};
|
||||||
PosTable::Origin origin((std::monostate()));
|
PosTable::Origin origin = state.positions.addOrigin(std::monostate(), 1);
|
||||||
auto posIdx = state.positions.add(origin, 1, 1);
|
auto posIdx = state.positions.add(origin, 0);
|
||||||
auto body = ExprInt(0);
|
auto body = ExprInt(0);
|
||||||
auto formals = Formals {};
|
auto formals = Formals {};
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue