mirror of
https://github.com/privatevoid-net/nix-super.git
synced 2024-11-24 23:06:16 +02:00
5d9fdab3de
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).
87 lines
2.1 KiB
C++
87 lines
2.1 KiB
C++
#pragma once
|
|
|
|
#include <cinttypes>
|
|
#include <numeric>
|
|
#include <vector>
|
|
|
|
#include "chunked-vector.hh"
|
|
#include "pos-idx.hh"
|
|
#include "position.hh"
|
|
#include "sync.hh"
|
|
|
|
namespace nix {
|
|
|
|
class PosTable
|
|
{
|
|
public:
|
|
class Origin
|
|
{
|
|
friend PosTable;
|
|
private:
|
|
uint32_t offset;
|
|
|
|
Origin(Pos::Origin origin, uint32_t offset, size_t size):
|
|
offset(offset), origin(origin), size(size)
|
|
{}
|
|
|
|
public:
|
|
const Pos::Origin origin;
|
|
const size_t size;
|
|
|
|
uint32_t offsetOf(PosIdx p) const
|
|
{
|
|
return p.id - 1 - offset;
|
|
}
|
|
};
|
|
|
|
private:
|
|
using Lines = std::vector<uint32_t>;
|
|
|
|
std::map<uint32_t, Origin> origins;
|
|
mutable Sync<std::map<uint32_t, Lines>> lines;
|
|
|
|
const Origin * resolve(PosIdx p) const
|
|
{
|
|
if (p.id == 0)
|
|
return nullptr;
|
|
|
|
const auto idx = p.id - 1;
|
|
/* 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
|
|
key is always 0. */
|
|
const auto pastOrigin = origins.upper_bound(idx);
|
|
return &std::prev(pastOrigin)->second;
|
|
}
|
|
|
|
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{};
|
|
}
|
|
};
|
|
|
|
}
|