Skip to content

Commit

Permalink
zcommon: add specialized versions of cityhash4
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
Harry-Chen committed Sep 9, 2024
1 parent ee56b4d commit 7065e45
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
7 changes: 7 additions & 0 deletions include/cityhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
extern "C" {
#endif

/*
* Define 1/2/3-argument specialized versions of cityhash4, which can reduce
* instruction count (especially multiplication) on some 32-bit arches.
*/
_SYS_CITYHASH_H uint64_t cityhash1(uint64_t);
_SYS_CITYHASH_H uint64_t cityhash2(uint64_t, uint64_t);
_SYS_CITYHASH_H uint64_t cityhash3(uint64_t, uint64_t, uint64_t);
_SYS_CITYHASH_H uint64_t cityhash4(uint64_t, uint64_t, uint64_t, uint64_t);

#ifdef __cplusplus
Expand Down
18 changes: 18 additions & 0 deletions lib/libzfs/libzfs.abi
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@
<elf-symbol name='avl_update_lt' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='avl_walk' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='bookmark_namecheck' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='cityhash1' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='cityhash2' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='cityhash3' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='cityhash4' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='color_end' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
<elf-symbol name='color_start' type='func-type' binding='global-binding' visibility='default-visibility' is-defined='yes'/>
Expand Down Expand Up @@ -9179,6 +9182,21 @@
</function-decl>
</abi-instr>
<abi-instr address-size='64' path='module/zcommon/cityhash.c' language='LANG_C99'>
<function-decl name='cityhash1' mangled-name='cityhash1' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash1'>
<parameter type-id='9c313c2d' name='w'/>
<return type-id='9c313c2d'/>
</function-decl>
<function-decl name='cityhash2' mangled-name='cityhash2' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash2'>
<parameter type-id='9c313c2d' name='w1'/>
<parameter type-id='9c313c2d' name='w2'/>
<return type-id='9c313c2d'/>
</function-decl>
<function-decl name='cityhash3' mangled-name='cityhash3' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash3'>
<parameter type-id='9c313c2d' name='w1'/>
<parameter type-id='9c313c2d' name='w2'/>
<parameter type-id='9c313c2d' name='w3'/>
<return type-id='9c313c2d'/>
</function-decl>
<function-decl name='cityhash4' mangled-name='cityhash4' visibility='default' binding='global' size-in-bits='64' elf-symbol-id='cityhash4'>
<parameter type-id='9c313c2d' name='w1'/>
<parameter type-id='9c313c2d' name='w2'/>
Expand Down
33 changes: 31 additions & 2 deletions module/zcommon/cityhash.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ cityhash_helper(uint64_t u, uint64_t v, uint64_t mul)
return (b);
}

uint64_t
cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
static inline uint64_t
cityhash_impl(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
{
uint64_t mul = HASH_K2 + 64;
uint64_t a = w1 * HASH_K1;
Expand All @@ -59,9 +59,38 @@ cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
uint64_t d = w3 * HASH_K2;
return (cityhash_helper(rotate(a + b, 43) + rotate(c, 30) + d,
a + rotate(b + HASH_K2, 18) + c, mul));
}

/*
* Passing w as the 2nd argument could save one 64-bit multiplication.
*/
uint64_t
cityhash1(uint64_t w)
{
return (cityhash_impl(0, w, 0, 0));
}

uint64_t
cityhash2(uint64_t w1, uint64_t w2)
{
return (cityhash_impl(w1, w2, 0, 0));
}

uint64_t
cityhash3(uint64_t w1, uint64_t w2, uint64_t w3)
{
return (cityhash_impl(w1, w2, w3, 0));
}

uint64_t
cityhash4(uint64_t w1, uint64_t w2, uint64_t w3, uint64_t w4)
{
return (cityhash_impl(w1, w2, w3, w4));
}

#if defined(_KERNEL)
EXPORT_SYMBOL(cityhash1);
EXPORT_SYMBOL(cityhash2);
EXPORT_SYMBOL(cityhash3);
EXPORT_SYMBOL(cityhash4);
#endif

0 comments on commit 7065e45

Please sign in to comment.