diff --git a/libraries/liblmdb/mdb.c b/libraries/liblmdb/mdb.c index 47a5036..4e23888 100644 --- a/libraries/liblmdb/mdb.c +++ b/libraries/liblmdb/mdb.c @@ -1940,7 +1940,7 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp) MDB_txn *txn = mc->mc_txn; MDB_env *env = txn->mt_env; pgno_t pgno, *mop = env->me_pghead; - unsigned i, j, k, mop_len = mop ? mop[0] : 0, n2 = num-1; + unsigned i, j, mop_len = mop ? mop[0] : 0, n2 = num-1; MDB_page *np; txnid_t oldest = 0, last; MDB_cursor_op op; @@ -1967,7 +1967,7 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp) for (op = MDB_FIRST;; op = MDB_NEXT) { MDB_val key, data; MDB_node *leaf; - pgno_t *idl, old_id, new_id; + pgno_t *idl; /* Seek a big enough contiguous page range. Prefer * pages at the tail, just truncating the list. @@ -2033,21 +2033,12 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp) #if (MDB_DEBUG) > 1 DPRINTF(("IDL read txn %"Z"u root %"Z"u num %u", last, txn->mt_dbs[FREE_DBI].md_root, i)); - for (k = i; k; k--) - DPRINTF(("IDL %"Z"u", idl[k])); + for (j = i; j; j--) + DPRINTF(("IDL %"Z"u", idl[j])); #endif /* Merge in descending sorted order */ - j = mop_len; - k = mop_len += i; - mop[0] = (pgno_t)-1; - old_id = mop[j]; - while (i) { - new_id = idl[i--]; - for (; old_id < new_id; old_id = mop[--j]) - mop[k--] = old_id; - mop[k--] = new_id; - } - mop[0] = mop_len; + mdb_midl_xmerge(mop, idl); + mop_len = mop[0]; } /* Use new pages from the map when nothing suitable in the freeDB */ diff --git a/libraries/liblmdb/midl.c b/libraries/liblmdb/midl.c index d3d872a..88a3aff 100644 --- a/libraries/liblmdb/midl.c +++ b/libraries/liblmdb/midl.c @@ -199,6 +199,20 @@ int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n ) return 0; } +void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge ) +{ + MDB_ID old_id, merge_id, i = merge[0], j = idl[0], k = i+j, total = k; + idl[0] = (MDB_ID)-1; /* delimiter for idl scan below */ + old_id = idl[j]; + while (i) { + merge_id = merge[i--]; + for (; old_id < merge_id; old_id = idl[--j]) + idl[k--] = old_id; + idl[k--] = merge_id; + } + idl[0] = total; +} + /* Quicksort + Insertion sort for small arrays */ #define SMALL 8 diff --git a/libraries/liblmdb/midl.h b/libraries/liblmdb/midl.h index 08616f4..a7f2502 100644 --- a/libraries/liblmdb/midl.h +++ b/libraries/liblmdb/midl.h @@ -68,6 +68,9 @@ typedef MDB_ID *MDB_IDL; #define MDB_IDL_FIRST( ids ) ( (ids)[1] ) #define MDB_IDL_LAST( ids ) ( (ids)[(ids)[0]] ) + /** Current max length of an #mdb_midl_alloc()ed IDL */ +#define MDB_IDL_ALLOCLEN( ids ) ( (ids)[-1] ) + /** Append ID to IDL. The IDL must be big enough. */ #define mdb_midl_xappend(idl, id) do { \ MDB_ID *xidl = (idl), xlen = ++(xidl[0]); \ @@ -128,6 +131,12 @@ int mdb_midl_append_list( MDB_IDL *idp, MDB_IDL app ); */ int mdb_midl_append_range( MDB_IDL *idp, MDB_ID id, unsigned n ); + /** Merge an IDL onto an IDL. The destination IDL must be big enough. + * @param[in] idl The IDL to merge into. + * @param[in] merge The IDL to merge. + */ +void mdb_midl_xmerge( MDB_IDL idl, MDB_IDL merge ); + /** Sort an IDL. * @param[in,out] ids The IDL to sort. */