refs.c: SSE2 optimizations for check_refname_component
PATCH v8 1/1 refs.c: SSE2 optimizations for check_refname_component
Optimize check_refname_component using SSE2 on x86_64.
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 SSE2: 23 ms
This cuts about 20% off of the runtime.
The configure.ac changes include code from the GNU C Library written by Joseph S. Myers <joseph at codesourcery dot com>.
Ondřej Bílka <[email protected]> suggested an SSE2 approach to the substring searches, which netted a speed boost over the SSE4.2 code I had initially written.
--- git-compat-util.h | 10 +++ refs.c | 244 +++++++++++++++++++++++++++++++++++++++++++++-------- t/t5511-refspec.sh | 19 +++++ 3 files changed, 239 insertions(+), 34 deletions(-)
git-compat-util.h +10 -0
@@ -668,6 +668,16 @@ void git_qsort(void *base, size_t nmemb, size_t size,#endif #endif +#if defined(__GNUC__) && defined(__x86_64__) +#include <emmintrin.h> +/* 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 +210 -34
293 lines not on this page · raw message
t/t5511-refspec.sh +19 -0
@@ -29,6 +29,10 @@ test_refspec push '+:' test_refspec fetch '' test_refspec fetch ':' test_refspec fetch '::' invalid +test_refspec fetch '.' invalid +test_refspec fetch './frotz' invalid +test_refspec fetch 'refs/.' invalid +test_refspec push '*//:refs/remotes/frotz/*' invalid test_refspec push 'refs/heads/*:refs/remotes/frotz/*' test_refspec push 'refs/heads/*:refs/remotes/frotz' invalid
@@ -71,6 +75,8 @@ test_refspec fetch ':refs/remotes/frotz/HEAD-to-me' test_refspec push ':refs/remotes/frotz/delete me' invalid test_refspec fetch ':refs/remotes/frotz/HEAD to me' invalid +test_refspec fetch ':refs/remotes/frotz/something.lock' invalid + test_refspec fetch 'refs/heads/*/for-linus:refs/remotes/mine/*-blah' invalid test_refspec push 'refs/heads/*/for-linus:refs/remotes/mine/*-blah' invalid
@@ -88,4 +94,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 SSE2 onx86_64.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 SSE2: 23 ms
Quoted 6 lines
This cuts about 20% off of the runtime.
The configure.ac changes include code from the GNU C Library written by Joseph S. Myers <joseph at codesourcery dot com>.
Ondřej Bílka <[email protected]> suggested an SSE2 approach to the
One e-mail address is obfuscated while the other not; intended?
Quoted 10 lines
substring searches, which netted a speed boost over the SSE4.2 code I had initially written.
Signed-off-by David Turner <[email protected]>---
git-compat-util.h +0
-0
git-compat-util.h void git_qsort(void *base, size_t nmemb, size_t size, · line 673
Quoted 6 lines
git-compat-util.h +3
-0@@ -668,6 +668,16 @@ void git_qsort(void *base, size_t nmemb, size_t size,#endif #endif +#if defined(__GNUC__) && defined(__x86_64__) +#include <emmintrin.h> +/* This is the system memory page size; it's used so that we can read
Style (there are other instances of the same kind).
/*
* This is the ...
Quoted 9 lines
git-compat-util.h +8
-0+* outside the bounds of an allocation without segfaulting. +*/ +static int check_refname_component_trailer (const char *cp,const char *refname,int flags) +{ +if (cp == refname) +return 0 ;/* Component has zero length. */ +if (refname[0 ] =='.' ) { +if (!(flags &REFNAME_DOT_COMPONENT ))
git-compat-util.h void git_qsort(void *base, size_t nmemb, size_t size,
Quoted 10 lines
git-compat-util.h +10
-0+return -1 ;/* Component starts with '.'. */ +/* +* Even if leading dots are allowed, don't allow "." +* as a component (".." is prevented by a rule above). +*/ +if (refname[1 ] =='\0' ) +return -1 ;/* Component equals ".". */ + } +if (cp - refname >=5 && !memcmp (cp -5 ,".lock" ,5 )) +return -1 ;/* Refname ends with ".lock". */
This is merely a moved code that retained the same comment, but it is more like "the current refname component ends with .lock", I suspect. In other words, we do not allow "refs/heads/foo.lock/bar". Am I reading the patch correctly?
git-compat-util.h
Quoted 9 lines
git-compat-util.h +9
-0+#if defined(__GNUC__) && defined(__x86_64__) +#define SSE_VECTOR_BYTES 16 + +/* Vectorized version of check_refname_format. */ +int check_refname_format (const char *refname,int flags) +{ +const char *cp = refname; + +const __m128i dot =_mm_set1_epi8 ('.' );
Style (there are other instances of the same kind). No SP between function/macro name and opening parenthesis.
git-compat-util.h
Quoted 11 lines
git-compat-util.h +11
-0+if (refname[0 ] =='.' ) { +if (refname[1 ] =='/' || refname[1 ] =='\0' ) +return -1 ; +if (!(flags &REFNAME_DOT_COMPONENT )) +return -1 ; + } +while (1 ) { +__m128i tmp, tmp1, result; +uint64_t mask; + +if ((uintptr_t ) cp %PAGE_SIZE >PAGE_SIZE -SSE_VECTOR_BYTES -1 )
OK, so we make sure we do not overrun by reading too much near the end of the page, as the next page might be unmapped.
I am showing my ignorance but does cp (i.e. refname) upon entry to this function need to be aligned in some way?
Thanks.
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · David Turner
Optimize
check_refname_componentusing SSE2 onx86_64.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 SSE2: 23 ms
Quoted 8 lines
Quoted 6 lines
This cuts about 20% off of the runtime.
The configure.ac changes include code from the GNU C Library written by Joseph S. Myers <joseph at codesourcery dot com>.
Ondřej Bílka <[email protected]> suggested an SSE2 approach to the
One e-mail address is obfuscated while the other not; intended?
Yes (although I'll happily obfuscate or remove Ondřej's if he prefers); Joseph's was obfuscated because that's how I found it in the source material.
Quoted 13 lines
Quoted 13 lines
substring searches, which netted a speed boost over the SSE4.2 code I had initially written.
Signed-off-by David Turner <[email protected]>---
git-compat-util.h +0
-0@@ -668,6 +668,16 @@ void git_qsort(void *base, size_t nmemb, size_t size,#endif #endif
Quoted 5 lines
git-compat-util.h void git_qsort(void *base, size_t nmemb, size_t size, · line 673
Quoted 3 lines
git-compat-util.h +3
-0+#if defined(__GNUC__) && defined(__x86_64__) +#include <emmintrin.h> +/* This is the system memory page size; it's used so that we can read Style (there are other instances of the same kind).
Will fix.
Quoted 13 lines
Quoted 13 lines
git-compat-util.h +13
-0+static int check_refname_component_trailer (const char *cp,const char *refname,int flags) +{ +if (cp == refname) +return 0 ;/* Component has zero length. */ +if (refname[0 ] =='.' ) { +if (!(flags &REFNAME_DOT_COMPONENT )) +return -1 ;/* Component starts with '.'. */ +/* +* Even if leading dots are allowed, don't allow "." +* as a component (".." is prevented by a rule above). +*/ +if (refname[1 ] =='\0' ) +return -1 ;/* Component equals ".". */
Quoted 8 lines
git-compat-util.h
Quoted 3 lines
git-compat-util.h +3
-0+ } +if (cp - refname >=5 && !memcmp (cp -5 ,".lock" ,5 )) +return -1 ;/* Refname ends with ".lock". */ This is merely a moved code that retained the same comment, but it is more like "the current refname component ends with .lock", I suspect. In other words, we do not allow "refs/heads/foo.lock/bar". Am I reading the patch correctly?
This code didn't actually need to be moved; I'll move it back.
But it now occurs to me that the SSE2 code does not actually check for .lock at the end of each component, but only at the end of string.
I don't know why we don't allow refs/heads/foo.lock/bar. I can understand not allowing refs/heads/foo.lock, since it might be a lock file. But I have never heard of a lock directory like this.
I'm happy to replicate what the existing code does, but do you think it is correct?
Quoted 2 lines
Style (there are other instances of the same kind). No SP between function/macro name and opening parenthesis.
Will fix.
Quoted 6 lines
Quoted 6 lines
git-compat-util.h +6
-0+if (refname[0 ] =='.' ) { +if (refname[1 ] =='/' || refname[1 ] =='\0' ) +return -1 ; +if (!(flags &REFNAME_DOT_COMPONENT )) +return -1 ; + }
Quoted 8 lines
git-compat-util.h
Quoted 5 lines
git-compat-util.h +5
-0+while (1 ) { +__m128i tmp, tmp1, result; +uint64_t mask; + +if ((uintptr_t ) cp %PAGE_SIZE >PAGE_SIZE -SSE_VECTOR_BYTES -1 )OK, so we make sure we do not overrun by reading too much near the end of the page, as the next page might be unmapped.
Exactly.
Quoted 2 lines
I am showing my ignorance but does cp (i.e. refname) upon entry to this function need to be aligned in some way?
Nope -- _mm_loadu_si128 does an unaligned load.