From 8446bb6653d32c531ab68b4776b4f31ca19e1e8d Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Wed, 19 Jun 2024 10:15:31 +0700 Subject: [PATCH] remove unused pointers in `env` (#146) * save * save --- mdbx/cursor.go | 6 ++---- mdbx/env.go | 25 +++++++------------------ 2 files changed, 9 insertions(+), 22 deletions(-) diff --git a/mdbx/cursor.go b/mdbx/cursor.go index 035738a..2fb29f1 100644 --- a/mdbx/cursor.go +++ b/mdbx/cursor.go @@ -153,8 +153,7 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error err = c.getValEmpty(op) } if err != nil { - c.txn.key = C.MDBX_val{} - c.txn.val = C.MDBX_val{} + c.txn.key, c.txn.val = C.MDBX_val{}, C.MDBX_val{} return nil, nil, err } @@ -173,8 +172,7 @@ func (c *Cursor) Get(setkey, setval []byte, op uint) (key, val []byte, err error // 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.key, c.txn.val = C.MDBX_val{}, C.MDBX_val{} return key, val, nil } diff --git a/mdbx/env.go b/mdbx/env.go index b0d1477..16d27de 100644 --- a/mdbx/env.go +++ b/mdbx/env.go @@ -123,8 +123,6 @@ type DBI C.MDBX_dbi // See MDBX_env. type Env struct { _env *C.MDBX_env - ckey *C.MDBX_val - cval *C.MDBX_val // closeLock is used to allow the Txn finalizer to check if the Env has // been closed, so that it may know if it must abort. @@ -140,8 +138,6 @@ func NewEnv() (*Env, error) { if ret != success { return nil, operrno("mdbx_env_create", ret) } - env.ckey = (*C.MDBX_val)(C.malloc(C.size_t(unsafe.Sizeof(C.MDBX_val{})))) - env.cval = (*C.MDBX_val)(C.malloc(C.size_t(unsafe.Sizeof(C.MDBX_val{})))) return env, nil } @@ -234,11 +230,6 @@ func (env *Env) Close() { C.mdbx_env_close(env._env) env._env = nil env.closeLock.Unlock() - - C.free(unsafe.Pointer(env.ckey)) - C.free(unsafe.Pointer(env.cval)) - env.ckey = nil - env.cval = nil } // CopyFD copies env to the the file descriptor fd. @@ -534,7 +525,7 @@ func (env *Env) BeginTxn(parent *Txn, flags uint) (*Txn, error) { // // See mdbx_txn_begin. func (env *Env) RunTxn(flags uint, fn TxnOp) error { - return env.run(false, flags, fn) + return env.run(flags, fn) } // View creates a readonly transaction with a consistent view of the @@ -548,7 +539,7 @@ func (env *Env) RunTxn(flags uint, fn TxnOp) error { // Any call to Commit, Abort, Reset or Renew on a Txn created by View will // panic. func (env *Env) View(fn TxnOp) error { - return env.run(false, Readonly, fn) + return env.run(Readonly, fn) } // Update calls fn with a writable transaction. Update commits the transaction @@ -575,7 +566,9 @@ func (env *Env) View(fn TxnOp) error { // Any call to Commit, Abort, Reset or Renew on a Txn created by Update will // panic. func (env *Env) Update(fn TxnOp) error { - return env.run(true, 0, fn) + runtime.LockOSThread() + defer runtime.UnlockOSThread() + return env.run(0, fn) } // UpdateLocked behaves like Update but does not lock the calling goroutine to @@ -596,14 +589,10 @@ func (env *Env) Update(fn TxnOp) error { // Any call to Commit, Abort, Reset or Renew on a Txn created by UpdateLocked // will panic. func (env *Env) UpdateLocked(fn TxnOp) error { - return env.run(false, 0, fn) + return env.run(0, fn) } -func (env *Env) run(lock bool, flags uint, fn TxnOp) error { - if lock { - runtime.LockOSThread() - defer runtime.UnlockOSThread() - } +func (env *Env) run(flags uint, fn TxnOp) error { txn, err := beginTxn(env, nil, flags) if err != nil { return err