bmatsuo / lmdb-go

Bindings for the LMDB C library
BSD 3-Clause "New" or "Revised" License
156 stars 58 forks source link

lmdb: adds finalizer to unmanaged Txn objects, aborting them before gc #30

Closed bmatsuo closed 9 years ago

bmatsuo commented 9 years ago

Fixes #27

Txn objects created by the Env.BeginTxn method now have a finalizer than aborts them if they become unreachable before being committed. Aside from freeing memory this can help databases from growing indefinitely and can also unlock an environment from a lost write transaction. These benefits are new for unmanaged transactions. Managed transactions have never had such shortcomings.

These benchmark comparisons should be taken with a grain of salt. There are some unexplained big swings. But unmanaged transactions do not appear to have a significant performance impact. And renewed transactions do not suffer from the finalizer.

benchmark                              old ns/op     new ns/op     delta
BenchmarkEnv_ReaderList-4              72344         73023         +0.94%
BenchmarkTxn_Put-4                     1754          1746          -0.46%
BenchmarkTxn_PutReserve-4              1771          1733          -2.15%
BenchmarkTxn_PutReserve_writemap-4     1519          1442          -5.07%
BenchmarkTxn_Put_writemap-4            1446          1436          -0.69%
BenchmarkTxn_Get_ro-4                  2469          2278          -7.74%
BenchmarkTxn_Get_raw_ro-4              870           874           +0.46%
BenchmarkScan_ro-4                     10521174      10206348      -2.99%
BenchmarkScan_raw_ro-4                 1553810       1556701       +0.19%
BenchmarkCursor-4                      553           544           -1.63%
BenchmarkCursor_Renew-4                198           193           -2.53%
BenchmarkTxn_Sub_commit-4              916848        565000        -38.38%
BenchmarkTxn_Sub_abort-4               907221        553781        -38.96%
BenchmarkTxn_abort-4                   17212         17430         +1.27%
BenchmarkTxn_commit-4                  17233         17432         +1.15%
BenchmarkTxn_ro-4                      136053        137380        +0.98%
BenchmarkTxn_unmanaged_abort-4         17095         17883         +4.61%
BenchmarkTxn_unmanaged_commit-4        17087         18069         +5.75%
BenchmarkTxn_unmanaged_ro-4            732379        136897        -81.31%
BenchmarkTxn_renew-4                   403           408           +1.24%
BenchmarkTxn_Put_append-4              686           691           +0.73%
BenchmarkTxn_Put_append_noflag-4       877           884           +0.80%