@@ -2149,6 +2149,11 @@ radix_tree<Key, Value, BytesView>::internal_bound(const K &k) const
21492149
21502150 const_iterator result;
21512151
2152+ /*
2153+ * n would point to element with key which we are looking for
2154+ * (if it existed). All elements on the left are smaller and all
2155+ * element on the right are bigger.
2156+ */
21522157 if (!n) {
21532158 assert (prev && !is_leaf (prev));
21542159
@@ -2178,13 +2183,33 @@ radix_tree<Key, Value, BytesView>::internal_bound(const K &k) const
21782183 * The target node must be the next leaf. */
21792184 result = ++const_iterator (leaf, this );
21802185 } else {
2181- /* *slot is the point of divergence. */
21822186 assert (diff < leaf_key.size () && diff < key.size ());
21832187
2184- /* The target node must be within *slot's subtree. The
2185- * left siblings of *slot are all less than the
2186- * looked-for key. */
2187- if (compare (key, leaf_key, diff) < 0 ) {
2188+ /* *slot is the point of divergence. It means the tree
2189+ * is compressed.
2190+ *
2191+ * Example for key AXXB or AZZB
2192+ * ROOT
2193+ * / \
2194+ * A B
2195+ * / \
2196+ * *slot ...
2197+ * / \
2198+ * YYA YYC
2199+ * / \
2200+ * ... ...
2201+ *
2202+ * We need to compare the first bytes of key and
2203+ * leaf_key to decide where to continue our search (for
2204+ * AXXB we would compare X with Y).
2205+ */
2206+
2207+ /* If next byte in key is less than in leaf_key it means
2208+ * that the target node must be within *slot's subtree.
2209+ * The left siblings of *slot are all less than the
2210+ * looked-for key (this is the case fo AXXB from the
2211+ * example above). */
2212+ if (key[diff] < leaf_key[diff]) {
21882213 auto target_leaf =
21892214 find_leaf<node::direction::Forward>(n);
21902215
@@ -2195,9 +2220,18 @@ radix_tree<Key, Value, BytesView>::internal_bound(const K &k) const
21952220 } else if (slot == &root) {
21962221 result = const_iterator (nullptr , this );
21972222 } else {
2198- /* Since looked-for key is larger than *slot,
2199- * the target node must be within subtree of a
2200- * right sibling of *slot. */
2223+ assert (key[diff] > leaf_key[diff]);
2224+
2225+ /* Since next byte in key is greater
2226+ * than in leaf_key, the target node
2227+ * must be within subtree of a right
2228+ * sibling of *slot. All leaves in the
2229+ * subtree under *slot are smaller than
2230+ * key (this is the case of AZZB). This
2231+ * is because the tree is compressed so
2232+ * all nodes under *slot share common prefix
2233+ * ("YY" in the example above) - leaf_key[diff]
2234+ * is the same for all keys under *slot. */
22012235 auto target_leaf = next_leaf<
22022236 node::direction::Forward>(
22032237 prev->template make_iterator <
0 commit comments