assoc_array: choose between splitting a node and a shortcut without gotos
Changes
1 files changed · +61 −66
| File | Status | Add | Del | Content |
|---|---|---|---|---|
| lib/assoc_array.c | modified | +61 | −66 | View file |
lib/assoc_array.c
View filediff --git a/lib/assoc_array.c b/lib/assoc_array.c
index 7f9afbc..8bfdb0d 100644
--- a/lib/assoc_array.c
+++ b/lib/assoc_array.c
@@ -470,6 +470,21 @@ static bool assoc_array_insert_in_empty_tree(struct assoc_array_edit *edit)
}
/*
+ * The pointer that leads to a node or shortcut with the given back pointer
+ * and parent slot: the root, a slot in a node, or a shortcut's next node.
+ */
+static struct assoc_array_ptr **assoc_array_parent_link(struct assoc_array *array,
+ struct assoc_array_ptr *back_pointer,
+ int parent_slot)
+{
+ if (!back_pointer)
+ return &array->root;
+ if (assoc_array_ptr_is_node(back_pointer))
+ return &assoc_array_ptr_to_node(back_pointer)->slots[parent_slot];
+ return &assoc_array_ptr_to_shortcut(back_pointer)->next_node;
+}
+
+/*
* Insert into a terminal node without changing its shape, if that can be
* done: in place of a leaf with the same index key, or in a free slot.
*/
@@ -548,6 +563,20 @@ static bool assoc_array_fill_segment_cache(struct assoc_array_edit *edit,
}
/*
+ * Whether the leaves of a full node and the new key all share one segment,
+ * so that splitting the node cannot make room.
+ */
+static bool assoc_array_all_one_segment(const struct assoc_array_edit *edit)
+{
+ int i;
+
+ for (i = 1; i < ASSOC_ARRAY_FAN_OUT + 1; i++)
+ if (edit->segment_cache[i] != edit->segment_cache[0])
+ return false;
+ return true;
+}
+
+/*
* Split a full node into new_n0, which takes its place, and new_n1, below
* new_n0 in the slot of a segment that at least two of the leaves share,
* the new one included. The caller has filled in the segment cache and
@@ -735,11 +764,8 @@ static bool assoc_array_insert_into_terminal_node(struct assoc_array_edit *edit,
{
struct assoc_array_shortcut *new_s0;
struct assoc_array_node *node, *new_n0, *new_n1;
- struct assoc_array_ptr *ptr;
- unsigned long dissimilarity, base_seg;
bool have_meta;
int level;
- int i;
node = result->terminal_node.node;
level = result->terminal_node.level;
@@ -777,78 +803,47 @@ static bool assoc_array_insert_into_terminal_node(struct assoc_array_edit *edit,
have_meta = assoc_array_fill_segment_cache(edit, ops, node, level);
edit->segment_cache[ASSOC_ARRAY_FAN_OUT] = result->terminal_node.slot;
- if (have_meta) {
- pr_devel("have meta\n");
- goto split_node;
- }
-
- /* The node contains only leaves */
- dissimilarity = 0;
- base_seg = edit->segment_cache[0];
- for (i = 1; i < ASSOC_ARRAY_FAN_OUT; i++)
- dissimilarity |= edit->segment_cache[i] ^ base_seg;
-
- pr_devel("only leaves; dissimilarity=%lx\n", dissimilarity);
-
- if ((dissimilarity & ASSOC_ARRAY_FAN_MASK) == 0) {
- /* The old leaves all cluster in the same slot. We will need
- * to insert a shortcut if the new node wants to cluster with them.
+ if (!have_meta && assoc_array_all_one_segment(edit)) {
+ /* The node only contains leaves and all the leaves cluster in
+ * the same slot, so we need to insert a shortcut to skip the
+ * common portion of the key and split the node at the
+ * shortcut's far end.
*/
- if ((edit->segment_cache[ASSOC_ARRAY_FAN_OUT] ^ base_seg) == 0)
- goto all_leaves_cluster_together;
-
- /* Otherwise all the old leaves cluster in the same slot, but
- * the new leaf wants to go into a different slot - so we
- * create a new node (n0) to hold the new leaf and a pointer to
- * a new node (n1) holding all the old leaves.
+ new_s0 = assoc_array_insert_shortcut(edit, ops, index_key,
+ node, level, new_n0);
+ if (!new_s0)
+ return false;
+ edit->set[0].ptr = assoc_array_parent_link(edit->array,
+ new_s0->back_pointer,
+ new_s0->parent_slot);
+ edit->set[0].to = assoc_array_shortcut_to_ptr(new_s0);
+ } else {
+ /* We need to split the current node. The node must contain
+ * anything from a single leaf (in the one leaf case, this leaf
+ * will cluster with the new leaf) and the rest meta-pointers,
+ * to all leaves, some of which may cluster.
+ *
+ * We need to expel at least two leaves out of a set consisting
+ * of the leaves in the node and the new leaf. The current meta
+ * pointers can just be copied as they shouldn't cluster with
+ * any of the leaves. If all the old leaves cluster in one slot
+ * and the new leaf does not, they all go and the new leaf stays.
*
- * This can be done by falling through to the node splitting
- * path.
+ * We need a new node (n0) to replace the current one and a new
+ * node to take the expelled nodes (n1).
*/
- pr_devel("present leaves cluster but not new leaf\n");
+ new_n0->back_pointer = node->back_pointer;
+ new_n0->parent_slot = node->parent_slot;
+ edit->set[0].ptr = assoc_array_parent_link(edit->array,
+ new_n0->back_pointer,
+ new_n0->parent_slot);
+ edit->set[0].to = assoc_array_node_to_ptr(new_n0);
}
-split_node:
- /* We need to split the current node. The node must contain anything
- * from a single leaf (in the one leaf case, this leaf will cluster
- * with the new leaf) and the rest meta-pointers, to all leaves, some
- * of which may cluster.
- *
- * It won't contain the case in which all the current leaves plus the
- * new leaves want to cluster in the same slot.
- *
- * We need to expel at least two leaves out of a set consisting of the
- * leaves in the node and the new leaf. The current meta pointers can
- * just be copied as they shouldn't cluster with any of the leaves.
- *
- * We need a new node (n0) to replace the current one and a new node to
- * take the expelled nodes (n1).
- */
- edit->set[0].to = assoc_array_node_to_ptr(new_n0);
- new_n0->back_pointer = node->back_pointer;
- new_n0->parent_slot = node->parent_slot;
-
-do_split_node:
assoc_array_split_node(edit, node, new_n0, new_n1);
-
- ptr = node->back_pointer;
- if (!ptr)
- edit->set[0].ptr = &edit->array->root;
- else if (assoc_array_ptr_is_node(ptr))
- edit->set[0].ptr = &assoc_array_ptr_to_node(ptr)->slots[node->parent_slot];
- else
- edit->set[0].ptr = &assoc_array_ptr_to_shortcut(ptr)->next_node;
edit->excised_meta[0] = assoc_array_node_to_ptr(node);
pr_devel("<--%s() = ok [split node]\n", __func__);
return true;
-
-all_leaves_cluster_together:
- new_s0 = assoc_array_insert_shortcut(edit, ops, index_key,
- node, level, new_n0);
- if (!new_s0)
- return false;
- edit->set[0].to = assoc_array_shortcut_to_ptr(new_s0);
- goto do_split_node;
}
/*