Skip to content

Commit 5ea9731

Browse files
committed
WIP cache
1 parent 8e9ead0 commit 5ea9731

1 file changed

Lines changed: 50 additions & 3 deletions

File tree

lib/errorlogger.cpp

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,13 @@
3535
#include <cstring>
3636
#include <fstream>
3737
#include <iomanip>
38+
#include <ios>
39+
#include <list>
3840
#include <sstream>
3941
#include <string>
4042
#include <unordered_map>
4143
#include <utility>
44+
#include <vector>
4245

4346
#include "xml.h"
4447

@@ -544,13 +547,57 @@ std::string ErrorMessage::toXML() const
544547
return printer.CStr();
545548
}
546549

550+
// Byte offset of the start of each line, indexed by 1-based line number.
551+
// Building this once per file keeps readCode() cheap. Without it every call
552+
// rescans the file from the start, which is quadratic for a file that produces
553+
// many findings - vendor/generated headers routinely produce tens of thousands.
554+
static const std::vector<std::streamoff>* getLineOffsets(const std::string &file)
555+
{
556+
// thread_local: the thread executor calls this concurrently
557+
static thread_local std::list<std::pair<std::string, std::vector<std::streamoff>>> cache;
558+
559+
for (auto it = cache.begin(); it != cache.end(); ++it) {
560+
if (it->first == file) {
561+
// most recently used goes first
562+
cache.splice(cache.begin(), cache, it);
563+
return &cache.front().second;
564+
}
565+
}
566+
567+
// Binary mode so the offsets match what the seek in readCode() expects.
568+
// A trailing '\r' is stripped by the caller.
569+
std::ifstream fin(file, std::ios::binary);
570+
if (!fin.is_open())
571+
return nullptr;
572+
573+
std::vector<std::streamoff> offsets{0, 0}; // index 0 is unused, line 1 starts at offset 0
574+
std::array<char, 64 * 1024> buf;
575+
std::streamoff pos = 0;
576+
while (fin.read(buf.data(), buf.size()) || fin.gcount() > 0) {
577+
const std::streamsize n = fin.gcount();
578+
for (std::streamsize i = 0; i < n; ++i) {
579+
if (buf[i] == '\n')
580+
offsets.push_back(pos + i + 1);
581+
}
582+
pos += n;
583+
}
584+
585+
constexpr std::size_t maxCachedFiles = 4;
586+
if (cache.size() >= maxCachedFiles)
587+
cache.pop_back();
588+
cache.emplace_front(file, std::move(offsets));
589+
return &cache.front().second;
590+
}
591+
547592
// TODO: read info from some shared resource instead?
548593
static std::string readCode(const std::string &file, int linenr, int column, const char endl[])
549594
{
550-
std::ifstream fin(file);
551595
std::string line;
552-
while (linenr > 0 && std::getline(fin,line)) {
553-
linenr--;
596+
const std::vector<std::streamoff>* const offsets = getLineOffsets(file);
597+
if (offsets && linenr > 0 && linenr < static_cast<int>(offsets->size())) {
598+
std::ifstream fin(file, std::ios::binary);
599+
fin.seekg((*offsets)[linenr]);
600+
std::getline(fin, line);
554601
}
555602
const std::string::size_type endPos = line.find_last_not_of("\r\n\t ");
556603
if (endPos + 1 < line.size())

0 commit comments

Comments
 (0)