Skip to contentSkip to conversation

flex_array: avoid divisions when accessing elements

PATCH In review Eric Paris 3 participants 3 messages May 2, 2011 Library code

lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/1 flex_array: avoid divisions when accessing elements

From: Jesse Gross <[email protected]>

On most architectures division is an expensive operation and accessing an element currently requires four of them. This performance penalty effectively precludes flex arrays from being used on any kind of fast path. However, two of these divisions can be handled at creation time and the others can be replaced by a reciprocal divide, completely avoiding real divisions on access.

Cc Dave Hansen <[email protected]>
Cc David Rientjes <[email protected]>
Cc Eric Paris <[email protected]>
[rebase on top of changes to support 0 len elements: eparis]
[initialize part_nr when array fits entirely in base: eparis]
---
 include/linux/flex_array.h |    2 +
 lib/flex_array.c           |   51 +++++++++++++++++++++++++-------------------
 2 files changed, 31 insertions(+), 22 deletions(-)
include/linux/flex_array.h +2 -0
index ebeb2f3..6843cf1 100644
@@ -21,6 +21,8 @@ struct flex_array {
 		struct {
 			int element_size;
 			int total_nr_elements;
+			int elems_per_part;
+			u32 reciprocal_elems;
 			struct flex_array_part *parts[];
 		};
 		/*
lib/flex_array.c +29 -22

128 lines not on this page · raw message

Signature · 1 line
1.7.1
Acked-by on v1 1/1
#1 lore raw
Quoted 7 lines · Eric Paris

On most architectures division is an expensive operation and accessing an element currently requires four of them. This performance penalty effectively precludes flex arrays from being used on any kind of fast path. However, two of these divisions can be handled at creation time and the others can be replaced by a reciprocal divide, completely avoiding real divisions on access.

Quoted 7 lines
Cc David Rientjes <[email protected]>
Cc Eric Paris <[email protected]>
[rebase on top of changes to support 0 len elements: eparis]
[initialize part_nr when array fits entirely in base: eparis]

This seems sane to me, especially in the element access case.

-- Dave

#2 lore raw
Quoted 16 lines · Dave Hansen
Quoted 7 lines · Eric Paris

On most architectures division is an expensive operation and accessing an element currently requires four of them.  This performance penalty effectively precludes flex arrays from being used on any kind of fast path.  However, two of these divisions can be handled at creation time and the others can be replaced by a reciprocal divide, completely avoiding real divisions on access.

Quoted 7 lines
Cc David Rientjes <[email protected]>
Cc Eric Paris <[email protected]>
[rebase on top of changes to support 0 len elements: eparis]
[initialize part_nr when array fits entirely in base: eparis]
Quoted 3 lines

This seems sane to me, especially in the element access case.

Thanks Eric!