mirror of
https://github.com/NixOS/nix
synced 2025-07-05 12:21:48 +02:00
Merge pull request #12645 from xokdvium/debugger-perf
Make debugger significantly faster
This commit is contained in:
commit
1bff2aeec0
14 changed files with 123 additions and 73 deletions
|
@ -50,6 +50,14 @@ struct LinesOfCode {
|
|||
std::optional<std::string> nextLineOfCode;
|
||||
};
|
||||
|
||||
/* NOTE: position.hh recursively depends on source-path.hh -> source-accessor.hh
|
||||
-> hash.hh -> config.hh -> experimental-features.hh -> error.hh -> Pos.
|
||||
There are other such cycles.
|
||||
Thus, Pos has to be an incomplete type in this header. But since ErrorInfo/Trace
|
||||
have to refer to Pos, they have to use pointer indirection via std::shared_ptr
|
||||
to break the recursive header dependency.
|
||||
FIXME: Untangle this mess. Should there be AbstractPos as there used to be before
|
||||
4feb7d9f71? */
|
||||
struct Pos;
|
||||
|
||||
void printCodeLines(std::ostream & out,
|
||||
|
|
|
@ -155,6 +155,7 @@ sources = files(
|
|||
'memory-source-accessor.cc',
|
||||
'mounted-source-accessor.cc',
|
||||
'position.cc',
|
||||
'pos-table.cc',
|
||||
'posix-source-accessor.cc',
|
||||
'references.cc',
|
||||
'serialise.cc',
|
||||
|
@ -225,6 +226,8 @@ headers = [config_h] + files(
|
|||
'muxable-pipe.hh',
|
||||
'os-string.hh',
|
||||
'pool.hh',
|
||||
'pos-idx.hh',
|
||||
'pos-table.hh',
|
||||
'position.hh',
|
||||
'posix-source-accessor.hh',
|
||||
'processes.hh',
|
||||
|
|
65
src/libutil/pos-idx.hh
Normal file
65
src/libutil/pos-idx.hh
Normal file
|
@ -0,0 +1,65 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <cinttypes>
|
||||
#include <functional>
|
||||
|
||||
namespace nix {
|
||||
|
||||
class PosIdx
|
||||
{
|
||||
friend struct LazyPosAcessors;
|
||||
friend class PosTable;
|
||||
friend class std::hash<PosIdx>;
|
||||
|
||||
private:
|
||||
uint32_t id;
|
||||
|
||||
explicit PosIdx(uint32_t id)
|
||||
: id(id)
|
||||
{
|
||||
}
|
||||
|
||||
public:
|
||||
PosIdx()
|
||||
: id(0)
|
||||
{
|
||||
}
|
||||
|
||||
explicit operator bool() const
|
||||
{
|
||||
return id > 0;
|
||||
}
|
||||
|
||||
auto operator<=>(const PosIdx other) const
|
||||
{
|
||||
return id <=> other.id;
|
||||
}
|
||||
|
||||
bool operator==(const PosIdx other) const
|
||||
{
|
||||
return id == other.id;
|
||||
}
|
||||
|
||||
size_t hash() const noexcept
|
||||
{
|
||||
return std::hash<uint32_t>{}(id);
|
||||
}
|
||||
};
|
||||
|
||||
inline PosIdx noPos = {};
|
||||
|
||||
}
|
||||
|
||||
namespace std {
|
||||
|
||||
template<>
|
||||
struct hash<nix::PosIdx>
|
||||
{
|
||||
std::size_t operator()(nix::PosIdx pos) const noexcept
|
||||
{
|
||||
return pos.hash();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace std
|
37
src/libutil/pos-table.cc
Normal file
37
src/libutil/pos-table.cc
Normal file
|
@ -0,0 +1,37 @@
|
|||
#include "pos-table.hh"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
namespace nix {
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
}
|
100
src/libutil/pos-table.hh
Normal file
100
src/libutil/pos-table.hh
Normal file
|
@ -0,0 +1,100 @@
|
|||
#pragma once
|
||||
///@file
|
||||
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a byte-offset PosIdx into a Pos with line/column information.
|
||||
*
|
||||
* @param p Byte offset into the virtual concatenation of all parsed contents
|
||||
* @return Position
|
||||
*
|
||||
* @warning Very expensive to call, as this has to read the entire source
|
||||
* into memory each time. Call this only if absolutely necessary. Prefer
|
||||
* to keep PosIdx around instead of needlessly converting it into Pos by
|
||||
* using this lookup method.
|
||||
*/
|
||||
Pos operator[](PosIdx p) const;
|
||||
|
||||
Pos::Origin originOf(PosIdx p) const
|
||||
{
|
||||
if (auto o = resolve(p))
|
||||
return o->origin;
|
||||
return std::monostate{};
|
||||
}
|
||||
};
|
||||
|
||||
}
|
|
@ -66,6 +66,13 @@ std::optional<std::string> Pos::getSource() const
|
|||
}, origin);
|
||||
}
|
||||
|
||||
std::optional<SourcePath> Pos::getSourcePath() const
|
||||
{
|
||||
if (auto * path = std::get_if<SourcePath>(&origin))
|
||||
return *path;
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
void Pos::print(std::ostream & out, bool showOrigin) const
|
||||
{
|
||||
if (showOrigin) {
|
||||
|
|
|
@ -50,6 +50,7 @@ struct Pos
|
|||
|
||||
explicit operator bool() const { return line > 0; }
|
||||
|
||||
/* TODO: Why std::shared_ptr<Pos> and not std::shared_ptr<const Pos>? */
|
||||
operator std::shared_ptr<Pos>() const;
|
||||
|
||||
/**
|
||||
|
@ -69,9 +70,7 @@ struct Pos
|
|||
/**
|
||||
* Get the SourcePath, if the source was loaded from a file.
|
||||
*/
|
||||
std::optional<SourcePath> getSourcePath() const {
|
||||
return *std::get_if<SourcePath>(&origin);
|
||||
}
|
||||
std::optional<SourcePath> getSourcePath() const;
|
||||
|
||||
struct LinesIterator {
|
||||
using difference_type = size_t;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue