Skip to content

Commit

Permalink
Do not use delta-encoding across block boundaries
Browse files Browse the repository at this point in the history
Possible a fix for issue #4

Also: in block.h, rename line_bytes_ to block_size_
  • Loading branch information
pdewacht committed Apr 11, 2018
1 parent 4f2f1d6 commit 7ed0d6f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
21 changes: 12 additions & 9 deletions src/block.h
Expand Up @@ -24,35 +24,38 @@

class block {
public:
block(): line_bytes_(0) {
block(): block_size_(0) {
lines_.reserve(max_lines_per_block_);
}

bool empty() const {
return line_bytes_ == 0;
return block_size_ == 0;
}

bool full() const {
return lines_.size() == max_lines_per_block_;
}

void add_line(std::vector<uint8_t> &&line) {
assert(!line.empty());
assert(line_fits(line.size()));
line_bytes_ += line.size();
block_size_ += line.size();
lines_.emplace_back(line);
}

bool line_fits(unsigned size) {
return lines_.size() != max_lines_per_block_
&& line_bytes_ + size < max_block_size_;
bool line_fits(unsigned line_size) const {
return !full() && block_size_ + line_size < max_block_size_;
}

void flush(FILE *f) {
if (!empty()) {
fprintf(f, "%dw%c%c",
line_bytes_ + 2, 0,
block_size_ + 2, 0,
static_cast<int>(lines_.size()));
for (auto &line : lines_) {
fwrite(line.data(), 1, line.size(), f);
}
line_bytes_ = 0;
block_size_ = 0;
lines_.clear();
}
}
Expand All @@ -62,7 +65,7 @@ class block {
static const unsigned max_lines_per_block_ = 128;

std::vector<std::vector<uint8_t>> lines_;
int line_bytes_;
int block_size_;
};

#endif // BLOCK_H
31 changes: 19 additions & 12 deletions src/job.cc
Expand Up @@ -91,24 +91,31 @@ void job::encode_page(const page_params &page_params,
write_page_header();
}

fputs("\033*b1030m", out_);

std::vector<uint8_t> line(linesize);
std::vector<uint8_t> reference(linesize);
block block;

if (!nextline(line)) {
return;
}
block.add_line(encode_line(line));
std::swap(line, reference);

fputs("\033*b1030m", out_);

for (int i = 1; i < lines && nextline(line); ++i) {
std::vector<uint8_t> encoded = encode_line(line, reference);
if (!block.line_fits(encoded.size())) {
for (int i = 0; i < lines && nextline(line); ++i) {
if (block.empty()) {
// Beginning of a new block, do not apply delta-encoding.
block.add_line(encode_line(line));
} else {
// In the middle of a block, try delta-encoding.
std::vector<uint8_t> encoded = encode_line(line, reference);
if (block.line_fits(encoded.size())) {
// Ok, there's enough room for another line.
block.add_line(std::move(encoded));
} else {
// Oops, the line didn't fit. Start a new block.
block.flush(out_);
block.add_line(encode_line(line));
}
}
if (block.full()) {
block.flush(out_);
}
block.add_line(std::move(encoded));
std::swap(line, reference);
}

Expand Down

0 comments on commit 7ed0d6f

Please sign in to comment.