Skip to content

Commit

Permalink
Use tighter packing on respread
Browse files Browse the repository at this point in the history
  • Loading branch information
kriszyp committed Feb 21, 2024
1 parent c5db3b1 commit 8e737bb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
11 changes: 6 additions & 5 deletions dependencies/lmdb/libraries/liblmdb/mdb.c
Original file line number Diff line number Diff line change
Expand Up @@ -2814,18 +2814,19 @@ mdb_page_alloc(MDB_cursor *mc, int num, MDB_page **mp)
}
}
}
if (empty_entries > ((i + (wrapped ? mop_len : 0) - start) >> 1) + 20) {
// see if there is too many empty entries; after a respread it should be between one third and one quarter full of empty entries
if (empty_entries > ((i + (wrapped ? mop_len : 0) - start) / 3) + 10) {
unsigned old_length = env->me_pghead[0];
mdb_midl_respread(&env->me_pghead);
mop = env->me_pghead;
mop_len = mop[0];
fprintf(stderr, "resized from %u to %u\n", old_length, mop_len);
//fprintf(stderr, "resized from %u to %u\n", old_length, mop_len);
goto restart_search;
}
env->me_freelist_position = i;
i = 0;

if (mop_len > 2000) {
if (mop_len > 4000) {
//fprintf(stderr, "Too many entries %u, looking for %u, best fit %u, not loading anymore\n", mop_len, num, best_fit_size);
goto continue_best_fit;
}
Expand Down Expand Up @@ -3880,8 +3881,8 @@ mdb_txn_end(MDB_txn *txn, unsigned mode)
mdb_midl_shrink(&txn->mt_free_pgs);
env->me_free_pgs = txn->mt_free_pgs;
/* me_pgstate: */
if (env->me_pghead && env->me_pghead[0] > 5000) {
fprintf(stderr, "Free list too large %u, dumping from memory\n", env->me_pghead[0]);
if (env->me_pghead && env->me_pghead[0] > 8000) {
//fprintf(stderr, "Free list too large %u, dumping from memory\n", env->me_pghead[0]);
// if it is too large, reset it
env->me_pghead = NULL;
env->me_pglast = 0;
Expand Down
7 changes: 5 additions & 2 deletions dependencies/lmdb/libraries/liblmdb/midl.c
Original file line number Diff line number Diff line change
Expand Up @@ -456,14 +456,16 @@ int mdb_midl_respread( MDB_IDL *idp )
unsigned j = 1;
unsigned size = ids[0];
unsigned new_size = 0;
unsigned entry_count = 0;
// first, do compaction
for (unsigned i = 1; i <= size; i++) {
ssize_t entry;
while (!(entry = ids[i])) {
if (++i > ids[0]) goto expand;
}
ids[j++] = entry;
new_size += entry < 0 ? 3 : 2; // one for empty space, one for the entry, and one for the length if it is a block
new_size += entry < 0 ? 2 : 1; // one for the entry, and one for the length if it is a block
if (++entry_count & 1) new_size++; // and one for empty space on every other
if (entry < 0) ids[j++] = ids[++i]; // this was a block with a length
}
expand:
Expand All @@ -480,7 +482,8 @@ int mdb_midl_respread( MDB_IDL *idp )
ids[i--] = entry;
j--;
}
ids[i--] = 0; // empty slot for growth
if (entry_count-- & 1)
ids[i--] = 0; // empty slot for growth
}
return 0;
}
Expand Down

0 comments on commit 8e737bb

Please sign in to comment.