Does cockroachDB copy?

The answer is most likely Yes.

Navigating through the C++ code, I eventually end up at the DBGetter object:

For convenience,

// DBGetter is an implementation of the Getter interface which
// retrieves the value for the supplied key from a rocksdb::DB.
struct DBGetter : public Getter {
  rocksdb::DB *const rep;
  rocksdb::ReadOptions const options;
  std::string const key;

  DBGetter(rocksdb::DB *const r, rocksdb::ReadOptions opts, std::string &&k)
      : rep(r),
        options(opts),
        key(std::move(k)) {
  }

  virtual DBStatus Get(DBString* value) {
    std::string tmp;
    rocksdb::Status s = rep->Get(options, key, &tmp);
    if (!s.ok()) {
      if (s.IsNotFound()) {
        // This mirrors the logic in rocksdb_get(). It doesn't seem like
        // a good idea, but some code in engine_test.go depends on it.
        value->data = NULL;
        value->len = 0;
        return kSuccess;
      }
      return ToDBStatus(s);
    }
    *value = ToDBString(tmp);
    return kSuccess;
  }
};

Look at the Get method. It declares a tmp C++ string. This should tell you that most likely there’s a copy involved, unless there is some C++ modern magic with std::move or right value reference, which is not that popular.

Finally, take a look at ToDBString. Here it is:

DBString ToDBString(const rocksdb::Slice& s) {
  DBString result;
  result.len = s.size();
  result.data = static_cast<char*>(malloc(result.len));
  memcpy(result.data, s.data(), s.size());
  return result;
}

There you go, a malloc and a memcpy.

Just to add on: Cockroach did not use the C API of RocksDB, but they still do a copy while trying to mirror the C API.

1 Like

They also use ToDBSlice, which seems to avoid a copy.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.