Skip to content

Commit

Permalink
Add n_keys option to Dict.empty()
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanfed committed Jul 6, 2022
1 parent 99c702f commit e0a5a13
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions numba/cext/dictobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,18 +563,26 @@ numba_dict_new(NB_Dict** out, Py_ssize_t size, Py_ssize_t key_size, Py_ssize_t v
/* Allocate a new dictionary with enough space to hold n_keys without resizes */
int
numba_dict_new_noresize(NB_Dict** out, Py_ssize_t n_keys, Py_ssize_t key_size, Py_ssize_t val_size) {

if (n_keys <= USABLE_FRACTION(D_MINSIZE)) {
return numba_dict_new(out, D_MINSIZE, key_size, val_size);
}

Py_ssize_t size;

size = n_keys + (n_keys >> 1); // Relies on a load factor of 2/3.
size = n_keys + (n_keys >> 1) + 1; // Relies on a load factor of 2/3.

/* Round up to the nearest power of 2.
Overflows when result is 2^(8*sizeof(Py_ssize_t)-1) due to signed Py_ssize_t.
Could pose a real problem on 32-bit systems. */
Could be problematic on 32-bit systems. */

size--;

uint32_t shift;
for (shift = 1; shift < sizeof(Py_ssize_t) * CHAR_BIT; shift <<= 1) {
size |= (size >> shift);
}

size++;

return numba_dict_new(out, size, key_size, val_size);
Expand Down

0 comments on commit e0a5a13

Please sign in to comment.