Skip to contentSkip to conversation

library: btree_blue - a simple btree with rapid linear traversal

PATCH liuwf 2 participants 3 messages May 9, 2023 Library code

lore
Share & tools
lore

liuwf

author
#0 lore raw

From: Liu Weifeng 4019c26b5c0d5f6b <[email protected]>
Date: Tue, 9 May 2023 07:39:58 -0400

Subject: library: btree_blue - a simple btree with rapid linear traversal V7.2

This is a bug-fixed version. Thanks Uladzislau Rezki very much for testing.

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:

Added Two-way links between leaf-nodes to offer a very fast linear traversal.

Besides rapid linear traversal, insert, search, delete are also relatively fast comparing with btree, rbtee, even though with a big node size (4K) btree_blue is still some fast.

The 0 value of key itself is allowed.

Node size can be choosed in 256, 512, 1024, 2K, 4K. Or 192, 384 ...

Key length has sizes of one long, two longs (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 new features on it, which are required for me and in many practices, I found it is a challenge to keep speed and effective with lib/btree, so I have to do several optimizings to keep or (hopefully) get more performance.

Basically, traversal in btree_blue is 8x (800%) faster than btree, 14x (1400%) faster than rbtree. Random insert/search/delete is 30% ~ 50% faster than btree and is 2x (200%) ~ 3x (300%) faster than rbtree, 2x (200%) ~ 4x (400%) faster than maple tree.

Note, those comparisons may not much fair for the three trees: maple tree is RCU-safe and can deal with many complex cases, rbtree support duplicated keys, lib/btree's grace logical has virtue itself ...

---
 include/linux/btree_blue.h |   85 +++
 lib/Kconfig                |    8 +
 lib/Makefile               |    2 +
 lib/btree_blue.c           | 1042 ++++++++++++++++++++++++++++++++++++
 lib/btree_blue_test.c      |  578 ++++++++++++++++++++
 5 files changed, 1715 insertions(+)
 create mode 100644 include/linux/btree_blue.h
 create mode 100644 lib/btree_blue.c
 create mode 100644 lib/btree_blue_test.c
include/linux/btree_blue.h new file +85 -0
new file mode 100644
index 000000000000..62217b96cb4e
@@ -0,0 +1,85 @@
+/* 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;
+
+	u16 node_size;
+	u16 leaf_size;
+	u16 stub_base;
+	u8 keylen;
+	u8 slot_width;
+	u8 height;
+	u8 reserved[1];
+
+	u16 slot_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.
+ *
+ * @return value. The API return the number of key-value pairs visited.
+ *
+ * */
+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 +8 -0
index ce2abffb9ed8..fa0845bb59ac 100644
@@ -466,6 +466,14 @@ config TEXTSEARCH_FSM
 config BTREE
 	bool
 
+config BTREE_BLUE
+	tristate
+	default m
+
+config BTREE_BLUE_TEST
+	tristate
+	default m
+
 config INTERVAL_TREE
 	bool
 	help
lib/Makefile +2 -0
index 4d9461bfea42..463deb504073 100644
@@ -152,6 +152,8 @@ 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_BTREE_BLUE_TEST) += btree_blue_test.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 +1042 -0

1,042 lines not on this page · raw message

lib/btree_blue_test.c new file +578 -0

578 lines not on this page · raw message

base-commit: fbe1871b562af6e9cffcf622247e821d1dd16c64

Signature · 1 line
2.30.2
#1 lore raw
kernel test robot noticed the following build errors:

Hi liuwf,

kernel test robot noticed the following build errors:

[auto build test ERROR on fbe1871b562af6e9cffcf622247e821d1dd16c64]

url:    https://github.com/intel-lab-lkp/linux/commits/liuwf/library-btree_blue-a-simple-btree-with-rapid-linear-traversal/20230509-095127
base:   fbe1871b562af6e9cffcf622247e821d1dd16c64
patch link:    https://lore.kernel.org/r/37586fd4e76b597aea4dad3f06bb7eb648425fa0.camel%40mailbox.org
patch subject: library: btree_blue - a simple btree with rapid linear traversal
config: i386-tinyconfig (https://download.01.org/0day-ci/archive/20230509/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0

reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/fba954cf20fd4764f0e6090d1164251873b7569c
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review liuwf/library-btree_blue-a-simple-btree-with-rapid-linear-traversal/20230509-095127 git checkout fba954cf20fd4764f0e6090d1164251873b7569c
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=i386 olddefconfig
make W=1 O=build_dir ARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

ld: lib/btree_blue_test.o: in function `btree_blue_test_init':

>> btree_blue_test.c:(.text+0xb7): undefined reference to `btree_init'
>> ld: btree_blue_test.c:(.text+0x133): undefined reference to `btree_geo64'
>> ld: btree_blue_test.c:(.text+0x13d): undefined reference to `btree_insert'
   ld: btree_blue_test.c:(.text+0x25b): undefined reference to `btree_geo64'
>> ld: btree_blue_test.c:(.text+0x269): undefined reference to `btree_lookup'
   ld: btree_blue_test.c:(.text+0x379): undefined reference to `btree_geo64'
   ld: btree_blue_test.c:(.text+0x38f): undefined reference to `btree_insert'
   ld: btree_blue_test.c:(.text+0x3aa): undefined reference to `btree_geo64'
>> ld: btree_blue_test.c:(.text+0x3b8): undefined reference to `btree_remove'
   ld: btree_blue_test.c:(.text+0x57f): undefined reference to `btree_geo64'
>> ld: btree_blue_test.c:(.text+0x58d): undefined reference to `btree_last'
   ld: btree_blue_test.c:(.text+0x5a4): undefined reference to `btree_geo64'
>> ld: btree_blue_test.c:(.text+0x5ae): undefined reference to `btree_get_prev'
   ld: btree_blue_test.c:(.text+0x5fb): undefined reference to `btree_geo64'
   ld: btree_blue_test.c:(.text+0x609): undefined reference to `btree_last'
   ld: btree_blue_test.c:(.text+0x62e): undefined reference to `btree_geo64'
   ld: btree_blue_test.c:(.text+0x638): undefined reference to `btree_get_prev'
   ld: btree_blue_test.c:(.text+0x6ef): undefined reference to `btree_geo64'
   ld: btree_blue_test.c:(.text+0x6f9): undefined reference to `btree_remove'
>> ld: btree_blue_test.c:(.text+0x7cd): undefined reference to `btree_destroy'
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests
#2 lore raw
In reply to liuwf
kernel test robot noticed the following build errors:

Hi liuwf,

kernel test robot noticed the following build errors:

[auto build test ERROR on fbe1871b562af6e9cffcf622247e821d1dd16c64]

url:    https://github.com/intel-lab-lkp/linux/commits/liuwf/library-btree_blue-a-simple-btree-with-rapid-linear-traversal/20230509-095127
base:   fbe1871b562af6e9cffcf622247e821d1dd16c64
patch link:    https://lore.kernel.org/r/37586fd4e76b597aea4dad3f06bb7eb648425fa0.camel%40mailbox.org
patch subject: library: btree_blue - a simple btree with rapid linear traversal
config: um-i386_defconfig (https://download.01.org/0day-ci/archive/20230509/[email protected]/config)
compiler: gcc-11 (Debian 11.3.0-12) 11.3.0

reproduce (this is a W=1 build):
# https://github.com/intel-lab-lkp/linux/commit/fba954cf20fd4764f0e6090d1164251873b7569c
git remote add linux-review https://github.com/intel-lab-lkp/linux
git fetch --no-tags linux-review liuwf/library-btree_blue-a-simple-btree-with-rapid-linear-traversal/20230509-095127 git checkout fba954cf20fd4764f0e6090d1164251873b7569c
# save the config file
mkdir build_dir && cp config build_dir/.config
make W=1 O=build_dir ARCH=um SUBARCH=i386 olddefconfig
make W=1 O=build_dir ARCH=um SUBARCH=i386 SHELL=/bin/bash

If you fix the issue, kindly add following tag where applicable
| Reported-by: kernel test robot <[email protected]>
| Link: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>, old ones prefixed by <<):

>> ERROR: modpost: "btree_init" [lib/btree_blue_test.ko] undefined!
>> ERROR: modpost: "btree_geo64" [lib/btree_blue_test.ko] undefined!
>> ERROR: modpost: "btree_insert" [lib/btree_blue_test.ko] undefined!
>> ERROR: modpost: "btree_lookup" [lib/btree_blue_test.ko] undefined!
>> ERROR: modpost: "btree_remove" [lib/btree_blue_test.ko] undefined!
>> ERROR: modpost: "btree_last" [lib/btree_blue_test.ko] undefined!
>> ERROR: modpost: "btree_get_prev" [lib/btree_blue_test.ko] undefined!
>> ERROR: modpost: "btree_destroy" [lib/btree_blue_test.ko] undefined!
Signature · 2 lines
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests