Big git diff speedup by avoiding x86 "fast string" memcmp
Subsystems (2)
Quoted 14 lines
static inline int dentry_memcmp_long(const unsigned char *cs,
const unsigned char *ct,ssize_tcount){ int ret; const unsigned long *ls = (const unsigned long *)cs; const unsigned long *lt = (const unsigned long *)ct; while (count > 8) { ret = (*cs != *ct); if (ret) break; cs++; ct++; count-=8;
Quoted 8 lines
}
if (count) {
unsigned long t = *ct & ((0xffffffffffffffff >> ((8 - count) * 8))
ret = (*cs != t)
}
return ret;
}First, let's get the code right, and use correct types, but also, there are some tricks to reduce the masking cost.
As long as you have to mask one string, *and* don't have to worry about running off the end of mapped memory, there's no additional cost to masking both in the loop. Just test (a ^ b) & mask.
#if 1 /* Table lookup */
static unsigned long const dentry_memcmp_mask[8] = {
#if defined(__LITTLE_ENDIAN) 0xff, 0xffff, 0xffffff, 0xffffffff, #if sizeof (unsigned long) > 4 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0xffffffffffffffff #endif #elif defined(__BIG_ENDIAN) #if sizeof (unsigned long) == 4 0xff000000, 0xffff0000, 0xffffff00, 0xffffffff #else 0xff00000000000000, 0xffff000000000000, 0xffffff0000000000, 0xffffffff00000000, 0xffffffffff000000, 0xffffffffffff0000, 0xffffffffffffff00, 0xffffffffffffffff
#endif
#else
#error undefined endianness
#endif
};
#define dentry_memcmp_mask(count) dentry_memcmp_mask[(count)-1]
#else /* In-line code */
#if defined(__LITTLE_ENDIAN)
#define dentry_memcmp_mask(count) (-1ul >> (sizeof 1ul - (count)) * 8) #elif defined(__BIG_ENDIAN)
#define dentry_memcmp_mask(count) (-1ul << (sizeof 1ul - (count)) * 8) #else
#error undefined endianness
#endif
static inline bool dentry_memcmp_long(unsigned char const *cs,
unsigned char const *ct, ssize_t count)
{
unsigned long const *ls = (unsigned long const *)cs;
unsigned long const *lt = (unsigned long const *)ct;
while (count > sizeof *cs) {
if (*cs != *ct)
return true;
cs++;
ct++;
count -= sizeof *cs;
}
/* Last 1..8 bytes */
return ((*ct ^ *cs) & dentry_memcmp_mask(count)) != 0;
}If your string is at least 8 bytes long, and the processor has fast unaligned loads, you can skip the mask entirely by redundantly comparing some bytes (although the code bloat is probably undesirable for inline code):
static inline bool dentry_memcmp_long(const unsigned char *cs,
const unsigned char *ct, ssize_t count)
{
unsigned long const *ls = (unsigned long const *)cs;
unsigned long const *lt = (unsigned long const *)ct;
if (count < sizeof *cs)
return ((*ct ^ *cs) & dentry_memcmp_mask(count)) != 0;
while (count > sizeof *cs) {
if (*cs != *ct)
return true;
cs++;
ct++;
count -= sizeof *cs;
}
cs = (unsigned long const *)((char const *)cs + count - sizeof *cs);
ct = (unsigned long const *)((char const *)ct + count - sizeof *ct);
return *cs != *ct;
}Quoted 56 lines · George Spelvin
Quoted 14 lines
static inline int dentry_memcmp_long(const unsigned char *cs,
const unsigned char *ct,ssize_tcount){ int ret; const unsigned long *ls = (const unsigned long *)cs; const unsigned long *lt = (const unsigned long *)ct; while (count > 8) { ret = (*cs != *ct); if (ret) break; cs++; ct++; count-=8;Quoted 8 lines
} if (count) { unsigned long t = *ct & ((0xffffffffffffffff >> ((8 - count) * 8)) ret = (*cs != t) } return ret; }First, let's get the code right, and use correct types, but also, there are some tricks to reduce the masking cost.
As long as you have to mask one string, *and* don't have to worry about running off the end of mapped memory, there's no additional cost to masking both in the loop. Just test (a ^ b) & mask.
#if 1 /* Table lookup */
static unsigned long const dentry_memcmp_mask[8] = {
#if defined(__LITTLE_ENDIAN) 0xff, 0xffff, 0xffffff, 0xffffffff, #if sizeof (unsigned long) > 4 0xffffffffff, 0xffffffffffff, 0xffffffffffffff, 0xffffffffffffffff #endif #elif defined(__BIG_ENDIAN) #if sizeof (unsigned long) == 4 0xff000000, 0xffff0000, 0xffffff00, 0xffffffff #else 0xff00000000000000, 0xffff000000000000, 0xffffff0000000000, 0xffffffff00000000, 0xffffffffff000000, 0xffffffffffff0000, 0xffffffffffffff00, 0xffffffffffffffff#endif
#else
#error undefined endianness
#endif
};#define
dentry_memcmp_mask(count)dentry_memcmp_mask[(count)-1]#else /* In-line code */
Quoted 9 lines
#if
defined(__LITTLE_ENDIAN)
#definedentry_memcmp_mask(count)(-1ul >> (sizeof 1ul - (count)) * 8) #elifdefined(__BIG_ENDIAN)
#definedentry_memcmp_mask(count)(-1ul << (sizeof 1ul - (count)) * 8) #else
#error undefined endianness#endif
Thanks Yes that fixes the _ENDIANness problem. and unsigned maths
I was considering the table as well. The table might be better or not, considering the pipe-line and the multiple-instructions-per-clock. x86 specially likes the operating on cost values. It should be tested with both #ifs
Quoted 14 lines
static inline bool dentry_memcmp_long(unsigned char const *cs,
unsigned char const *ct,ssize_tcount){ unsigned long const *ls = (unsigned long const *)cs; unsigned long const *lt = (unsigned long const *)ct; while (count > sizeof *cs) { if (*cs != *ct) return true; cs++; ct++; count -= sizeof *cs; } /* Last 1..8 bytes */
what if at this point count == 0, I think you need the if (count) still
Quoted 1 line
return ((*ct ^ *cs) & dentry_memcmp_mask(count)) != 0;
OK I like it, my version could be equally fast if the compiler would use cmov but not all ARChs have it.
Quoted 18 lines
}
If your string is at least 8 bytes long, and the processor has fast unaligned loads, you can skip the mask entirely by redundantly comparing some bytes (although the code bloat is probably undesirable for inline code):
static inline bool dentry_memcmp_long(const unsigned char *cs,
const unsigned char *ct,ssize_tcount){ unsigned long const *ls = (unsigned long const *)cs; unsigned long const *lt = (unsigned long const *)ct; if (count < sizeof *cs) return ((*ct ^ *cs) & dentry_memcmp_mask(count)) != 0; while (count > sizeof *cs) { if (*cs != *ct) return true;
Quoted 8 lines
cs++; ct++; count -= sizeof *cs; } cs = (unsigned long const *)((char const *)cs + count - sizeof *cs); ct = (unsigned long const *)((char const *)ct + count - sizeof *ct); return *cs != *ct; }
As Linus said, All this is mute, the qstr part of the compare is not long aligned at the start. So I guess we come up loosing.
Thanks
Boaz
Quoted 20 lines · George Spelvin
Quoted 20 lines
static inline int
dentry_memcmp_long(const unsigned char *cs, const unsigned char *ct, ssize_t count){
int ret;
const unsigned long *ls = (const unsigned long *)cs; const unsigned long *lt = (const unsigned long *)ct;while (count > 8) {
ret = (*cs != *ct);
if (ret)
break;
cs++;
ct++;
count-=8;
}
if (count) {
unsigned long t = *ct & ((0xffffffffffffffff>> ((8 - count) * 8)) ret = (*cs != t)
}
Quoted 4 lines
Quoted 2 lines
return ret;
}First, let's get the code right, and use correct types, but also, there
You still used the wrong vars in the loop.
Quoted 5 lines
are some tricks to reduce the masking cost.
As long as you have to mask one string, *and* don't have to worry about running off the end of mapped memory, there's no additional cost to masking both in the loop. Just test (a ^ b) & mask.
Using a lookup table I considered, but maybe not well enough. It is another cacheline, but common to all lookups. So it could well be worth it, let's keep your code around...
The big problem for CPUs that don't do well on this type of code is what the string goes through during the entire syscall.
First, a byte-by-byte strcpy_from_user of the whole name string to kernel space. Then a byte-by-byte chunking and hashing component paths according to '/'. Then a byte-by-byte memcmp against the dentry name.
I'd love to do everything with 8 byte loads, do the component separation and hashing at the same time as copy from user, and have the padded and aligned component strings and their hash available... but complexity.
On my Westmere system, time to do a stat is 640 cycles plus 10 cycles for every byte in the string (this cost holds perfectly from 1 byte name up to 32 byte names in my test range). git diff average path name strings are 31 bytes, although this is much less cache friendly, and over several components (my test is just a single component).
But still, even if the base cost were doubled, it may still spend 20% or so kernel cycles in name string handling.
This 8 byte memcpy takes my microbenchmark down to 8 cycles per byte, so it may get several more % on git diff.
A careful thinking about the initial strcpy_from_user, and hashing code could shave another few cycles off it. Well worth investigating I think.
Quoted 4 lines
First, a byte-by-byte
strcpy_from_userof the whole name string to kernel space. Then a byte-by-byte chunking and hashing component paths according to '/'. Then a byte-by-byte memcmp against the dentry name.
Well, you've put your finger on the obvious place to to the aligned copy, if you want. Looking into it, I note that __d_lookup() does a 32-bit hash compare before doing a string compare, so the memcmp should almost always succeed. (Failures are due to hash collisions and rename races.)
Quoted 4 lines
I'd love to do everything with 8 byte loads, do the component separation and hashing at the same time as copy from user, and have the padded and aligned component strings and their hash available... but complexity.
It actually doesn't seem that difficult to do in fs/namei.c:do_getname via a heavily hacked strncpy_from_user. Call it strhash_from_user, and it copies the string, while accumulating the hash and the length, until it hits a nul or /. It has to return the length, the hash, and the termination condition: nul, /, or space exhausted.
Then you could arrange for each component to be padded so that it starts aligned.
(The hash and length can be stored directly in the qstr. The length is only otherwise needed for components that end in /, so you could return a magic negative length in those cases.)
If it matters, Bob Jenkins wrote a "one-at-a-time" hash function that is actually sligthy less work per character than the current partial_name_hash. (Two shifts, two adds and a *11 gets turned into two shifts, two adds, and and an XOR)
static inline u32 __attribute__((const))partial_name_hash(unsigned char c, u32 hash)
{
hash += c;
hash += hash << 10;
hash ^= hash >> 6;
return hash;
}static inline u32 end_name_hash(u32 hash)
{
hash += hash << 3;
hash ^= hash >> 11;
hash += hash << 15;
return hash;
}(I note that there's zero reason for the current partial_name_hash to use an unsigned long intermediate value, since the high 32 bits have no effect on the final result. It just forces unnecessary REX prefixes.)
The main problem with initializing all the qstr structures early is that it can lead to an oddly dynamic PATH_MAX and/or a type of kernel DOS by passing in paths with many short directory names that would need maximal padding.
Quoted 6 lines
On my Westmere system, time to do a stat is 640 cycles plus 10 cycles for every byte in the string (this cost holds perfectly from 1 byte name up to 32 byte names in my test range).
git diffaverage path name strings are 31 bytes, although this is much less cache friendly, and over several components (my test is just a single component).
Thank you for this useful real-world number!
I hadn't realized the base cost was so low.
Quoted 7 lines · George Spelvin
Quoted 4 lines
First, a byte-by-byte
strcpy_from_userof the whole name string to kernel space. Then a byte-by-byte chunking and hashing component paths according to '/'. Then a byte-by-byte memcmp against the dentry name.Well, you've put your finger on the obvious place to to the aligned copy, if you want.
Yes, this would be a nice place to align things and parse '/' compoents, although in fact it doesn't strictly have to be a byte-by-byte copy (although doing long loads would put a lot more complexity in fault handling and parsing).
Quoted 15 lines
Looking into it, I note that
__d_lookup()does a 32-bit
hash compare before doing a string compare, so the memcmp should almost always succeed. (Failures are due to hash collisions and rename races.)Quoted 4 lines
I'd love to do everything with 8 byte loads, do the component separation and hashing at the same time as copy from user, and have the padded and aligned component strings and their hash available... but complexity.
It actually doesn't seem that difficult to do in fs/namei.c:do_getname via a heavily hacked
strncpy_from_user. Call itstrhash_from_user, and it copies the string, while accumulating the hash and the length, until it hits a nul or /. It has to return the length, the hash, and the termination condition: nul, /, or space exhausted.
Quoted 6 lines
Then you could arrange for each component to be padded so that it starts aligned.
(The hash and length can be stored directly in the qstr. The length is only otherwise needed for components that end in /, so you could return a magic negative length in those cases.)
Probably, due to bloat caused by the hash for small names, and that some filesystems do their own hashing, we should still hash in the same place. However we can do sizeof(long) at a time hashing, which should be nice and fast too.
Quoted 4 lines
The main problem with initializing all the qstr structures early is that it can lead to an oddly dynamic
PATH_MAXand/or a type of kernel DOS by passing in paths with many short directory names that would need maximal padding.
If the results are compelling enough, this could be worked around some way. Simple realloc probably, seeing as it would be a rare case.