Skip to content

Commit 5ae4c78

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 callsites 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 Same tendency applies to the count of instructions. Therefore 1-arg version is defined as a macro to the 2-arg one. On all 64-bit arches, the differences are negligible. See more discussion at openzfs#16483. Acked-by: Alexander Motin <[email protected]> Signed-off-by: Shengqi Chen <[email protected]>
1 parent ee56b4d commit 5ae4c78

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

include/cityhash.h

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

35+
/*
36+
* We have 2/3-argument specialized versions of cityhash4,
37+
* which can reduce instruction count and stack usage on some 32-bit arches.
38+
* For 1-arg version, using cityhash2 is enough.
39+
*/
40+
#define cityhash1(w) (cityhash2(w, 0))
41+
_SYS_CITYHASH_H uint64_t cityhash2(uint64_t, uint64_t);
42+
_SYS_CITYHASH_H uint64_t cityhash3(uint64_t, uint64_t, uint64_t);
3543
_SYS_CITYHASH_H uint64_t cityhash4(uint64_t, uint64_t, uint64_t, uint64_t);
3644

3745
#ifdef __cplusplus

lib/libzfs/libzfs.abi

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@
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='cityhash2' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
157+
<elf-symbol name='cityhash3' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
156158
<elf-symbol name='cityhash4' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
157159
<elf-symbol name='color_end' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
158160
<elf-symbol name='color_start' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
@@ -9179,6 +9181,17 @@
91799181
</function-decl>
91809182
</abi-instr>
91819183
<abi-instr address-size='64' path='module/zcommon/cityhash.c' language='LANG_C99'>
9184+
<function-decl name='cityhash2' mangled-name='cityhash2' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash2'>
9185+
<parameter type-id='9c313c2d' name='w1'/>
9186+
<parameter type-id='9c313c2d' name='w2'/>
9187+
<return type-id='9c313c2d'/>
9188+
</function-decl>
9189+
<function-decl name='cityhash3' mangled-name='cityhash3' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash3'>
9190+
<parameter type-id='9c313c2d' name='w1'/>
9191+
<parameter type-id='9c313c2d' name='w2'/>
9192+
<parameter type-id='9c313c2d' name='w3'/>
9193+
<return type-id='9c313c2d'/>
9194+
</function-decl>
91829195
<function-decl name='cityhash4' mangled-name='cityhash4' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash4'>
91839196
<parameter type-id='9c313c2d' name='w1'/>
91849197
<parameter type-id='9c313c2d' name='w2'/>

module/zcommon/cityhash.c

Lines changed: 21 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,28 @@ 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+
}
63+
64+
uint64_t
65+
cityhash2(uint64_t w1, uint64_t w2)
66+
{
67+
return (cityhash_impl(w1, w2, 0, 0));
68+
}
6269

70+
uint64_t
71+
cityhash3(uint64_t w1, uint64_t w2, uint64_t w3)
72+
{
73+
return (cityhash_impl(w1, w2, w3, 0));
74+
}
75+
76+
uint64_t
77+
cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
78+
{
79+
return (cityhash_impl(w1, w2, w3, w4));
6380
}
6481

6582
#if defined(_KERNEL)
83+
EXPORT_SYMBOL(cityhash2);
84+
EXPORT_SYMBOL(cityhash3);
6685
EXPORT_SYMBOL(cityhash4);
6786
#endif

0 commit comments

Comments
 (0)