# Potentially faster DB::get by wrapping C++ string

**URL:** <https://discuss.dgraph.io/t/potentially-faster-db-get-by-wrapping-c-string/876>\
**Category:** Users\
**Created:** [September 15, 2016, 1:13pm UTC](https://discuss.dgraph.io/t/potentially-faster-db-get-by-wrapping-c-string/876 "2016-09-15T13:13:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jchiu](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/jchiu/32/301_2.png) [@jchiu](https://discuss.dgraph.io/u/jchiu)\
**Post date:** [September 15, 2016, 1:13pm UTC](https://discuss.dgraph.io/t/potentially-faster-db-get-by-wrapping-c-string/876/1 "2016-09-15T13:13:13Z")

</div>

Issue: The RocksDB C++ Get function takes in a `std::string` pointer. The current C API will copy that into a char array (not null-terminated C string). This copy seems redundant.

To remove this copying, one direction is to have a Go wrapper around a C++ string. Here is my attempt. It seems to work fine.

> <https://github.com/jchiu0/experimental/tree/master/wstring>
>
> //github.com/jchiu0/experimental/tree/master/wstring

Previously I was worried about the garbage collection not freeing the C++ string. The solution is to use `runtime.SetFinalizer`. I verified in a sample run that the C++ string does get deallocated properly when GC runs.

`SetFinalizer` does look hacky. And the user must not do anything to the read-only data returned from C++ string. If you all are ok with this, I will try integrating it into our RocksDB wrapper code.

---

<div class="post-metadata">

**Author:** ![mrjn](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.dgraph.io/mrjn/32/775_2.png) [@mrjn](https://discuss.dgraph.io/u/mrjn)\
**Post date:** [September 15, 2016, 2:55pm UTC](https://discuss.dgraph.io/t/potentially-faster-db-get-by-wrapping-c-string/876/2 "2016-09-15T14:55:07Z")

</div>

Looks like an interesting approach. Remind me to look deeper into this tomorrow. One thing to note is that the value returned from rocksdb needs to be copied over at least once. Because that value is only valid for the life or the callijg function, from what I understand.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex007/uploads/dgraph/original/2X/2/2b38fdece2abe5814f2e51bee69d1e67fc304b0a.png) [@system](https://discuss.dgraph.io/u/system)\
**Post date:** [November 28, 2017, 1:00am UTC](https://discuss.dgraph.io/t/potentially-faster-db-get-by-wrapping-c-string/876/3 "2017-11-28T01:00:32Z")

</div>

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