aboutsummaryrefslogtreecommitdiff
path: root/src/libexpr/primops.cc
diff options
context:
space:
mode:
authoreldritch horrors <pennae@lix.systems>2024-01-29 06:19:23 +0100
committereldritch horrors <pennae@lix.systems>2024-03-18 16:12:46 +0100
commit86a1121d16f7eeb594955e39ff4e6fcd69f1b8c5 (patch)
tree4309fa2dd8699b05a57ba733c2e38b045f899c01 /src/libexpr/primops.cc
parentc39150e6bb4dd04bc60f1d9c13b7bc89d55948a8 (diff)
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). (cherry picked from commit 5d9fdab3de0ee17c71369ad05806b9ea06dfceda) Change-Id: Ie0b2430cb120c09097afa8c0101884d94f4bbf34
Diffstat (limited to 'src/libexpr/primops.cc')
-rw-r--r--src/libexpr/primops.cc48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/libexpr/primops.cc b/src/libexpr/primops.cc
index a5b539202..77601e457 100644
--- a/src/libexpr/primops.cc
+++ b/src/libexpr/primops.cc
@@ -2492,6 +2492,54 @@ static RegisterPrimOp primop_unsafeGetAttrPos(PrimOp {
.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. */
static void prim_hasAttr(EvalState & state, const PosIdx pos, Value * * args, Value & v)
{