Skip to contentSkip to conversation

Library: [RFC PATCH] btree_blue - a btree with linear travesal function and more

PATCH liuwf 1 participant 1 message Apr 24, 2023 Library code

lore
Share & tools
lore

liuwf

author
#0 lore raw

From 10ca8a2c319c743561020f578308da136e63e916 Mon Sep 17 00:00:00 2001
From: Liu Weifeng 4019c26b5c0d5f6b <[email protected]>
Date: Mon, 24 Apr 2023 18:40:39 -0400

btree_blue is a simple btree whose algorithm implementation comes from Joern Engel's lib/btree. btree_blue extended lib/btree with several features and optimizations:

Add two-way links between leaf-nodes to offer a fast linear traversal.

Insert, Search, Delete are relatively fast comparing with lib/btree, rbtee. Even if choosing a big node size(4K) btree_blue is still a bit faster than other trees.

The value of key itself can use a 0 value.

Nodes can be variable in size 256, 512, 1024, 2K, 4K, or 192, 384 ...

Key length has sizes of one long, two long, four long (32-bit OS).

Joern Engel's lib/btree represents an elegance in logical and is fast and effective(100% slots usage). When I decided to add features based on it like two-way links between leaf-nodes, bigger node sizes (and more,in the feature) which are required in many practices, I found it is a challenge to keep speed and slots usage with lib/btree, so I have to give several optimizings to keep or get more(hopefully) performance.

Here are results of a simple test, the results may be unfair for some objects, for example, maple tree is RCU-safe and can deal with many complex cases, rbtree support duplicated keys, lib/btree's grace has virtue itself ....

For the fast linear traversal, I have not give a contrast test, and I think results would not be on an order of magnitude over those who have not direct links between nodes.

1. btree_blue with a faster plain byte comparing version

btree_blue use node size of 512 bytes in the first part of test, leaf node has slots usage of 15/16 (can be higher like 31/32 in 1k node size ...), non-leaf nodes keep 100% slots usage.

--------------------------------------------------------------------------------
# 1000000 inserts to a empty tree ...

btree_blue is faster than 29% (btree), 130% (rbtree), 313% (maple tree)

maple tree 1000000 inserts use time: 989232430 ns
rbtree 1000000 inserts use time: 552244351 ns
btree 1000000 inserts use time: 310080508 ns
btree_blue 1000000 inserts use time: 238926066 ns

# 1000000 searches ...

btree_blue, btree and maple_tree got almost the same rate, and are all faster nealy 100% than rbtree.

maple tree 1000000 searches use time: 241619833 ns
rbtree 1000000 searches use time: 421757078 ns
btree 1000000 searches use time: 232226540 ns
btree_blue 1000000 searches use time: 220941629 ns

# 1000000 mixed insert + delete based on a tree which has 1000000 nodes ...

btree_blue is faster than 28% (btree), 132% (rbtree), 307% (maple_tree)

maple tree 1000000 mixed insert + delete use time: 2321929510 ns
rbtree 1000000 mixed insert + delete use time: 1325037376 ns
btree 1000000 mixed insert + delete use time: 734110187 ns
btree_blue 1000000 mixed insert + delete use time: 569514235 ns

# delete a tree which has 1000000 nodes to empty ...

btree_blue is faster than 34% (btree), 117% (rbtree), 613% (maple_tree)

maple tree 1000000 deletes use time: 1606556096 ns
rbtree 1000000 deletes use time: 489245700 ns
btree 1000000 deletes use time: 301982693 ns
btree_blue 1000000 deletes use time: 224875145 ns

btree_blue uses a big node size of 4096 bytes

# 1000000 inserts to a empty tree ...

maple tree 1000000 inserts use time: 1006161194 ns
rbtree 1000000 inserts use time: 582635990 ns
btree 1000000 inserts use time: 311212510 ns
btree_blue 1000000 inserts use time: 323870887 ns

# 1000000 searches ...

maple tree 1000000 searches use time: 260520956 ns
rbtree 1000000 searches use time: 459984150 ns
btree 1000000 searches use time: 242243634 ns
btree_blue 1000000 searches use time: 207793404 ns

# 1000000 mixed insert + delete based on a tree which has 1000000 nodes ...

maple tree 1000000 mixed insert + delete use time: 2316355888 ns
rbtree 1000000 mixed insert + delete use time: 1499731734 ns
btree 1000000 mixed insert + delete use time: 761392377 ns
btree_blue 1000000 mixed insert + delete use time: 737142324 ns

# delete a tree which has 1000000 nodes to empty ...

maple tree 1000000 deletes use time: 1616467751 ns
rbtree 1000000 deletes use time: 570675402 ns
btree 1000000 deletes use time: 299528612 ns
btree_blue 1000000 deletes use time: 226293835 ns

2 btree_blue with binary search version, btree_blue uses node size of 512 bytes
--------------------------------------------------------------------------------

# 1000000 inserts to a empty tree ...

maple tree 1000000 inserts use time: 1007482573 ns
rbtree 1000000 inserts use time: 552520109 ns
btree 1000000 inserts use time: 311470955 ns
btree_blue 1000000 inserts use time: 305165271 ns

# 1000000 searches ...

maple tree 1000000 searches use time: 248842879 ns
rbtree 1000000 searches use time: 459954315 ns
btree 1000000 searches use time: 239902063 ns
btree_blue 1000000 searches use time: 251252337 ns

# 1000000 mixed insert + delete based on a tree which has 1000000 nodes ...

maple tree 1000000 mixed insert + delete use time: 2279468017 ns
rbtree 1000000 mixed insert + delete use time: 1567305915 ns
btree 1000000 mixed insert + delete use time: 742089882 ns
btree_blue 1000000 mixed insert + delete use time: 679494846 ns

# delete a tree which has 1000000 nodes to empty ...

maple tree 1000000 deletes use time: 1630034161 ns
rbtree 1000000 deletes use time: 561434384 ns
btree 1000000 deletes use time: 317263337 ns
btree_blue 1000000 deletes use time: 252872063 ns

# btree_blue uses node size of 4096 bytes

# 1000000 inserts to a empty tree ...

maple tree 1000000 inserts use time: 1013781694 ns
rbtree 1000000 inserts use time: 549275685 ns
btree 1000000 inserts use time: 314653894 ns
btree_blue 1000000 inserts use time: 395352766 ns

# 1000000 searches ...

maple tree 1000000 searches use time: 256280986 ns
rbtree 1000000 searches use time: 453439598 ns
btree 1000000 searches use time: 243705745 ns
btree_blue 1000000 searches use time: 241042481 ns

# 1000000 mixed insert + delete based on a tree which has 1000000 nodes ...

maple tree 1000000 mixed insert + delete use time: 2319430653 ns
rbtree 1000000 mixed insert + delete use time: 1440130422 ns
btree 1000000 mixed insert + delete use time: 742443694 ns
btree_blue 1000000 mixed insert + delete use time: 886005615 ns

# delete a tree which has 1000000 nodes to empty ...

maple tree 1000000 deletes use time: 1618646788 ns
rbtree 1000000 deletes use time: 554263247 ns
btree 1000000 deletes use time: 314184624 ns
btree_blue 1000000 deletes use time: 258111208 ns

---
 include/linux/btree_blue.h |  87 ++++
 lib/Kconfig                |   4 +
 lib/Makefile               |   1 +
 lib/btree_blue.c           | 971 +++++++++++++++++++++++++++++++++++++
 4 files changed, 1063 insertions(+)
 create mode 100644 include/linux/btree_blue.h
 create mode 100644 lib/btree_blue.c
include/linux/btree_blue.h new file +87 -0
new file mode 100644
index 000000000000..dfa185d8893b
@@ -0,0 +1,87 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef BTREE_BLUE_H
+#define BTREE_BLUE_H
+
+#include <linux/kernel.h>
+#include <linux/mempool.h>
+
+
+#define MAX_TREE_HEIGHT		8
+#define MIN_SLOTS_NUMBER	16
+#define MIN_NODE_SIZE		(MIN_SLOTS_NUMBER * 2 * sizeof(unsigned long))
+
+
+#define GET_PREV		0
+#define GET_NEXT		1
+
+struct btree_blue_head;
+struct btree_blue_node_cb;
+
+
+struct btree_blue_head {
+	unsigned long *node;
+	mempool_t *mempool;
+	
+	u16 node_size;
+	u16 stub_base;
+	u8 keylen;
+	u8 slot_width;
+	u8 height;
+	u8 reserved[1];
+
+	u16 vols[MAX_TREE_HEIGHT + 1];
+};
+
+void *btree_blue_alloc(gfp_t gfp_mask, void *pool_data);
+
+void btree_blue_free(void *element, void *pool_data);
+
+int __must_check btree_blue_init(struct btree_blue_head *head, 
+								 int node_size_in_byte, 
+								 int key_len_in_byte, int flags);
+
+void btree_blue_destroy(struct btree_blue_head *head);
+
+void *btree_blue_lookup(struct btree_blue_head *head, unsigned long *key);
+
+int __must_check btree_blue_insert(struct btree_blue_head *head, 
+								   unsigned long *key, void *val, gfp_t gfp);
+
+int btree_blue_update(struct btree_blue_head *head, unsigned long *key, 
+					  void *val);
+
+void *btree_blue_remove(struct btree_blue_head *head, unsigned long *key);
+
+void* btree_blue_first(struct btree_blue_head *head,  unsigned long * __key);
+void* btree_blue_last(struct btree_blue_head *head,  unsigned long * __key);
+
+void* btree_blue_get_prev(struct btree_blue_head *head, unsigned long * __key);
+void* btree_blue_get_next(struct btree_blue_head *head, unsigned long * __key);
+
+typedef bool (*btree_blue_traverse_FN_T) (const unsigned long *key, 
+										  const void *val);
+
+/*
+ * Visit each key-value pair started from the @key and continue toward 
+ * @prev_or_next until the last or fisrt. 
+ *
+ * IF @key is given NULL, visit starts with the last(the biggest) key and walk 
+ * toward the smallest.
+ *
+ * @prev_or_next, bool value to specify the visit direction.
+ *
+ * @callback. Your function that is called in the visit loop with each key-value
+ * visited.
+ * If a function like : bool myfunc(const unsigned long *key, const void *val) 
+ * is given to the param @callback, you will see every *key and *val from the 
+ * start @key(included, and it's value). Your function's return value of 0 
+ * indicates to continue visit and 1 to exit the loop.
+ *
+ * */
+size_t btree_blue_traverse_from_key(struct btree_blue_head *head, 
+									unsigned long *key, 
+									btree_blue_traverse_FN_T callback, 
+									bool prev_or_next);
+
+#endif
+
lib/Kconfig +4 -0
index 9bbf8a4b2108..a0e185ddf389 100644
@@ -464,6 +464,10 @@ config TEXTSEARCH_FSM
 
 config BTREE
 	bool
+	
+config BTREE_BLUE
+	tristate
+	default m
 
 config INTERVAL_TREE
 	bool
lib/Makefile +1 -0
index 59bd7c2f793a..fe37f467d219 100644
@@ -154,6 +154,7 @@ obj-$(CONFIG_TRACE_MMIO_ACCESS) += trace_readwrite.o
 obj-$(CONFIG_GENERIC_HWEIGHT) += hweight.o
 
 obj-$(CONFIG_BTREE) += btree.o
+obj-$(CONFIG_BTREE_BLUE) += btree_blue.o
 obj-$(CONFIG_INTERVAL_TREE) += interval_tree.o
 obj-$(CONFIG_ASSOCIATIVE_ARRAY) += assoc_array.o
 obj-$(CONFIG_DEBUG_PREEMPT) += smp_processor_id.o
lib/btree_blue.c new file +971 -0

971 lines not on this page · raw message

Signature · 1 line
2.30.2