Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No pointer optimisation txn #142

Merged
merged 5 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions mdbx/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error
}
if err != nil {
*c.txn.key = C.MDBX_val{}
*c.txn.val = C.MDBX_val{}
c.txn.val = C.MDBX_val{}
return nil, nil, err
}

Expand All @@ -174,12 +174,12 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error
key = castToBytes(c.txn.key)
}
}
val = castToBytes(c.txn.val)
val = castToBytes(&c.txn.val)

// Clear transaction storage record storage area for future use and to
// prevent dangling references.
*c.txn.key = C.MDBX_val{}
*c.txn.val = C.MDBX_val{}
c.txn.val = C.MDBX_val{}

return key, val, nil
}
Expand All @@ -189,7 +189,7 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error
//
// See mdb_cursor_get.
func (c *Cursor) getVal0(op uint) error {
ret := C.mdbx_cursor_get(c._c, c.txn.key, c.txn.val, C.MDBX_cursor_op(op))
ret := C.mdbx_cursor_get(c._c, c.txn.key, &c.txn.val, C.MDBX_cursor_op(op))
return operrno("mdbx_cursor_get", ret)
}

Expand All @@ -206,7 +206,7 @@ func (c *Cursor) getVal1(setkey []byte, op uint) error {
c._c,
k, C.size_t(len(setkey)),
c.txn.key,
c.txn.val,
&c.txn.val,
C.MDBX_cursor_op(op),
)
return operrno("mdbx_cursor_get", ret)
Expand All @@ -220,7 +220,7 @@ func (c *Cursor) getVal01(setval []byte, op uint) error {
c._c,
v, C.size_t(len(setval)),
c.txn.key,
c.txn.val,
&c.txn.val,
C.MDBX_cursor_op(op),
)
return operrno("mdbx_cursor_get", ret)
Expand All @@ -242,7 +242,7 @@ func (c *Cursor) getVal2(setkey, setval []byte, op uint) error {
c._c,
k, C.size_t(len(setkey)),
v, C.size_t(len(setval)),
c.txn.key, c.txn.val,
c.txn.key, &c.txn.val,
C.MDBX_cursor_op(op),
)
return operrno("mdbx_cursor_get", ret)
Expand Down Expand Up @@ -280,16 +280,16 @@ func (c *Cursor) PutReserve(key []byte, n int, flags uint) ([]byte, error) {
ret := C.mdbxgo_cursor_put1(
c._c,
k, C.size_t(len(key)),
c.txn.val,
&c.txn.val,
C.MDBX_put_flags_t(flags|C.MDBX_RESERVE),
)
err := operrno("mdbx_cursor_put", ret)
if err != nil {
*c.txn.val = C.MDBX_val{}
c.txn.val = C.MDBX_val{}
return nil, err
}
b := castToBytes(c.txn.val)
*c.txn.val = C.MDBX_val{}
b := castToBytes(&c.txn.val)
c.txn.val = C.MDBX_val{}
return b, nil
}

Expand Down
20 changes: 9 additions & 11 deletions mdbx/txn.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Txn struct {
env *Env
_txn *C.MDBX_txn
key *C.MDBX_val
val *C.MDBX_val
val C.MDBX_val

errLogf func(format string, v ...interface{})

Expand Down Expand Up @@ -93,14 +93,12 @@ func beginTxn(env *Env, parent *Txn, flags uint) (*Txn, error) {
// In a write Txn we can use the shared, C-allocated key and value
// allocated by env, and freed when it is closed.
txn.key = env.ckey
txn.val = env.cval
} else {
// It is not easy to share C.MDBX_val values in this scenario unless
// there is a synchronized pool involved, which will increase
// overhead. Further, allocating these values with C will add
// overhead both here and when the values are freed.
txn.key = new(C.MDBX_val)
txn.val = new(C.MDBX_val)
}
} else {
// Because parent Txn objects cannot be used while a sub-Txn is active
Expand Down Expand Up @@ -593,15 +591,15 @@ func (txn *Txn) Get(dbi DBI, key []byte) ([]byte, error) {
ret := C.mdbxgo_get(
txn._txn, C.MDBX_dbi(dbi),
k, C.size_t(len(key)),
txn.val,
&txn.val,
)
err := operrno("mdbx_get", ret)
if err != nil {
*txn.val = C.MDBX_val{}
txn.val = C.MDBX_val{}
return nil, err
}
b := castToBytes(txn.val)
*txn.val = C.MDBX_val{}
b := castToBytes(&txn.val)
txn.val = C.MDBX_val{}
return b, nil
}

Expand Down Expand Up @@ -637,16 +635,16 @@ func (txn *Txn) PutReserve(dbi DBI, key []byte, n int, flags uint) ([]byte, erro
ret := C.mdbxgo_put1(
txn._txn, C.MDBX_dbi(dbi),
k, C.size_t(len(key)),
txn.val,
&txn.val,
C.MDBX_put_flags_t(flags|C.MDBX_RESERVE),
)
err := operrno("mdbx_put", ret)
if err != nil {
*txn.val = C.MDBX_val{}
txn.val = C.MDBX_val{}
return nil, err
}
b := castToBytes(txn.val)
*txn.val = C.MDBX_val{}
b := castToBytes(&txn.val)
txn.val = C.MDBX_val{}
return b, nil
}

Expand Down
Loading