<commit_before>#include <xzero/net/EndPointWriter.h>
#include <xzero/net/EndPoint.h>
#include <unistd.h>

namespace xzero {

EndPointWriter::EndPointWriter()
    : chunks_() {
}

EndPointWriter::~EndPointWriter() {
}

void EndPointWriter::write(const BufferRef& data) {
  chunks_.emplace_back(std::unique_ptr<Chunk>(new BufferChunk(data)));
}

void EndPointWriter::write(Buffer&& chunk) {
  chunks_.emplace_back(std::unique_ptr<Chunk>(
        new BufferChunk(std::forward<Buffer>(chunk))));
}

void EndPointWriter::write(FileRef&& chunk) {
  chunks_.emplace_back(std::unique_ptr<Chunk>(
        new FileChunk(std::forward<FileRef>(chunk))));
}

bool EndPointWriter::flush(EndPoint* sink) {
  while (!chunks_.empty()) {
    if (!chunks_.front()->transferTo(sink))
      return false;

    chunks_.pop_front();
  }

  return true;
}

// {{{ EndPointWriter::BufferChunk
bool EndPointWriter::BufferChunk::transferTo(EndPoint* sink) {
  size_t n = sink->flush(data_.ref(offset_));
  // printf("EndPointWriter.BufferChunk.transferTo(): %zu/%zu bytes written\n",
  //        n, data_.size() - offset_);

  offset_ += n;

  return offset_ == data_.size();
}
// }}}
// {{{ EndPointWriter::FileChunk
EndPointWriter::FileChunk::~FileChunk() {
}

bool EndPointWriter::FileChunk::transferTo(EndPoint* sink) {
  const size_t n = sink->flush(file_.handle(), file_.offset(), file_.size());
  // printf("EndPointWriter.FileChunk.transferTo(): %zu/%zu bytes written\n",
  //        n, file_.size());

  file_.setSize(file_.size() - n);
  file_.setOffset(file_.offset() + n);

  return file_.size() == 0;
}
// }}}<commit_msg>EndPointWriter: adds fast-path for BufferRef-writes<commit_after>#include <xzero/net/EndPointWriter.h>
#include <xzero/net/EndPoint.h>
#include <unistd.h>

namespace xzero {

EndPointWriter::EndPointWriter()
    : chunks_() {
}

EndPointWriter::~EndPointWriter() {
}

void EndPointWriter::write(const BufferRef& data) {
  chunks_.emplace_back(std::unique_ptr<Chunk>(new BufferRefChunk(data)));
}

void EndPointWriter::write(Buffer&& chunk) {
  chunks_.emplace_back(std::unique_ptr<Chunk>(
        new BufferChunk(std::forward<Buffer>(chunk))));
}

void EndPointWriter::write(FileRef&& chunk) {
  chunks_.emplace_back(std::unique_ptr<Chunk>(
        new FileChunk(std::forward<FileRef>(chunk))));
}

bool EndPointWriter::flush(EndPoint* sink) {
  while (!chunks_.empty()) {
    if (!chunks_.front()->transferTo(sink))
      return false;

    chunks_.pop_front();
  }

  return true;
}

// {{{ EndPointWriter::BufferChunk
bool EndPointWriter::BufferChunk::transferTo(EndPoint* sink) {
  size_t n = sink->flush(data_.ref(offset_));
  // printf("EndPointWriter.BufferChunk.transferTo(): %zu/%zu bytes written\n",
  //        n, data_.size() - offset_);

  offset_ += n;

  return offset_ == data_.size();
}
// }}}
// {{{ EndPointWriter::BufferRefChunk
bool EndPointWriter::BufferRefChunk::transferTo(EndPoint* sink) {
  size_t n = sink->flush(data_.ref(offset_));
  // printf("BufferRefChunk.transferTo: %zu bytes\n", n);

  offset_ += n;
  return offset_ == data_.size();
}
// }}}
// {{{ EndPointWriter::FileChunk
EndPointWriter::FileChunk::~FileChunk() {
}

bool EndPointWriter::FileChunk::transferTo(EndPoint* sink) {
  const size_t n = sink->flush(file_.handle(), file_.offset(), file_.size());
  // printf("EndPointWriter.FileChunk.transferTo(): %zu/%zu bytes written\n",
  //        n, file_.size());

  file_.setSize(file_.size() - n);
  file_.setOffset(file_.offset() + n);

  return file_.size() == 0;
}
// }}}
{'fraction_non_alphanumeric': 0.1816633550950894, 'fraction_numerical': 0.0005676979846721544, 'mean_word_length': 4.824793388429752, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4771925', 'n_tokens_mistral': 1197, 'n_tokens_neox': 1193, 'n_words': 188}