From 2d7661a949ad209fc80eb6d658482759f8f44752 Mon Sep 17 00:00:00 2001 From: matan Date: Wed, 4 Jul 2018 04:34:19 +0000 Subject: [PATCH] btable: optimize entry address calculation The btable uses division and modulo calculations, which are quiet expensive in the most platforms, for the entry address calculations. In case the allocation limit of a btable is power of 2 number, the calculations can be optimized to use simple bit calculations. Optimize the entry address calculations in the aforementioned case. Partial-Bug: #1781402 Change-Id: Idfc56e14a6d499d23a306866cfa463f95eee1151 Signed-off-by: Matan Azrad --- dp-core/vr_btable.c | 18 ++++++++++++++++++ include/vr_btable.h | 16 +++++++++++++--- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/dp-core/vr_btable.c b/dp-core/vr_btable.c index 8dbcb5199..903a4db76 100644 --- a/dp-core/vr_btable.c +++ b/dp-core/vr_btable.c @@ -87,6 +87,20 @@ vr_btable_free(struct vr_btable *table) return; } +static void +vr_btable_fill_pow2_fields(struct vr_btable *table) { + unsigned int number = table->vb_alloc_limit; + + if (!(table->vb_alloc_limit & (table->vb_alloc_limit - 1))) { + while (number >>= 1) + table->vb_alloc_limit_log++; + table->vb_alloc_limit_mask = table->vb_alloc_limit - 1; + } else { + table->vb_alloc_limit_log = 0; + table->vb_alloc_limit_mask = 0; + } +} + struct vr_btable * vr_btable_alloc(unsigned int num_entries, unsigned int entry_size) { @@ -160,6 +174,8 @@ vr_btable_alloc(unsigned int num_entries, unsigned int entry_size) table->vb_entries = num_entries; table->vb_esize = entry_size; + vr_btable_fill_pow2_fields(table); + return table; exit_alloc: @@ -217,6 +233,8 @@ vr_btable_attach(struct iovec *iov, unsigned int iov_len, table->vb_entries = (total_size / esize); table->vb_flags |= VB_FLAG_MEMORY_ATTACHED; + vr_btable_fill_pow2_fields(table); + return table; error: diff --git a/include/vr_btable.h b/include/vr_btable.h index 77e795cca..b27383ac4 100644 --- a/include/vr_btable.h +++ b/include/vr_btable.h @@ -21,6 +21,8 @@ struct vr_btable { unsigned short vb_partitions; unsigned int vb_flags; unsigned int vb_alloc_limit; + unsigned short vb_alloc_limit_log; + unsigned int vb_alloc_limit_mask; void **vb_mem; struct vr_btable_partition *vb_table_info; }; @@ -48,13 +50,21 @@ vr_btable_size(struct vr_btable *table) static inline void * vr_btable_get(struct vr_btable *table, unsigned int entry) { - unsigned int t_index, t_offset; + unsigned int t_index, t_offset, entry_cont; if (entry >= table->vb_entries) return NULL; - t_index = (entry * table->vb_esize) / table->vb_alloc_limit; - t_offset = (entry * table->vb_esize) % table->vb_alloc_limit; + entry_cont = entry * table->vb_esize; + + if (table->vb_alloc_limit_mask) { + t_index = entry_cont >> table->vb_alloc_limit_log; + t_offset = entry_cont & table->vb_alloc_limit_mask; + } else { + t_index = entry_cont / table->vb_alloc_limit; + t_offset = entry_cont % table->vb_alloc_limit; + } + if (t_index >= table->vb_partitions) return NULL;