Skip to content

Commit

Permalink
Removed memory allocations when calling C.go_* functions
Browse files Browse the repository at this point in the history
  • Loading branch information
valyala committed Apr 7, 2017
1 parent 82bc72d commit 32d9f38
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 23 deletions.
20 changes: 8 additions & 12 deletions bindings/go/ybc/ybc.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ func (sc *SimpleCache) Set(key, value []byte, ttl time.Duration) error {
initKey(&k, key)
var v C.struct_ybc_value
initValue(&v, value, ttl)
if C.go_simple_set(sc.cache.ctx(), k, v) == 0 {
if C.go_simple_set(sc.cache.ctx(), k.ptr, k.size, v.ptr, v.size, v.ttl) == 0 {
return ErrNoSpace
}
return nil
Expand All @@ -389,11 +389,7 @@ func (sc *SimpleCache) Get(key []byte) (value []byte, err error) {
initKey(&k, key)

value = make([]byte, sc.maxItemSize)
v := C.struct_ybc_value{
ptr: bufPtr(value),
size: C.size_t(len(value)),
}
rv := C.go_simple_get(sc.cache.ctx(), k, v)
rv := C.go_simple_get(sc.cache.ctx(), k.ptr, k.size, bufPtr(value), C.size_t(len(value)))
if rv.result != 1 {
value = nil
err = ErrCacheMiss
Expand Down Expand Up @@ -454,7 +450,7 @@ func (cache *Cache) Set(key []byte, value []byte, ttl time.Duration) error {
initKey(&k, key)
var v C.struct_ybc_value
initValue(&v, value, ttl)
if C.go_item_set(cache.ctx(), k, v) == 0 {
if C.go_item_set(cache.ctx(), k.ptr, k.size, v.ptr, v.size, v.ttl) == 0 {
return ErrNoSpace
}
return nil
Expand Down Expand Up @@ -527,7 +523,7 @@ func (cache *Cache) Delete(key []byte) bool {
cache.dg.CheckLive()
var k C.struct_ybc_key
initKey(&k, key)
return C.go_item_remove(cache.ctx(), k) != C.int(0)
return C.go_item_remove(cache.ctx(), k.ptr, k.size) != C.int(0)
}

// The same as Cache.Set(), but additionally returns item object associated
Expand All @@ -541,7 +537,7 @@ func (cache *Cache) SetItem(key []byte, value []byte, ttl time.Duration) (item *
initKey(&k, key)
var v C.struct_ybc_value
initValue(&v, value, ttl)
rv := C.go_set_item_and_value(cache.ctx(), item.ctx(), k, v)
rv := C.go_set_item_and_value(cache.ctx(), item.ctx(), k.ptr, k.size, v.ptr, v.size, v.ttl)
if rv.result == 0 {
releaseItem(item)
err = ErrNoSpace
Expand All @@ -565,7 +561,7 @@ func (cache *Cache) GetItem(key []byte) (item *Item, err error) {
item = acquireItem()
var k C.struct_ybc_key
initKey(&k, key)
rv := C.go_get_item_and_value(cache.ctx(), item.ctx(), k)
rv := C.go_get_item_and_value(cache.ctx(), item.ctx(), k.ptr, k.size)
if rv.result == 0 {
releaseItem(item)
err = ErrCacheMiss
Expand Down Expand Up @@ -614,7 +610,7 @@ func (cache *Cache) GetDeAsyncItem(key []byte, graceDuration time.Duration) (ite
var k C.struct_ybc_key
initKey(&k, key)
mGraceTtl := C.uint64_t(graceDuration / time.Millisecond)
rv := C.go_get_item_and_value_de_async(cache.ctx(), item.ctx(), k, mGraceTtl)
rv := C.go_get_item_and_value_de_async(cache.ctx(), item.ctx(), k.ptr, k.size, mGraceTtl)
switch rv.status {
case C.YBC_DE_WOULDBLOCK:
releaseItem(item)
Expand Down Expand Up @@ -648,7 +644,7 @@ func (cache *Cache) NewSetTxn(key []byte, valueSize int, ttl time.Duration) (txn
txn = acquireSetTxn()
var k C.struct_ybc_key
initKey(&k, key)
if C.go_set_txn_begin(cache.ctx(), txn.ctx(), k, C.size_t(valueSize), C.uint64_t(ttl/time.Millisecond)) == 0 {
if C.go_set_txn_begin(cache.ctx(), txn.ctx(), k.ptr, k.size, C.size_t(valueSize), C.uint64_t(ttl/time.Millisecond)) == 0 {
err = ErrNoSpace
return
}
Expand Down
85 changes: 74 additions & 11 deletions bindings/go/ybc/ybc_go_glue.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
#include "ybc.h"

static int go_item_set(struct ybc *cache, const struct ybc_key key,
const struct ybc_value value)
static int go_item_set(struct ybc *cache,
const void *const key_ptr, const size_t key_size,
void *const value_ptr, const size_t value_size, const uint64_t value_ttl)
{
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};
const struct ybc_value value = {
.ptr = value_ptr,
.size = value_size,
.ttl = value_ttl,
};

return ybc_item_set(cache, &key, &value);
}

static int go_item_remove(struct ybc *cache, const struct ybc_key key)
static int go_item_remove(struct ybc *cache,
const void *const key_ptr, const size_t key_size)
{
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};

return ybc_item_remove(cache, &key);
}

static int go_simple_set(struct ybc *cache, const struct ybc_key key,
const struct ybc_value value)
static int go_simple_set(struct ybc *cache,
const void *const key_ptr, const size_t key_size,
void *const value_ptr, const size_t value_size, const uint64_t value_ttl)
{
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};
const struct ybc_value value = {
.ptr = value_ptr,
.size = value_size,
.ttl = value_ttl,
};

return ybc_simple_set(cache, &key, &value);
}

Expand All @@ -23,9 +51,18 @@ struct go_ret_size {
};

static struct go_ret_size go_simple_get(struct ybc *const cache,
const struct ybc_key key, struct ybc_value value)
const void *const key_ptr, const size_t key_size,
void *const value_ptr, const size_t value_size)
{
struct go_ret_size rv;
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};
struct ybc_value value = {
.ptr = value_ptr,
.size = value_size,
};

rv.result = ybc_simple_get(cache, &key, &value);
rv.size = value.size;
Expand All @@ -35,8 +72,14 @@ static struct go_ret_size go_simple_get(struct ybc *const cache,

static int go_set_txn_begin(struct ybc *const cache,
struct ybc_set_txn *const txn,
const struct ybc_key key, const size_t value_size, const uint64_t ttl)
const void *const key_ptr, const size_t key_size,
const size_t value_size, const uint64_t ttl)
{
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};

return ybc_set_txn_begin(cache, txn, &key, value_size, ttl);
}

Expand All @@ -46,9 +89,14 @@ struct go_ret_value {
};

static struct go_ret_value go_get_item_and_value(struct ybc *const cache,
struct ybc_item *const item, const struct ybc_key key)
struct ybc_item *const item,
const void *const key_ptr, const size_t key_size)
{
struct go_ret_value rv;
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};

rv.result = ybc_item_get(cache, item, &key);
if (rv.result != 0) {
Expand All @@ -65,9 +113,14 @@ struct go_ret_de_value {

static struct go_ret_de_value go_get_item_and_value_de_async(
struct ybc *const cache, struct ybc_item *const item,
const struct ybc_key key, const uint64_t grace_ttl)
const void *const key_ptr, const size_t key_size,
const uint64_t grace_ttl)
{
struct go_ret_de_value rv;
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};

rv.status = ybc_item_get_de_async(cache, item, &key, grace_ttl);
if (rv.status == YBC_DE_SUCCESS) {
Expand All @@ -78,10 +131,20 @@ static struct go_ret_de_value go_get_item_and_value_de_async(
}

static struct go_ret_value go_set_item_and_value(struct ybc *const cache,
struct ybc_item *const item, const struct ybc_key key,
const struct ybc_value value)
struct ybc_item *const item,
const void *const key_ptr, const size_t key_size,
void *const value_ptr, const size_t value_size, const uint64_t value_ttl)
{
struct go_ret_value rv;
const struct ybc_key key = {
.ptr = key_ptr,
.size = key_size,
};
const struct ybc_value value = {
.ptr = value_ptr,
.size = value_size,
.ttl = value_ttl,
};

rv.result = ybc_item_set_item(cache, item, &key, &value);
if (rv.result != 0) {
Expand Down

0 comments on commit 32d9f38

Please sign in to comment.