Browse Source

some optimizations

master
Martin Dørum 3 years ago
parent
commit
1c738d18ae
4 changed files with 50 additions and 30 deletions
  1. 40
    22
      src/BXParser.cc
  2. 7
    7
      src/BXParser.h
  3. 1
    0
      src/compdb.cc
  4. 2
    1
      src/sys.cc

+ 40
- 22
src/BXParser.cc View File

@@ -6,31 +6,31 @@
#include <errno.h>

int BXParser::get() {
if (dataidx_ < datalen_) {
return buf_[dataidx_++];
if (bufidx_ < buflen_) {
return buf_[bufidx_++];
}

dataidx_ = 0;
bufidx_ = 0;
stream_.read(buf_, sizeof(buf_));
datalen_ = stream_.gcount();
if (datalen_ == 0) {
buflen_ = stream_.gcount();
if (buflen_ == 0) {
return EOF;
}

return buf_[dataidx_++];
return buf_[bufidx_++];
}

int BXParser::peek() {
if (dataidx_ < datalen_) {
return buf_[dataidx_];
if (bufidx_ < buflen_) {
return buf_[bufidx_];
} else {
return stream_.peek();
}
}

int BXParser::peek2() {
if (dataidx_ + 1 < datalen_) {
return buf_[dataidx_ + 1];
if (bufidx_ + 1 < buflen_) {
return buf_[bufidx_ + 1];
} else {
stream_.get();
int ch = stream_.peek();
@@ -380,20 +380,35 @@ void BXParser::parseList(const BXVariables &vars, std::vector<std::string> &valu
}
}

BXWriter::~BXWriter() {
if (bufidx_ > 0) {
stream_.write(buf_, bufidx_);
}
}

void BXWriter::put(char ch) {
ch_ += 1;
stream_ << ch;
buf_[bufidx_++] = ch;

if (bufidx_ == sizeof(buf_)) {
stream_.write(buf_, sizeof(buf_));
bufidx_ = 0;
}
}

void BXWriter::put(const std::string &str) {
ch_ += str.size();
stream_ << str;
}
size_t w = sizeof(buf_) - bufidx_;
if (w > str.size()) {
w = str.size();
}

void BXWriter::newline() {
ch_ = 1;
line_ += 1;
stream_ << '\n';
memcpy(buf_ + bufidx_, str.c_str(), w);
if (str.size() - w > 0) {
stream_.write(buf_, bufidx_ + w);
stream_.write(str.c_str() + w, str.size() - w);
bufidx_ = 0;
} else {
bufidx_ += w;
}
}

void BXWriter::escape(const std::string &str) {
@@ -409,18 +424,21 @@ void BXWriter::escape(const std::string &str) {

void BXWriter::write(const BXVariables &vars) {
for (const auto &pair: vars) {
size_t chars = 0;
put(pair.first);
put(" :=");
for (auto &val: pair.second) {
if (ch_ >= 80) {
newline();
if (chars >= 80) {
put('\n');
put('\t');
chars = 0;
} else {
put(' ');
}

escape(val);
chars += val.size();
}
newline();
put('\n');
}
}

+ 7
- 7
src/BXParser.h View File

@@ -61,17 +61,17 @@ private:
int flags_;
int line_;
int ch_;
std::istream &stream_;

std::istream &stream_;
char buf_[1024];
size_t dataidx_ = 0;
size_t datalen_ = 0;
size_t bufidx_ = 0;
size_t buflen_ = 0;
};

class BXWriter {
public:
BXWriter(std::ostream &stream, int line = 1, int ch = 1):
line_(line), ch_(ch), stream_(stream) {}
BXWriter(std::ostream &stream): stream_(stream) {}
~BXWriter();

void write(const BXVariables &vars);

@@ -81,7 +81,7 @@ private:
void newline();
void escape(const std::string &str);

int line_;
int ch_;
std::ostream &stream_;
char buf_[1024];
size_t bufidx_ = 0;
};

+ 1
- 0
src/compdb.cc View File

@@ -4,6 +4,7 @@ namespace compdb {

std::string escape(const std::string &str) {
std::string out;
out.reserve(str.size() * 1.5);

// TODO: Handle unicode according to JSON spec
for (char ch: str) {

+ 2
- 1
src/sys.cc View File

@@ -79,7 +79,8 @@ void execute(const std::vector<std::string> &args, std::string *output, bool pri
// argv[0] should be interpreted as a shell command, because being able to run
// CC='gcc --sysroot=/blah' is used by some systems.
std::string command = std::string(args[0]) + " \"$@\"";
std::vector<const char *> argv;
static thread_local std::vector<const char *> argv;
argv.clear();
argv.push_back("/bin/sh"); // TODO: Use $SHELL?
argv.push_back("-c");
argv.push_back(command.c_str());

Loading…
Cancel
Save