libtraceeval: Replace hash lookup with red-black tree
PATCH 1/1 libtraceeval: Replace hash lookup with red-black tree
From: "Steven Rostedt (Google)" <[email protected]>
When starting to write another tool that will look at all of an existing traceeval during the run, I found that the hash table is not efficient when needing to iterate events in the fast path. That's because the hash buckets can be large (1024 buckets in size), and to iterate all elements in the traceeval will require iterating all the buckets even when there's only a few items in the traceeval.
One solution could have been just to add a link list of all elements, but this would require two indices to maintain. Instead, switching to a red-black tree is what I decided to go with. One, I had just written one for libtracecmd[1], and two, it will always be a O(log(n)) lookup, where as a hash, if unlucky, could be O(n).
[1] https://lore.kernel.org/all/[email protected]/
--- src/Makefile | 2 +- src/delta.c | 11 +- src/eval-local.h | 94 ++++------ src/hash.c | 123 ------------- src/histograms.c | 179 ++++++------------- src/rbtree.c | 455 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 549 insertions(+), 315 deletions(-) delete mode 100644 src/hash.c create mode 100644 src/rbtree.c
src/Makefile +1 -1
@@ -5,7 +5,7 @@ include $(src)/scripts/utils.mk OBJS = OBJS += histograms.o OBJS += delta.o -OBJS += hash.o +OBJS += rbtree.o OBJS := $(OBJS:%.o=$(bdir)/%.o)
src/delta.c +4 -7
@@ -358,22 +358,19 @@ int traceeval_delta_stop_size(struct traceeval *teval,static int create_delta_iter_array (struct traceeval_iterator *iter) {struct traceeval *teval = iter->teval; -struct hash_table *hist = teval->hist; -struct hash_iter *hiter; -struct hash_item *item; +struct teval_rbtree_node *node =NULL ;size_t ts_idx = teval->nr_val_types -1 ;size_t idx =0 ; -int i; - iter->nr_entries =hash_nr_items (hist); + iter->nr_entries = teval->tree.nr_nodes; iter->entries =calloc (iter->nr_entries,sizeof (*iter->entries));if (!iter->entries) {teval_print_err (TEVAL_CRIT ,"Failed to allocate entries" );return -1 ; } -for (i =0 , hiter =hash_iter_start (hist); (item =hash_iter_next (hiter)); i++) { - struct entry *entry = container_of(item, struct entry, hash); +while ((node =teval_rbtree_next (teval, node))) { + struct entry *entry = container_of(node, struct entry, node);/* Only add entries where the timestamp is non zero */ if (!entry->vals[ts_idx].number_64)
src/eval-local.h +32 -62
139 lines not on this page · raw message
src/hash.c deleted +0 -123
123 lines not on this page · raw message
src/histograms.c +57 -122
306 lines not on this page · raw message
src/rbtree.c new file +455 -0
455 lines not on this page · raw message
Signature · 1 line
2.42.0