refs.c: SSE4.2 optimizations for check_refname_component
v7 cover refs.c: SSE4.2 optimizations for check_refname_component
Since Junio has picked up the first patch from previous versions of this series, I'm just going to send the second (SSE) one. I decided not to s/NO_SSE42/!HAVE_SSE42/ because it looks like git mostly uses the former convention (for instance, that's what GIT_PARSE_WITH generates).
Thanks for all of the comments so far.
PATCH v7 1/1 refs.c: SSE4.2 optimizations for check_refname_component
Optimize check_refname_component using SSE4.2, where available.
git rev-parse HEAD is a good test-case for this, since it does almost nothing except parse refs. For one particular repo with about 60k refs, almost all packed, the timings are:
Look up table: 29 ms SSE4.2: 25 ms
This is about a 15% improvement.
The configure.ac changes include code from the GNU C Library written by Joseph S. Myers <joseph at codesourcery dot com>.
Only supports GCC and Clang at present, because C interfaces to the cpuid instruction are not well-standardized.
--- Makefile | 6 +++ aclocal.m4 | 6 +++ compat/cpuid.h | 25 +++++++++ configure.ac | 17 ++++++ git-compat-util.h | 23 ++++++++ refs.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++------ t/t5511-refspec.sh | 13 +++++ 7 files changed, 225 insertions(+), 15 deletions(-) create mode 100644 compat/cpuid.h
Makefile +6 -0
@@ -1326,6 +1326,11 @@ else COMPAT_OBJS += compat/win32mmap.o endif endif +ifdef NO_SSE42 + BASIC_CFLAGS += -DNO_SSE42 +else + BASIC_CFLAGS += -msse4.2 +endif ifdef OBJECT_CREATION_USES_RENAMES COMPAT_CFLAGS += -DOBJECT_CREATION_MODE=1 endif
@@ -2199,6 +2204,7 @@ GIT-BUILD-OPTIONS: FORCE @echo NO_PERL=\''$(subst ','\'',$(subst ','\'',$(NO_PERL)))'\' >>$@ @echo NO_PYTHON=\''$(subst ','\'',$(subst ','\'',$(NO_PYTHON)))'\' >>$@ @echo NO_UNIX_SOCKETS=\''$(subst ','\'',$(subst ','\'',$(NO_UNIX_SOCKETS)))'\' >>$@ + @echo NO_SSE42=\''$(subst ','\'',$(subst ','\'',$(NO_SSE42)))'\' >>$@ ifdef TEST_OUTPUT_DIRECTORY @echo TEST_OUTPUT_DIRECTORY=\''$(subst ','\'',$(subst ','\'',$(TEST_OUTPUT_DIRECTORY)))'\' >>$@ endif
aclocal.m4 +6 -0
@@ -38,3 +38,9 @@ AC_DEFUN([TYPE_SOCKLEN_T], [#include <sys/types.h> #include <sys/socket.h>]) ]) + +dnl Test a compiler option or options with an empty input file. +dnl LIBC_TRY_CC_OPTION([options], [action-if-true], [action-if-false]) +AC_DEFUN([LIBC_TRY_CC_OPTION], +[AS_IF([AC_TRY_COMMAND([${CC-cc} $1 -xc /dev/null -S -o /dev/null])], + [$2], [$3])])
compat/cpuid.h new file +25 -0
@@ -0,0 +1,25 @@ +#ifndef COMPAT_CPUID_H +#define COMPAT_CPUID_H + +#if (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))) +#include <cpuid.h> +#define CPUID_SSE42_BIT (1 << 20) + +static int processor_supports_sse42 (void ) +{ +unsigned eax, ebx, ecx, edx; +__cpuid (1 , eax, ebx, ecx, edx); +return ecx &CPUID_SSE42_BIT ; +} + +#else + +static int processor_supports_sse42 (void ) +{ +/* TODO: handle CPUID on non-GCC compilers */ +return 0 ; +} + +#endif + +#endif /* COMPAT_CPUID_H */
configure.ac +17 -0
@@ -382,6 +382,23 @@ AS_HELP_STRING([],[Tcl/Tk interpreter will be found in a system.]), GIT_PARSE_WITH(tcltk)) # +# Declare the with-sse42/without-sse42 options. +AC_ARG_WITH(sse42, +AS_HELP_STRING([--with-sse42],[use SSE4.2 instructions]) +AS_HELP_STRING([],[(default is YES if your compiler supports -msse4.2)]), +GIT_PARSE_WITH(sse42)) + +if test "$NO_SSE42" != "YesPlease"; then + dnl Check if -msse4.2 works. + AC_CACHE_CHECK(for SSE4.2 support, cc_cv_sse42, [dnl + LIBC_TRY_CC_OPTION([-msse4.2], [cc_cv_sse42=yes], [cc_cv_sse42=no]) + ]) + if test $cc_cv_sse42 = no; then + NO_SSE42=1 + fi +fi + +GIT_CONF_SUBST([NO_SSE42]) ## Checks for programs. AC_MSG_NOTICE([CHECKS for programs])
git-compat-util.h +23 -0
@@ -668,6 +668,29 @@ void git_qsort(void *base, size_t nmemb, size_t size,#endif #endif +#ifndef NO_SSE42 +#include <nmmintrin.h> +#include "compat/cpuid.h" +/* +* Clang ships with a version of nmmintrin.h that's incomplete; if +* necessary, we define the constants that we're going to use. +*/ +#ifndef _SIDD_UBYTE_OPS +#define _SIDD_UBYTE_OPS 0x00 +#define _SIDD_CMP_EQUAL_ANY 0x00 +#define _SIDD_CMP_RANGES 0x04 +#define _SIDD_CMP_EQUAL_ORDERED 0x0c +#define _SIDD_NEGATIVE_POLARITY 0x10 +#endif + +/* This is the system memory page size; it's used so that we can read +* outside the bounds of an allocation without segfaulting. +*/ +#ifndef PAGE_SIZE +#define PAGE_SIZE 4096 +#endif +#endif +#ifdef UNRELIABLE_FSTAT #define fstat_is_reliable () 0#else
refs.c +135 -15
179 lines not on this page · raw message
t/t5511-refspec.sh +13 -0
@@ -88,4 +88,17 @@ test_refspec fetch "refs/heads/${good}" bad=$(printf '\011tab') test_refspec fetch "refs/heads/${bad}" invalid +test_refspec fetch 'refs/heads/a-very-long-refname' +test_refspec fetch 'refs/heads/.a-very-long-refname' invalid +test_refspec fetch 'refs/heads/abcdefgh0123..' invalid +test_refspec fetch 'refs/heads/abcdefgh01234..' invalid +test_refspec fetch 'refs/heads/abcdefgh012345..' invalid +test_refspec fetch 'refs/heads/abcdefgh0123456..' invalid +test_refspec fetch 'refs/heads/abcdefgh01234567..' invalid +test_refspec fetch 'refs/heads/abcdefgh0123.a' +test_refspec fetch 'refs/heads/abcdefgh01234.a' +test_refspec fetch 'refs/heads/abcdefgh012345.a' +test_refspec fetch 'refs/heads/abcdefgh0123456.a' +test_refspec fetch 'refs/heads/abcdefgh01234567.a' + test_done
Signature · 1 line
2.0.0.rc1.24.g0588c94.dirty
Quoted 9 lines · David Turner
Optimize
check_refname_componentusing SSE4.2, where available.git rev-parse HEAD is a good test-case for this, since it does almost nothing except parse refs. For one particular repo with about 60k refs, almost all packed, the timings are:
Look up table: 29 ms SSE4.2: 25 ms
Quoted 8 lines
This is about a 15% improvement.
The configure.ac changes include code from the GNU C Library written by Joseph S. Myers <joseph at codesourcery dot com>.
Only supports GCC and Clang at present, because C interfaces to the cpuid instruction are not well-standardized.
Still a SSE4.2 is not that useful, in most cases SSE2 is faster. Here I think that difference will not be that big when correctly implemented. That will avoid a runtime checks.
For parallelisation you need to take extra step and paralelize whole check than going component-by-component.
For detecting sequences a faster way is construct bitmasks with SSE2 so you could combine these. It avoids needing special casing on 16-byte boundaries.
Below is untested implementation where you could add a bad character check with SSE4.2 which would speed it up. Are refs mostly alphanumerical? If so we could speed this up by paralelized alnum check and handling other characters in slower path.
#include <stdint.h>
#include <emmintrin.h>
char bad_table[256]; // TODO
int bad_characters(unsigned char *x)
{
while (*x)
if (bad_table[*x++])
return -1;
return 0;
}int check_refname_skeleton(char *x)
{
if (bad_characters(x))
return -1;
__m128i slash = _mm_set1_epi8 ('/');
__m128i dot = _mm_set1_epi8 ('.');
__m128i char_l = _mm_set1_epi8 ('l');
__m128i at = _mm_set1_epi8 ('@');
__m128i brace = _mm_set1_epi8 ('{');
while (1) {
if (((uint64_t) x) & 4095 < 4096 - 17)
{
if (bytewise_check(x) != -2);
return bytewise_check(x);
x += 16;
}
__m128i v0 = _mm_loadu_si128 ((__m128i *)(x));
__m128i v1 = _mm_loadu_si128 ((__m128i *)(x + 1));
__m128i result;
// terminating 0
result = _mm_cmpeq_epi8(v0, _mm_set1_epi8('\000'));
// sequence ..
result = _mm_or_si128(result, _mm_and_si128 (_mm_cmpeq_epi8(v0, dot),
_mm_cmpeq_epi8(v1, dot)));
// sequence /.
result = _mm_or_si128(result, _mm_and_si128 (_mm_cmpeq_epi8(v0, slash),
_mm_cmpeq_epi8(v1, dot)));
// sequence //
result = _mm_or_si128(result, _mm_and_si128 (_mm_cmpeq_epi8(v0, slash),
_mm_cmpeq_epi8(v1, slash)));
// sequence .l
result = _mm_or_si128(result, _mm_and_si128 (_mm_cmpeq_epi8(v0, dot),
_mm_cmpeq_epi8(v1, char_l)));
// sequence @{
result = _mm_or_si128(result, _mm_and_si128 (_mm_cmpeq_epi8(v0, at),
_mm_cmpeq_epi8(v1, brace)));
uint64_t mask = _mm_movemask_epi8(result);
if (mask) {
char *p = x + __builtin_ctzl(mask);
if (!*p)
return 0;
else if (p[0] == '.' && p[1] == 'l')
if (bytewise_check(x) != -2)
return bytewise_check(x);
else
return -1;
}
x += 16;
}
}Quoted 15 lines · Ondřej Bílka
Quoted 14 lines · David Turner
Optimize
check_refname_componentusing SSE4.2, where available.git rev-parse HEAD is a good test-case for this, since it does almost nothing except parse refs. For one particular repo with about 60k refs, almost all packed, the timings are:
Look up table: 29 ms SSE4.2: 25 msThis is about a 15% improvement.
The configure.ac changes include code from the GNU C Library written by Joseph S. Myers <joseph at codesourcery dot com>.
Quoted 6 lines
Quoted 3 lines
Only supports GCC and Clang at present, because C interfaces to the cpuid instruction are not well-standardized.
Still a SSE4.2 is not that useful, in most cases SSE2 is faster. Here I think that difference will not be that big when correctly implemented. That will avoid a runtime checks.
Surprisingly to me, this is true! At least, on my machine. Sadly, the only way to make it avoid a runtime check is to exclude 32-bit machines (or to make the option non-default, which I would prefer not to do).
Quoted 2 lines
For parallelisation you need to take extra step and paralelize whole check than going component-by-component.
Good idea.
Quoted 3 lines
For detecting sequences a faster way is construct bitmasks with SSE2 so you could combine these. It avoids needing special casing on 16-byte boundaries.
That does seem to be faster.
Quoted 4 lines
Below is untested implementation where you could add a bad character check with SSE4.2 which would speed it up. Are refs mostly alphanumerical? If so we could speed this up by paralelized alnum check and handling other characters in slower path.
Twitter's are almost entirely in [-._/a-zA-Z0-9] -- there are only a handful of exceptions. So, a method that has some bycatch outside of this range is just as fast as the SSE4.2 bad character check (but somewhat more code).
Quoted 5 lines · David Turner
Since Junio has picked up the first patch from previous versions of this series, I'm just going to send the second (SSE) one. I decided not to s/NO_SSE42/!HAVE_SSE42/ because it looks like git mostly uses the former convention (for instance, that's what
GIT_PARSE_WITHgenerates).
Yeah but NO_FROTZ is used only when FROTZ is something everybody is expected to have (e.g. it's in posix, people ought to have it, but we do support those who don't), isn't it? For a very arch specific stuff like sse42, I'd feel better to make it purely opt-in by forcing people to explicitly say HAVE_SSE42 to enable it.
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · David Turner
Since Junio has picked up the first patch from previous versions of this series, I'm just going to send the second (SSE) one. I decided not to s/NO_SSE42/!HAVE_SSE42/ because it looks like git mostly uses the former convention (for instance, that's what
GIT_PARSE_WITHgenerates).
Quoted 5 lines
Yeah but
NO_FROTZis used only when FROTZ is something everybody is expected to have (e.g. it's in posix, people ought to have it, but we do support those who don't), isn't it? For a very arch specific stuff like sse42, I'd feel better to make it purely opt-in by forcing people to explicitly sayHAVE_SSE42to enable it.
The patch now has two kinds of autodetection:
1. At build-time, we check for the compiler supporting -msse4.2. If it does, and if the user has not explicitly done --without-sse, then we build with SSE support. This does not mean that the SSE code will necessarily be used because:
2. At run-time, if we have built with SSE support, we check cpuid to choose a version of the function that will run on the current CPU.
So I think we never hit a case where we try to use SSE and fail, which is the major reason I see to make it non-default.
To me, this means that we should not require people to explicitly request SSE, because we generally want to try to provide the most-efficient version of git that will work everywhere. In fact, I am not sure we need a --without-sse option at all, since all it saves is a cpuid instruction. But I don't need to remove the option, in case there's a use for it I'm not thinking of.
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · David Turner
Since Junio has picked up the first patch from previous versions of this series, I'm just going to send the second (SSE) one. I decided not to s/NO_SSE42/!HAVE_SSE42/ because it looks like git mostly uses the former convention (for instance, that's what
GIT_PARSE_WITHgenerates).
Quoted 5 lines
Yeah but
NO_FROTZis used only when FROTZ is something everybody is expected to have (e.g. it's in posix, people ought to have it, but we do support those who don't), isn't it? For a very arch specific stuff like sse42, I'd feel better to make it purely opt-in by forcing people to explicitly sayHAVE_SSE42to enable it.
Just FYI: I am getting
compat/cpuid.h:8:12: error: 'processor_supports_sse42' defined but not used [-Werror=unused-function]
cc1: all warnings being treated as errors
while building 'pu'; I'll have to rebuild 'pu' without this patch before I can push the day's result out.
Am 6/10/2014 1:05, schrieb Junio C Hamano:
Quoted 16 lines
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · David Turner
Since Junio has picked up the first patch from previous versions of this series, I'm just going to send the second (SSE) one. I decided not to s/NO_SSE42/!HAVE_SSE42/ because it looks like git mostly uses the former convention (for instance, that's what
GIT_PARSE_WITHgenerates).Quoted 5 lines
Yeah but
NO_FROTZis used only when FROTZ is something everybody is expected to have (e.g. it's in posix, people ought to have it, but we do support those who don't), isn't it? For a very arch specific stuff like sse42, I'd feel better to make it purely opt-in by forcing people to explicitly sayHAVE_SSE42to enable it.
Quoted 8 lines
Just FYI: I am getting
compat/cpuid.h:8:12: error: 'processor_supports_sse42' defined but not used [-Werror=unused-function]
cc1: all warnings being treated as errorswhile building 'pu'; I'll have to rebuild 'pu' without this patch before I can push the day's result out.
And I get this when I compile on Windows with msysgit:
CC abspath.o
In file included from git-compat-util.h:694,
from cache.h:4,
from abspath.c:1:compat/cpuid.h: In function 'processor_supports_sse42':compat/cpuid.h:11: warning: implicit declaration of function '__cpuid' abspath.c: At top level:compat/cpuid.h:8: warning: 'processor_supports_sse42' defined but not used abspath.c: In function 'processor_supports_sse42':compat/cpuid.h:11: warning: 'eax' is used uninitialized in this function compat/cpuid.h:11: warning: 'ebx' is used uninitialized in this function compat/cpuid.h:11: warning: 'ecx' is used uninitialized in this function compat/cpuid.h:11: warning: 'edx' is used uninitialized in this function
Perhaps our gcc is too old?
-- Hannes
Quoted 16 lines · Johannes Sixt
And I get this when I compile on Windows with msysgit:
CC abspath.o
In file included from git-compat-util.h:694,from cache.h:4, from abspath.c:1:
compat/cpuid.h: In function 'processor_supports_sse42':compat/cpuid.h:11: warning: implicit declaration of function '__cpuid'abspath.c: At top level:compat/cpuid.h:8: warning: 'processor_supports_sse42' defined but not usedabspath.c: In function 'processor_supports_sse42':compat/cpuid.h:11: warning: 'eax' is used uninitialized in this functioncompat/cpuid.h:11: warning: 'ebx' is used uninitialized in this functioncompat/cpuid.h:11: warning: 'ecx' is used uninitialized in this functioncompat/cpuid.h:11: warning: 'edx' is used uninitialized in this function
Quoted 1 line
Perhaps our gcc is too old?
Maybe.
In any case, it is a good indication that it probably is a good idea to start with an optional USE_SSE42 (not !NO_SSE42 or HAVE_SSE42) so that it is clear to anybody that those with SSE42 does not have to use this compilation option. Once the code proves itself, we can consider turning it on by default when able, but it seems that it is a bit too premature for that (not that the code itself is premature in the original author's environment, but its portability has not been quite ready for everybody yet, it seems).
Quoted 42 lines · Johannes Sixt
Am 6/10/2014 1:05, schrieb Junio C Hamano:
Quoted 16 lines
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · David Turner
Since Junio has picked up the first patch from previous versions of this series, I'm just going to send the second (SSE) one. I decided not to s/NO_SSE42/!HAVE_SSE42/ because it looks like git mostly uses the former convention (for instance, that's what
GIT_PARSE_WITHgenerates).Quoted 5 lines
Yeah but
NO_FROTZis used only when FROTZ is something everybody is expected to have (e.g. it's in posix, people ought to have it, but we do support those who don't), isn't it? For a very arch specific stuff like sse42, I'd feel better to make it purely opt-in by forcing people to explicitly sayHAVE_SSE42to enable it.Quoted 8 lines
Just FYI: I am getting
compat/cpuid.h:8:12: error: 'processor_supports_sse42' defined but not used [-Werror=unused-function]
cc1: all warnings being treated as errorswhile building 'pu'; I'll have to rebuild 'pu' without this patch before I can push the day's result out.
And I get this when I compile on Windows with msysgit:
CC abspath.o
In file included from git-compat-util.h:694,from cache.h:4, from abspath.c:1:
compat/cpuid.h: In function 'processor_supports_sse42':compat/cpuid.h:11: warning: implicit declaration of function '__cpuid'abspath.c: At top level:compat/cpuid.h:8: warning: 'processor_supports_sse42' defined but not usedabspath.c: In function 'processor_supports_sse42':compat/cpuid.h:11: warning: 'eax' is used uninitialized in this functioncompat/cpuid.h:11: warning: 'ebx' is used uninitialized in this functioncompat/cpuid.h:11: warning: 'ecx' is used uninitialized in this functioncompat/cpuid.h:11: warning: 'edx' is used uninitialized in this function
Quoted 1 line
Perhaps our gcc is too old?
It is too old for my patch because it doesn't support ifunc (and I suspect that no version of GCC for Windows supports ifunc). But that does not seem to be what is going on in your error message. Instead, when we #include <cpuid.h>, we get compat/cpuid.h rather than the system's cpuid.h. When I rename compat/cpuid.h to something else (and adjust the code accordingly), and add a check for gcc 4.5+ and for Windows before using ifunc, then everything works.
So tomorrow, I'm going to send a new patch (which also fixes the warnings Junio reported) which I've tested on Windows, GNU/Linux, and Mac OS X.
I'm testing on a Windows 8 VM from modern.ie with msysgit's "netinstaller" -- is that a reasonable test environment?
On 2014-06-13 03.18, David Turner wrote:
[]
Quoted 6 lines
It is too old for my patch because it doesn't support ifunc (and I suspect that no version of GCC for Windows supports ifunc). But that does not seem to be what is going on in your error message. Instead, when we #include <
cpuid.h>, we getcompat/cpuid.hrather than the system'scpuid.h. When I renamecompat/cpuid.hto something else
compat/git_cpuid.h ?
Quoted 2 lines
I'm testing on a Windows 8 VM from modern.ie with msysgit's "netinstaller" -- is that a reasonable test environment?
Many people are using Windows 7,
and we shouldn't break for things for Windows XP.
From: "Torsten Bögershausen" <[email protected]>
Quoted 8 lines · David Turner
[]
Quoted 6 lines
It is too old for my patch because it doesn't support ifunc (and I suspect that no version of GCC for Windows supports ifunc). But that does not seem to be what is going on in your error message. Instead, when we #include <
cpuid.h>, we getcompat/cpuid.hrather than the system'scpuid.h. When I renamecompat/cpuid.hto something else
Quoted 8 lines
compat/git_cpuid.h?Quoted 2 lines
I'm testing on a Windows 8 VM from modern.ie with msysgit's "netinstaller" -- is that a reasonable test environment?
Many people are using Windows 7,
and we shouldn't break for things for Windows XP.
I'd like that.
--
Philip
sent from my XP netbook;-)