read-cache: speed up add_index_entry
v6 cover read-cache: speed up add_index_entry
From: Jeff Hostetler <[email protected]>
Version 6 combines the strcmp_offset() function and unit tests into a single commit and places it first in the history, so that it can be isolated into a separate patch series if desired. It also clarifies the return value when the strings are equal. The commit message for the actual speed up now includes the perf numbers (rather than having them here in the cover letter).
================
Teach add_index_entry_with_check() and has_dir_name() to avoid index lookups if the given path sorts after the last entry in the index.
This saves at least 2 binary searches per entry.
This improves performance during checkout and read-tree because merge_working_tree() and unpack_trees() processes a list of already sorted entries.
Jeff Hostetler (3):
read-cache: add strcmp_offset function
p0004-read-tree: perf test to time read-tree
read-cache: speed up add_index_entry during checkout
Makefile | 1 + cache.h | 1 + read-cache.c | 64 ++++++++++++++++++++++- t/helper/.gitignore | 1 + t/helper/test-strcmp-offset.c | 64 +++++++++++++++++++++++ t/perf/p0004-read-tree.sh | 117 ++++++++++++++++++++++++++++++++++++++++++ t/t0065-strcmp-offset.sh | 11 ++++ 7 files changed, 258 insertions(+), 1 deletion(-) create mode 100644 t/helper/test-strcmp-offset.c create mode 100755 t/perf/p0004-read-tree.sh create mode 100755 t/t0065-strcmp-offset.sh
Signature · 1 line
2.9.3
PATCH v6 1/3 read-cache: add strcmp_offset function
From: Jeff Hostetler <[email protected]>
Add strcmp_offset() function to also return the offset of the first change.
Add unit test and helper to verify.
--- Makefile | 1 + cache.h | 1 + read-cache.c | 20 ++++++++++++++ t/helper/.gitignore | 1 + t/helper/test-strcmp-offset.c | 64 +++++++++++++++++++++++++++++++++++++++++++ t/t0065-strcmp-offset.sh | 11 ++++++++ 6 files changed, 98 insertions(+) create mode 100644 t/helper/test-strcmp-offset.c create mode 100755 t/t0065-strcmp-offset.sh
Makefile +1 -0
@@ -631,6 +631,7 @@ TEST_PROGRAMS_NEED_X += test-scrap-cache-tree TEST_PROGRAMS_NEED_X += test-sha1 TEST_PROGRAMS_NEED_X += test-sha1-array TEST_PROGRAMS_NEED_X += test-sigchain +TEST_PROGRAMS_NEED_X += test-strcmp-offset TEST_PROGRAMS_NEED_X += test-string-list TEST_PROGRAMS_NEED_X += test-submodule-config TEST_PROGRAMS_NEED_X += test-subprocess
cache.h +1 -0
@@ -574,6 +574,7 @@ extern int write_locked_index(struct index_state *, struct lock_file *lock, unsiextern int discard_index (struct index_state *);extern int unmerged_index (const struct index_state *);extern int verify_path (const char *path); +extern int strcmp_offset (const char *s1,const char *s2,int *first_change);extern int index_dir_exists (struct index_state *istate,const char *name,int namelen);extern void adjust_dirname_case (struct index_state *istate,char *name);extern struct cache_entry *index_file_exists (struct index_state *istate,const char *name,int namelen,int igncase);
read-cache.c +20 -0
@@ -887,6 +887,26 @@ static int has_file_name(struct index_state *istate,return retval; } + +/* +* Like strcmp(), but also return the offset of the first change. +* If strings are equal, return the length. +*/ +int strcmp_offset (const char *s1,const char *s2,int *first_change) +{ +int k; + +if (!first_change) +return strcmp (s1, s2); + +for (k =0 ; s1[k] == s2[k]; k++) +if (s1[k] =='\0' ) +break ; + + *first_change = k; +return ((unsigned char *)s1)[k] - ((unsigned char *)s2)[k]; +} +/* * Do we have another file with a pathname that is a proper * subset of the name we're trying to add?
t/helper/.gitignore +1 -0
@@ -25,6 +25,7 @@ /test-sha1 /test-sha1-array /test-sigchain +/test-strcmp-offset /test-string-list /test-submodule-config /test-subprocess
t/helper/test-strcmp-offset.c new file +64 -0
64 lines not on this page · raw message
t/t0065-strcmp-offset.sh new file +11 -0
@@ -0,0 +1,11 @@ +#!/bin/sh + +test_description='Test strcmp_offset functionality' + +. ./test-lib.sh + +test_expect_success run_helper ' + test-strcmp-offset +' + +test_done
Signature · 1 line
2.9.3
Re: [PATCH v6 1/3] read-cache: add strcmp_offset function
Am 06.04.2017 um 18:34 schrieb [email protected]:
Quoted 17 lines
read-cache.c +9
-0@@ -887,6 +887,26 @@ static int has_file_name(struct index_state *istate,return retval; } + +/* +* Like strcmp(), but also return the offset of the first change. +* If strings are equal, return the length. +*/ +int strcmp_offset (const char *s1,const char *s2,int *first_change) +{ +int k; +
read-cache.c static int has_file_name(struct index_state *istate, · line 908
Quoted 10 lines
read-cache.c +10
-0+if (!first_change) +return strcmp (s1, s2); + +for (k =0 ; s1[k] == s2[k]; k++) +if (s1[k] =='\0' ) +break ; + + *first_change = k; +return ((unsigned char *)s1)[k] - ((unsigned char *)s2)[k]; +}
I like how this is shaping up to become a reusable function, but I only found one other possible use case (in read-cache.c::ce_write_entry), which somehow irritates me. Either I didn't look hard enough (likely), we haven't got fitting code just yet, or this function isn't meant to be exported -- in the latter case its interface doesn't have to be polished as much. (Just a thought, don't let me stop you.)
Assuming strcmp_offset() is a library-grade function then its first_change parameter should point to a size_t instead of an int, no?
Casting away the const qualifier in the return line is a bit iffy. Why not cast after dereferencing, like this?
return (unsigned char)s1[k] - (unsigned char)s2[k];
René
Re: [PATCH v6 1/3] read-cache: add strcmp_offset function
Quoted 35 lines · René Scharfe
Am 06.04.2017 um 18:34 schrieb [email protected]:
Quoted 16 lines
read-cache.c +8
-0@@ -887,6 +887,26 @@ static int has_file_name(struct index_state *istate,return retval; } + +/* +* Like strcmp(), but also return the offset of the first change. +* If strings are equal, return the length. +*/ +int strcmp_offset(const char *s1, const char *s2, int *first_change) +{ + int k; +read-cache.c static int has_file_name(struct index_state *istate, · line 907
Quoted 10 lines
read-cache.c +10
-0+if (!first_change) +return strcmp (s1, s2); + +for (k =0 ; s1[k] == s2[k]; k++) +if (s1[k] =='\0' ) +break ; + + *first_change = k; +return ((unsigned char *)s1)[k] - ((unsigned char *)s2)[k]; +}I like how this is shaping up to become a reusable function, but I only found one other possible use case (in read-cache.c::ce_write_entry), which somehow irritates me. Either I didn't look hard enough (likely), we haven't got fitting code just yet, or this function isn't meant to be exported -- in the latter case its interface doesn't have to be polished as much. (Just a thought, don't let me stop you.)
Quoted 2 lines
Assuming
strcmp_offset()is a library-grade function then itsfirst_changeparameter should point to asize_tinstead of an int, no?
yes, size_t. thanks.
Quoted 5 lines
Casting away the const qualifier in the return line is a bit iffy. Why not cast after dereferencing, like this?
return (unsigned char)s1[k] - (unsigned char)s2[k];
Good point.
Thanks
Jeff
PATCH v6 2/3 p0004-read-tree: perf test to time read-tree
In reply to [email protected]From: Jeff Hostetler <[email protected]>
--- t/perf/p0004-read-tree.sh | 117 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100755 t/perf/p0004-read-tree.sh
t/perf/p0004-read-tree.sh new file +117 -0
@@ -0,0 +1,117 @@ +#!/bin/sh + +test_description="Tests performance of read-tree" + +. ./perf-lib.sh + +test_perf_default_repo +test_checkout_worktree + +## usage: dir depth width files +make_paths () { + for f in $(seq $4) + do + echo $1/file$f + done; + if test $2 -gt 0; + then + for w in $(seq $3) + do + make_paths $1/dir$w $(($2 - 1)) $3 $4 + done + fi + return 0 +} + +fill_index () { + make_paths $1 $2 $3 $4 | + sed "s/^/100644 $EMPTY_BLOB /" | + git update-index --index-info + return 0 +} + +br_base=xxx_base_xxx +br_work1=xxx_work1_xxx +br_work2=xxx_work2_xxx +br_work3=xxx_work3_xxx + +new_dir=xxx_dir_xxx + +## (5, 10, 9) will create 999,999 files. +## (4, 10, 9) will create 99,999 files. +depth=5 +width=10 +files=9 + +export br_base +export br_work1 +export br_work2 +export br_work3 + +export new_dir + +export depth +export width +export files + +## The number of files in the xxx_base_xxx branch. +nr_base=$(git ls-files | wc -l) +export nr_base + +## Inflate the index with thousands of empty files and commit it. +## Turn on sparse-checkout so that we don't have to populate them +## later when we start switching branches. Use reset --hard to +## quickly checkout the new HEAD with minimum actual files. +test_expect_success 'inflate the index' ' + git reset --hard && + git branch $br_base && + git branch $br_work1 && + git checkout $br_work1 && + fill_index $new_dir $depth $width $files && + git commit -m $br_work1 && + echo $new_dir/file1 >.git/info/sparse-checkout && + git config --local core.sparsecheckout 1 && + git reset --hard +' + +## The number of files in the xxx_work1_xxx branch. +nr_work1=$(git ls-files | wc -l) +export nr_work1 + +test_perf "read-tree work1 ($nr_work1)" ' + git read-tree -m $br_base $br_work1 -n +' + +## Alternate between base and work branches several +## times to measure a large change. +test_perf "switch base work1 ($nr_base $nr_work1)" ' + git checkout $br_base && + git checkout $br_work1 +' + +## Create work2 by modifying 1 file in work1. +## Create work3 as an alias of work2. +test_expect_success 'setup work2' ' + git branch $br_work2 && + git checkout $br_work2 && + echo x >$new_dir/file1 && + git add $new_dir/file1 && + git commit -m $br_work2 && + git branch $br_work3 +' + +## Alternate between work1 and work2 several times +## to measure a very small change. +test_perf "switch work1 work2 ($nr_work1)" ' + git checkout $br_work1 && + git checkout $br_work2 +' + +## Alternate between branches work2 and work3 which +## are aliases of the same commit. +test_perf "switch commit aliases ($nr_work1)" ' + git checkout $br_work3 && + git checkout $br_work2 +' + +test_done
Signature · 1 line
2.9.3
PATCH v6 3/3 read-cache: speed up add_index_entry during checkout
In reply to [email protected]From: Jeff Hostetler <[email protected]>
Teach add_index_entry_with_check() and has_dir_name() to see if the path of the new item is greater than the last path in the index array before attempting to search for it.
During checkout, merge_working_tree() populates the new index in sorted order, so this change will save at least 2 binary lookups per file. This preserves the original behavior but simply checks the last element before starting the search.
This helps performance on very large repositories.
================
Before and after numbers on index with 1M files ./p0004-read-tree.sh
0004.2: read-tree work1 (1003037) 3.21(2.54+0.62) 0004.3: switch base work1 (3038 1003037) 7.49(5.39+1.84) 0004.5: switch work1 work2 (1003037) 11.91(8.38+3.00) 0004.6: switch commit aliases (1003037) 12.22(8.30+3.06)
./p0004-read-tree.sh
0004.2: read-tree work1 (1003040) 2.40(1.65+0.73) 0004.3: switch base work1 (3041 1003040) 6.07(4.12+1.66) 0004.5: switch work1 work2 (1003040) 10.23(6.76+2.92) 0004.6: switch commit aliases (1003040) 10.53(6.97+2.83)
================
--- read-cache.c | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-)
read-cache.c +43 -1
@@ -918,6 +918,21 @@ static int has_dir_name(struct index_state *istate,int stage =ce_stage (ce);const char *name = ce->name;const char *slash = name +ce_namelen (ce); +int len_eq_last; +int cmp_last =0 ; + +if (istate->cache_nr >0 ) { +/* +* Compare the entry's full path with the last path in the index. +* If it sorts AFTER the last entry in the index and they have no +* common prefix, then there cannot be any F/D name conflicts. +*/ + cmp_last =strcmp_offset (name, + istate->cache[istate->cache_nr-1 ]->name, + &len_eq_last); +if (cmp_last >0 && len_eq_last ==0 ) +return retval; + }for (;;) {int len;
@@ -930,6 +945,24 @@ static int has_dir_name(struct index_state *istate, } len = slash - name; +if (cmp_last >0 ) { +/* +* If this part of the directory prefix (including the trailing +* slash) already appears in the path of the last entry in the +* index, then we cannot also have a file with this prefix (or +* any parent directory prefix). +*/ +if (len+1 <= len_eq_last) +return retval; +/* +* If this part of the directory prefix (excluding the trailing +* slash) is longer than the known equal portions, then this part +* of the prefix cannot collide with a file. Go on to the parent. +*/ +if (len > len_eq_last) +continue ; + } + pos =index_name_stage_pos (istate, name, len, stage);if (pos >=0 ) {/*
@@ -1021,7 +1054,16 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_eif (!(option &ADD_CACHE_KEEP_CACHE_TREE ))cache_tree_invalidate_path (istate, ce->name); - pos =index_name_stage_pos (istate, ce->name,ce_namelen (ce),ce_stage (ce)); + +/* +* If this entry's path sorts after the last entry in the index, +* we can avoid searching for it. +*/ +if (istate->cache_nr >0 && +strcmp (ce->name, istate->cache[istate->cache_nr -1 ]->name) >0 ) + pos = -istate->cache_nr -1 ; +else + pos =index_name_stage_pos (istate, ce->name,ce_namelen (ce),ce_stage (ce));/* existing match? Just replace it. */ if (pos >=0 ) {
Signature · 1 line
2.9.3
Quoted 9 lines · On Thu, Apr 06, 2017 at 04:34:39PM +0000, [email protected] wrote:
Teach
add_index_entry_with_check()andhas_dir_name()to avoid index lookups if the given path sorts after the last entry in the index.This saves at least 2 binary searches per entry.
This improves performance during checkout and read-tree because
merge_working_tree()andunpack_trees()processes a list of already sorted entries.
Just thinking about this algorithmically for a moment. You're saving the binary search when the input is given in sorted order. But in other cases you're adding an extra strcmp() before the binary search begins. So it's a tradeoff.
How often is the input sorted? You save O(log n) strcmps for a "hit" with your patch, and one for a "miss". So it's a net win if we expect at least 1/log(n) of additions to be sorted (I'm talking about individual calls, but it should scale linearly either way over a set of n calls).
I have no clue if that's a reasonable assumption or not.
-Peff
Quoted 17 lines · Jeff King
Quoted 9 lines · On Thu, Apr 06, 2017 at 04:34:39PM +0000, [email protected] wrote:
Teach
add_index_entry_with_check()andhas_dir_name()to avoid index lookups if the given path sorts after the last entry in the index.This saves at least 2 binary searches per entry.
This improves performance during checkout and read-tree because
merge_working_tree()andunpack_trees()processes a list of already sorted entries.Just thinking about this algorithmically for a moment. You're saving the binary search when the input is given in sorted order. But in other cases you're adding an extra
strcmp()before the binary search begins. So it's a tradeoff.
Quoted 6 lines
How often is the input sorted? You save O(log n) strcmps for a "hit" with your patch, and one for a "miss". So it's a net win if we expect at least 1/log(n) of additions to be sorted (I'm talking about individual calls, but it should scale linearly either way over a set of n calls).
I have no clue if that's a reasonable assumption or not.
I was seeing checkout call merge_working_tree to iterate over the source index/trees and call add_index_entry() for each. For example, in a "checkout -b" like operation where both sides are the same, this calls keep_entry() which appends the entry to the new index array. The append path should always be taken because the iteration is being driven from a sorted list.
I would think calls to add/stage individual files arrive in random order, so I'm not suggesting replacing the code -- just checking the end first.
Jeff
Quoted 19 lines · Jeff Hostetler
Quoted 11 lines
Just thinking about this algorithmically for a moment. You're saving the binary search when the input is given in sorted order. But in other cases you're adding an extra
strcmp()before the binary search begins. So it's a tradeoff.How often is the input sorted? You save O(log n) strcmps for a "hit" with your patch, and one for a "miss". So it's a net win if we expect at least 1/log(n) of additions to be sorted (I'm talking about individual calls, but it should scale linearly either way over a set of n calls).
I have no clue if that's a reasonable assumption or not.
I was seeing checkout call
merge_working_treeto iterate over the source index/trees and calladd_index_entry()for each. For example, in a "checkout -b" like operation where both sides are the same, this callskeep_entry()which appends the entry to the new index array. The append path should always be taken because the iteration is being driven from a sorted list.
Quoted 3 lines
I would think calls to add/stage individual files arrive in random order, so I'm not suggesting replacing the code -- just checking the end first.
Right, what I was wondering is how much this costs in those random-order cases. We _know_ it speeds up the cases you care about, but I want to make sure that it is not making some other case worse. How often do the random-order cases come up, and how much are they slowed?
I suspect in practice that calls here fall into one of two camps:
feeding a small-ish (compared to the total number of entries) set of paths, or feeding _every_ path. And if you are feeding every path, you are likely to do so in sorted order, rather than a random jumble. So it helps in the big cases, and the small cases are presumably small enough that we don't care much.
At least that seems like a plausible line of reasoning to me. ;)
-Peff