summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmaury Séchet <deadalnix@gmail.com>2023-03-02 23:32:42 +0000
committerQi Wang <interwq@gmail.com>2023-03-31 14:34:24 -0700
commit543e2d61e6047208d647cf3fd3499bead3bcc23e (patch)
tree5880141a89909fefafe52374102bd30fa2b3b328
parent31e01a98f159926493158cde6453cde55f21c42b (diff)
downloadjemalloc-543e2d61e6047208d647cf3fd3499bead3bcc23e.tar.gz
Simplify the logic in ph_insert
Also fixes what looks like an off by one error in the lazy aux list merge part of the code that previously never touched the last node in the aux list.
-rw-r--r--include/jemalloc/internal/ph.h59
1 files changed, 30 insertions, 29 deletions
diff --git a/include/jemalloc/internal/ph.h b/include/jemalloc/internal/ph.h
index 5f091c5f..8ceadb90 100644
--- a/include/jemalloc/internal/ph.h
+++ b/include/jemalloc/internal/ph.h
@@ -318,36 +318,37 @@ ph_insert(ph_t *ph, void *phn, size_t offset, ph_cmp_t cmp) {
*/
if (ph->root == NULL) {
ph->root = phn;
- } else {
- /*
- * As a special case, check to see if we can replace the root.
- * This is practically common in some important cases, and lets
- * us defer some insertions (hopefully, until the point where
- * some of the items in the aux list have been removed, savings
- * us from linking them at all).
- */
- if (cmp(phn, ph->root) < 0) {
- phn_lchild_set(phn, ph->root, offset);
- phn_prev_set(ph->root, phn, offset);
- ph->root = phn;
- ph->auxcount = 0;
- return;
- }
- ph->auxcount++;
- phn_next_set(phn, phn_next_get(ph->root, offset), offset);
- if (phn_next_get(ph->root, offset) != NULL) {
- phn_prev_set(phn_next_get(ph->root, offset), phn,
- offset);
- }
- phn_prev_set(phn, ph->root, offset);
- phn_next_set(ph->root, phn, offset);
+ return;
}
- if (ph->auxcount > 1) {
- unsigned nmerges = ffs_zu(ph->auxcount - 1);
- bool done = false;
- for (unsigned i = 0; i < nmerges && !done; i++) {
- done = ph_try_aux_merge_pair(ph, offset, cmp);
- }
+
+ /*
+ * As a special case, check to see if we can replace the root.
+ * This is practically common in some important cases, and lets
+ * us defer some insertions (hopefully, until the point where
+ * some of the items in the aux list have been removed, savings
+ * us from linking them at all).
+ */
+ if (cmp(phn, ph->root) < 0) {
+ phn_lchild_set(phn, ph->root, offset);
+ phn_prev_set(ph->root, phn, offset);
+ ph->root = phn;
+ ph->auxcount = 0;
+ return;
+ }
+
+ phn_next_set(phn, phn_next_get(ph->root, offset), offset);
+ if (phn_next_get(ph->root, offset) != NULL) {
+ phn_prev_set(phn_next_get(ph->root, offset), phn,
+ offset);
+ }
+ phn_prev_set(phn, ph->root, offset);
+ phn_next_set(ph->root, phn, offset);
+
+ ph->auxcount++;
+ unsigned nmerges = ffs_zu(ph->auxcount);
+ bool done = false;
+ for (unsigned i = 0; i < nmerges && !done; i++) {
+ done = ph_try_aux_merge_pair(ph, offset, cmp);
}
}