Skip to content

Commit

Permalink
btable: optimize entry address calculation
Browse files Browse the repository at this point in the history
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 <matan@mellanox.com>
  • Loading branch information
matan committed Nov 25, 2018
1 parent 687637e commit 2d7661a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
18 changes: 18 additions & 0 deletions dp-core/vr_btable.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 13 additions & 3 deletions include/vr_btable.h
Expand Up @@ -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;
};
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 2d7661a

Please sign in to comment.