mirror of
https://github.com/git/git
synced 2024-08-27 11:39:22 +00:00
![Jeff King](/assets/img/avatar_default.png)
Our put_be32() routine and its variants (get_be32(), put_be64(), etc) has two implementations: on some platforms we cast memory in place and use nothl()/htonl(), which can cause unaligned memory access. And on others, we pick out the individual bytes using bitshifts. This introduces extra complexity, and sometimes causes compilers to generate warnings about type-punning. And it's not clear there's any performance advantage. This split goes back to660231aa97
(block-sha1: support for architectures with memory alignment restrictions, 2009-08-12). The unaligned versions were part of the original block-sha1 code ind7c208a92e
(Add new optimized C 'block-sha1' routines, 2009-08-05), which says it is: Based on the mozilla SHA1 routine, but doing the input data accesses a word at a time and with 'htonl()' instead of loading bytes and shifting. Back then, Linus provided timings versus the mozilla code which showed a 27% improvement: https://lore.kernel.org/git/alpine.LFD.2.01.0908051545000.3390@localhost.localdomain/ However, the unaligned loads were either not the useful part of that speedup, or perhaps compilers and processors have changed since then. Here are times for computing the sha1 of 4GB of random data, with and without -DNO_UNALIGNED_LOADS (and BLK_SHA1=1, of course). This is with gcc 10, -O2, and the processor is a Core i9-9880H. [stock] Benchmark #1: t/helper/test-tool sha1 <foo.rand Time (mean ± σ): 6.638 s ± 0.081 s [User: 6.269 s, System: 0.368 s] Range (min … max): 6.550 s … 6.841 s 10 runs [-DNO_UNALIGNED_LOADS] Benchmark #1: t/helper/test-tool sha1 <foo.rand Time (mean ± σ): 6.418 s ± 0.015 s [User: 6.058 s, System: 0.360 s] Range (min … max): 6.394 s … 6.447 s 10 runs And here's the same test run on an AMD A8-7600, using gcc 8. [stock] Benchmark #1: t/helper/test-tool sha1 <foo.rand Time (mean ± σ): 11.721 s ± 0.113 s [User: 10.761 s, System: 0.951 s] Range (min … max): 11.509 s … 11.861 s 10 runs [-DNO_UNALIGNED_LOADS] Benchmark #1: t/helper/test-tool sha1 <foo.rand Time (mean ± σ): 11.744 s ± 0.066 s [User: 10.807 s, System: 0.928 s] Range (min … max): 11.637 s … 11.863 s 10 runs So the unaligned loads don't seem to help much, and actually make things worse. It's possible there are platforms where they provide more benefit, but: - the non-x86 platforms for which we use this code are old and obscure (powerpc and s390). - the main caller that cares about performance is block-sha1. But these days it is rarely used anyway, in favor of sha1dc (which is already much slower, and nobody seems to have cared that much). Let's just drop unaligned versions entirely in the name of simplicity. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
194 lines
4.3 KiB
C
194 lines
4.3 KiB
C
#ifndef COMPAT_BSWAP_H
|
|
#define COMPAT_BSWAP_H
|
|
|
|
/*
|
|
* Let's make sure we always have a sane definition for ntohl()/htonl().
|
|
* Some libraries define those as a function call, just to perform byte
|
|
* shifting, bringing significant overhead to what should be a simple
|
|
* operation.
|
|
*/
|
|
|
|
/*
|
|
* Default version that the compiler ought to optimize properly with
|
|
* constant values.
|
|
*/
|
|
static inline uint32_t default_swab32(uint32_t val)
|
|
{
|
|
return (((val & 0xff000000) >> 24) |
|
|
((val & 0x00ff0000) >> 8) |
|
|
((val & 0x0000ff00) << 8) |
|
|
((val & 0x000000ff) << 24));
|
|
}
|
|
|
|
static inline uint64_t default_bswap64(uint64_t val)
|
|
{
|
|
return (((val & (uint64_t)0x00000000000000ffULL) << 56) |
|
|
((val & (uint64_t)0x000000000000ff00ULL) << 40) |
|
|
((val & (uint64_t)0x0000000000ff0000ULL) << 24) |
|
|
((val & (uint64_t)0x00000000ff000000ULL) << 8) |
|
|
((val & (uint64_t)0x000000ff00000000ULL) >> 8) |
|
|
((val & (uint64_t)0x0000ff0000000000ULL) >> 24) |
|
|
((val & (uint64_t)0x00ff000000000000ULL) >> 40) |
|
|
((val & (uint64_t)0xff00000000000000ULL) >> 56));
|
|
}
|
|
|
|
#undef bswap32
|
|
#undef bswap64
|
|
|
|
#if defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
|
|
|
|
#define bswap32 git_bswap32
|
|
static inline uint32_t git_bswap32(uint32_t x)
|
|
{
|
|
uint32_t result;
|
|
if (__builtin_constant_p(x))
|
|
result = default_swab32(x);
|
|
else
|
|
__asm__("bswap %0" : "=r" (result) : "0" (x));
|
|
return result;
|
|
}
|
|
|
|
#define bswap64 git_bswap64
|
|
#if defined(__x86_64__)
|
|
static inline uint64_t git_bswap64(uint64_t x)
|
|
{
|
|
uint64_t result;
|
|
if (__builtin_constant_p(x))
|
|
result = default_bswap64(x);
|
|
else
|
|
__asm__("bswap %q0" : "=r" (result) : "0" (x));
|
|
return result;
|
|
}
|
|
#else
|
|
static inline uint64_t git_bswap64(uint64_t x)
|
|
{
|
|
union { uint64_t i64; uint32_t i32[2]; } tmp, result;
|
|
if (__builtin_constant_p(x))
|
|
result.i64 = default_bswap64(x);
|
|
else {
|
|
tmp.i64 = x;
|
|
result.i32[0] = git_bswap32(tmp.i32[1]);
|
|
result.i32[1] = git_bswap32(tmp.i32[0]);
|
|
}
|
|
return result.i64;
|
|
}
|
|
#endif
|
|
|
|
#elif defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
|
|
|
|
#include <stdlib.h>
|
|
|
|
#define bswap32(x) _byteswap_ulong(x)
|
|
#define bswap64(x) _byteswap_uint64(x)
|
|
|
|
#endif
|
|
|
|
#if defined(bswap32)
|
|
|
|
#undef ntohl
|
|
#undef htonl
|
|
#define ntohl(x) bswap32(x)
|
|
#define htonl(x) bswap32(x)
|
|
|
|
#endif
|
|
|
|
#if defined(bswap64)
|
|
|
|
#undef ntohll
|
|
#undef htonll
|
|
#define ntohll(x) bswap64(x)
|
|
#define htonll(x) bswap64(x)
|
|
|
|
#else
|
|
|
|
#undef ntohll
|
|
#undef htonll
|
|
|
|
#if defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && defined(__BIG_ENDIAN)
|
|
|
|
# define GIT_BYTE_ORDER __BYTE_ORDER
|
|
# define GIT_LITTLE_ENDIAN __LITTLE_ENDIAN
|
|
# define GIT_BIG_ENDIAN __BIG_ENDIAN
|
|
|
|
#elif defined(BYTE_ORDER) && defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN)
|
|
|
|
# define GIT_BYTE_ORDER BYTE_ORDER
|
|
# define GIT_LITTLE_ENDIAN LITTLE_ENDIAN
|
|
# define GIT_BIG_ENDIAN BIG_ENDIAN
|
|
|
|
#else
|
|
|
|
# define GIT_BIG_ENDIAN 4321
|
|
# define GIT_LITTLE_ENDIAN 1234
|
|
|
|
# if defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
|
|
# define GIT_BYTE_ORDER GIT_BIG_ENDIAN
|
|
# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
|
|
# define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
|
|
# elif defined(__THW_BIG_ENDIAN__) && !defined(__THW_LITTLE_ENDIAN__)
|
|
# define GIT_BYTE_ORDER GIT_BIG_ENDIAN
|
|
# elif defined(__THW_LITTLE_ENDIAN__) && !defined(__THW_BIG_ENDIAN__)
|
|
# define GIT_BYTE_ORDER GIT_LITTLE_ENDIAN
|
|
# else
|
|
# error "Cannot determine endianness"
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#if GIT_BYTE_ORDER == GIT_BIG_ENDIAN
|
|
# define ntohll(n) (n)
|
|
# define htonll(n) (n)
|
|
#else
|
|
# define ntohll(n) default_bswap64(n)
|
|
# define htonll(n) default_bswap64(n)
|
|
#endif
|
|
|
|
#endif
|
|
|
|
static inline uint16_t get_be16(const void *ptr)
|
|
{
|
|
const unsigned char *p = ptr;
|
|
return (uint16_t)p[0] << 8 |
|
|
(uint16_t)p[1] << 0;
|
|
}
|
|
|
|
static inline uint32_t get_be32(const void *ptr)
|
|
{
|
|
const unsigned char *p = ptr;
|
|
return (uint32_t)p[0] << 24 |
|
|
(uint32_t)p[1] << 16 |
|
|
(uint32_t)p[2] << 8 |
|
|
(uint32_t)p[3] << 0;
|
|
}
|
|
|
|
static inline uint64_t get_be64(const void *ptr)
|
|
{
|
|
const unsigned char *p = ptr;
|
|
return (uint64_t)get_be32(&p[0]) << 32 |
|
|
(uint64_t)get_be32(&p[4]) << 0;
|
|
}
|
|
|
|
static inline void put_be32(void *ptr, uint32_t value)
|
|
{
|
|
unsigned char *p = ptr;
|
|
p[0] = value >> 24;
|
|
p[1] = value >> 16;
|
|
p[2] = value >> 8;
|
|
p[3] = value >> 0;
|
|
}
|
|
|
|
static inline void put_be64(void *ptr, uint64_t value)
|
|
{
|
|
unsigned char *p = ptr;
|
|
p[0] = value >> 56;
|
|
p[1] = value >> 48;
|
|
p[2] = value >> 40;
|
|
p[3] = value >> 32;
|
|
p[4] = value >> 24;
|
|
p[5] = value >> 16;
|
|
p[6] = value >> 8;
|
|
p[7] = value >> 0;
|
|
}
|
|
|
|
#endif /* COMPAT_BSWAP_H */
|