Skip to content

Commit f0aef77

Browse files
committed
WIP cache
1 parent d88274f commit f0aef77

1 file changed

Lines changed: 50 additions & 4 deletions

File tree

lib/errorlogger.cpp

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@
3636
#include <cstring>
3737
#include <fstream>
3838
#include <iomanip>
39-
#include <numeric>
39+
#include <ios>
40+
#include <list>
4041
#include <sstream>
4142
#include <string>
4243
#include <unordered_map>
4344
#include <utility>
45+
#include <vector>
4446

4547
#include "xml.h"
4648

@@ -638,13 +640,57 @@ std::string ErrorMessage::toXML() const
638640
return printer.CStr();
639641
}
640642

643+
// Byte offset of the start of each line, indexed by 1-based line number.
644+
// Building this once per file keeps readCode() cheap. Without it every call
645+
// rescans the file from the start, which is quadratic for a file that produces
646+
// many findings - vendor/generated headers routinely produce tens of thousands.
647+
static const std::vector<std::streamoff>* getLineOffsets(const std::string &file)
648+
{
649+
// thread_local: the thread executor calls this concurrently
650+
static thread_local std::list<std::pair<std::string, std::vector<std::streamoff>>> cache;
651+
652+
for (auto it = cache.begin(); it != cache.end(); ++it) {
653+
if (it->first == file) {
654+
// most recently used goes first
655+
cache.splice(cache.begin(), cache, it);
656+
return &cache.front().second;
657+
}
658+
}
659+
660+
// Binary mode so the offsets match what the seek in readCode() expects.
661+
// A trailing '\r' is stripped by the caller.
662+
std::ifstream fin(file, std::ios::binary);
663+
if (!fin.is_open())
664+
return nullptr;
665+
666+
std::vector<std::streamoff> offsets{0, 0}; // index 0 is unused, line 1 starts at offset 0
667+
std::array<char, 64 * 1024> buf;
668+
std::streamoff pos = 0;
669+
while (fin.read(buf.data(), buf.size()) || fin.gcount() > 0) {
670+
const std::streamsize n = fin.gcount();
671+
for (std::streamsize i = 0; i < n; ++i) {
672+
if (buf[i] == '\n')
673+
offsets.push_back(pos + i + 1);
674+
}
675+
pos += n;
676+
}
677+
678+
constexpr std::size_t maxCachedFiles = 4;
679+
if (cache.size() >= maxCachedFiles)
680+
cache.pop_back();
681+
cache.emplace_front(file, std::move(offsets));
682+
return &cache.front().second;
683+
}
684+
641685
// TODO: read info from some shared resource instead?
642686
static std::string readCode(const std::string &file, int linenr, int column, const char endl[])
643687
{
644-
std::ifstream fin(file);
645688
std::string line;
646-
while (linenr > 0 && std::getline(fin,line)) {
647-
linenr--;
689+
const std::vector<std::streamoff>* const offsets = getLineOffsets(file);
690+
if (offsets && linenr > 0 && linenr < static_cast<int>(offsets->size())) {
691+
std::ifstream fin(file, std::ios::binary);
692+
fin.seekg((*offsets)[linenr]);
693+
std::getline(fin, line);
648694
}
649695
const std::string::size_type endPos = line.find_last_not_of("\r\n\t ");
650696
if (endPos + 1 < line.size())

0 commit comments

Comments
 (0)