flex_array: avoid divisions when accessing elements
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.
--- 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
@@ -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
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
Signed-off-by Jesse Gross <[email protected]>Cc David Rientjes <[email protected]>Cc Eric Paris <[email protected]>Signed-off-by Andrew Morton <[email protected]>[rebase on top of changes to support 0 len elements: eparis][initialize part_nr when array fits entirely in base: eparis]Signed-off-by Eric Paris <[email protected]>
This seems sane to me, especially in the element access case.
-- Dave
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
Signed-off-by Jesse Gross <[email protected]>Cc David Rientjes <[email protected]>Cc Eric Paris <[email protected]>Signed-off-by Andrew Morton <[email protected]>[rebase on top of changes to support 0 len elements: eparis][initialize part_nr when array fits entirely in base: eparis]Signed-off-by Eric Paris <[email protected]>
Quoted 3 lines
This seems sane to me, especially in the element access case.
Acked-by Dave Hansen <[email protected]>
Thanks Eric!