Skip to content

Commit 7b580dc

Browse files
committed
zcommon: add specialized versions of cityhash4
Specializing cityhash4 on 32-bit architectures can reduce the size of stack frames as well as instruction count. This is a tiny but useful optimization, since some callers invoke it frequently. When specializing into 1/2/3/4-arg versions, the stack usage (in bytes) on some 32-bit arches are listed as follows: - x86: 32, 32, 32, 40 - arm-v7a: 20, 20, 28, 36 - riscv: 0, 0, 0, 16 - power: 16, 16, 16, 32 - mipsel: 8, 8, 8, 24 And each actual argument (even if passing 0) contributes evenly to the number of multiplication instructions generated: - x86: 9, 12, 15 ,18 - arm-v7a: 6, 8, 10, 12 - riscv / power: 12, 18, 20, 24 - mipsel: 9, 12, 15, 19 On 64-bit architectures, the tendencies are similar. But both stack sizes and instruction counts are significantly smaller thus negligible. See more discussion at #16483. Acked-by: Alexander Motin <[email protected]> Signed-off-by: Shengqi Chen <[email protected]>
1 parent 81591ac commit 7b580dc

File tree

3 files changed

+56
-2
lines changed

3 files changed

+56
-2
lines changed

include/cityhash.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@
3232
extern "C" {
3333
#endif
3434

35+
/*
36+
* Define 1/2/3-argument specialized versions of cityhash4, which can reduce
37+
* instruction count (especially multiplication) on some 32-bit arches.
38+
*/
39+
_SYS_CITYHASH_H uint64_t cityhash1(uint64_t);
40+
_SYS_CITYHASH_H uint64_t cityhash2(uint64_t, uint64_t);
41+
_SYS_CITYHASH_H uint64_t cityhash3(uint64_t, uint64_t, uint64_t);
3542
_SYS_CITYHASH_H uint64_t cityhash4(uint64_t, uint64_t, uint64_t, uint64_t);
3643

3744
#ifdef __cplusplus

lib/libzfs/libzfs.abi

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@
153153
<elf-symbol name='avl_update_lt' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
154154
<elf-symbol name='avl_walk' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
155155
<elf-symbol name='bookmark_namecheck' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
156+
<elf-symbol name='cityhash1' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
157+
<elf-symbol name='cityhash2' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
158+
<elf-symbol name='cityhash3' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
156159
<elf-symbol name='cityhash4' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
157160
<elf-symbol name='color_end' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
158161
<elf-symbol name='color_start' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
@@ -9179,6 +9182,21 @@
91799182
</function-decl>
91809183
</abi-instr>
91819184
<abi-instr address-size='64' path='module/zcommon/cityhash.c' language='LANG_C99'>
9185+
<function-decl name='cityhash1' mangled-name='cityhash1' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash1'>
9186+
<parameter type-id='9c313c2d' name='w'/>
9187+
<return type-id='9c313c2d'/>
9188+
</function-decl>
9189+
<function-decl name='cityhash2' mangled-name='cityhash2' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash2'>
9190+
<parameter type-id='9c313c2d' name='w1'/>
9191+
<parameter type-id='9c313c2d' name='w2'/>
9192+
<return type-id='9c313c2d'/>
9193+
</function-decl>
9194+
<function-decl name='cityhash3' mangled-name='cityhash3' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash3'>
9195+
<parameter type-id='9c313c2d' name='w1'/>
9196+
<parameter type-id='9c313c2d' name='w2'/>
9197+
<parameter type-id='9c313c2d' name='w3'/>
9198+
<return type-id='9c313c2d'/>
9199+
</function-decl>
91829200
<function-decl name='cityhash4' mangled-name='cityhash4' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash4'>
91839201
<parameter type-id='9c313c2d' name='w1'/>
91849202
<parameter type-id='9c313c2d' name='w2'/>

module/zcommon/cityhash.c

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ cityhash_helper(uint64_t u, uint64_t v, uint64_t mul)
4949
return (b);
5050
}
5151

52-
uint64_t
53-
cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
52+
static inline uint64_t
53+
cityhash_impl(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
5454
{
5555
uint64_t mul = HASH_K2 + 64;
5656
uint64_t a = w1 * HASH_K1;
@@ -59,9 +59,38 @@ cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
5959
uint64_t d = w3 * HASH_K2;
6060
return (cityhash_helper(rotate(a + b, 43) + rotate(c, 30) + d,
6161
a + rotate(b + HASH_K2, 18) + c, mul));
62+
}
6263

64+
/*
65+
* Passing w as the 2nd argument could save one 64-bit multiplication.
66+
*/
67+
uint64_t
68+
cityhash1(uint64_t w)
69+
{
70+
return (cityhash_impl(0, w, 0, 0));
71+
}
72+
73+
uint64_t
74+
cityhash2(uint64_t w1, uint64_t w2)
75+
{
76+
return (cityhash_impl(w1, w2, 0, 0));
77+
}
78+
79+
uint64_t
80+
cityhash3(uint64_t w1, uint64_t w2, uint64_t w3)
81+
{
82+
return (cityhash_impl(w1, w2, w3, 0));
83+
}
84+
85+
uint64_t
86+
cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
87+
{
88+
return (cityhash_impl(w1, w2, w3, w4));
6389
}
6490

6591
#if defined(_KERNEL)
92+
EXPORT_SYMBOL(cityhash1);
93+
EXPORT_SYMBOL(cityhash2);
94+
EXPORT_SYMBOL(cityhash3);
6695
EXPORT_SYMBOL(cityhash4);
6796
#endif

0 commit comments

Comments
 (0)