rvagg/node-leveldown — pull request #70 by mcollina (2013-11-03 13:41:15+00:00)
Title: Persistent handles not needed for batch

I'm debugging for https://github.com/rvagg/node-levelup/issues/171 and I found out something weird: persistent handles might not be needed for batches. This reduces the memory footprint for batches.

In order to add a put to a batch we call `batch->put`, like this: https://github.com/rvagg/node-leveldown/blob/master/src/batch.cc#L125-L128.
This is the content of `batch->put` (
https://github.com/rvagg/node-leveldown/blob/master/deps/leveldb/leveldb-1.14.0/db/write_batch.cc#L98-L103):
```
void WriteBatch::Put(const Slice& key, const Slice& value) {
WriteBatchInternal::SetCount(this, WriteBatchInternal::Count(this) + 1);
rep_.push_back(static_cast(kTypeValue));
PutLengthPrefixedSlice(&rep_, key);
PutLengthPrefixedSlice(&rep_, value);
}
```

In `batch->put`, `rep_` is a `std::string`.

You can find `PutLengthPrefixedSlice` at: https://github.com/rvagg/node-leveldown/blob/master/deps/leveldb/leveldb-1.14.0/util/coding.cc#L98-L101, with this content:
```
void PutLengthPrefixedSlice(std::string* dst, const Slice& value) {
PutVarint32(dst, value.size());
dst->append(value.data(), value.size());
}
```

As far as I know C++, the `append` method copies the content: http://www.cplusplus.com/reference/string/string/append/.
So, using persistent handles are not needed in this case.

BTW, this didn't solve my segfault issue.

What do you think? Is this _very_ wrong and works because of some weird magic or not? Please, try it and make some strong objection against this.

I _just_ commented out 6 lines. So, some cleanup for Slice objects might be needed, plus some refactoring.