Index v5
Subsystems (2)
cover Index v5
Hi,
This is a follow up for last years Google Summer of Code (late I know :-) ), which wasn't merged back then. The previous rounds of the series are at $gmane/202752, $gmane/202923, $gmane/203088 and $gmane/203517.
Since then I added a index reading api, which allows certain parts of Git to take advantage of the the partial reading capability of the new index file format now. In this series the grep and the ls-files and the code-paths used by them are switched to the new api.
Another goal for the api is to hide the open coded loops and accesses to the in-memory format, to make it simpler to change the in-memory format to a version that fits the new on-disk format better.
Except for the new patches, mostly the "read-cache: read index-v5" patch changed, as the possibility to read the index partially was added.
The first patch for t2104 makes sense without the rest of the series, as it fixes running the test-suite with index-v4 as the default index format.
Below are the timings for the WebKit repository. c4b2d88 is the revicion before adding anything, while HEAD are the times at the last patch in the series. The slower times in update-index come from the update-index patch so they are no problem (in c4b2d88 the index is only read, while in HEAD it's read and written). The increase in time in the ls-files test come from the not having the prune_cache function in the index api.
I have not added this function as it only seems of use in ls-files, but it can still be added if this increase is a problem.
Test c4b2d88 HEAD
-------------------------------------------------------------------------------------
0003.2: v[23]: update-index 0.11(0.06+0.04) 0.22(0.15+0.05) +100.0% 0003.3: v[23]: grep nonexistent -- subdir 0.12(0.08+0.03) 0.12(0.09+0.02) +0.0% 0003.4: v[23]: ls-files -- subdir 0.11(0.08+0.01) 0.12(0.08+0.03) +9.1% 0003.6: v4: update-index 0.09(0.06+0.02) 0.18(0.14+0.03) +100.0% 0003.7: v4: grep nonexistent -- subdir 0.10(0.08+0.02) 0.10(0.07+0.02) +0.0% 0003.8: v4: ls-files -- subdir 0.09(0.07+0.01) 0.10(0.08+0.01) +11.1% 0003.10: v5: update-index <missing> 0.15(0.10+0.03) 0003.11: v5: grep nonexistent -- subdir <missing> 0.01(0.00+0.00) 0003.12: v5: ls-files -- subdir <missing> 0.01(0.01+0.00)
And for reference the times for a synthetic repository with a 470MB index file, just to demonstrate the improvements in large repositories.
Test c4b2d88 HEAD
-------------------------------------------------------------------------------------
0003.2: v[23]: update-index 1.50(1.18+0.30) 3.18(2.55+0.60) +112.0% 0003.3: v[23]: grep nonexistent -- subdir 1.62(1.28+0.32) 1.66(1.28+0.36) +2.5% 0003.4: v[23]: ls-files -- subdir 1.49(1.21+0.26) 1.62(1.28+0.32) +8.7% 0003.6: v4: update-index 1.18(0.89+0.28) 2.68(2.22+0.44) +127.1% 0003.7: v4: grep nonexistent -- subdir 1.29(1.00+0.28) 1.30(1.04+0.24) +0.8% 0003.8: v4: ls-files -- subdir 1.20(0.95+0.23) 1.30(0.98+0.30) +8.3% 0003.10: v5: update-index <missing> 2.12(1.63+0.48) 0003.11: v5: grep nonexistent -- subdir <missing> 0.08(0.04+0.02) 0003.12: v5: ls-files -- subdir <missing> 0.07(0.05+0.01)
Thomas Gummerer (21):
t2104: Don't fail for index versions other than [23]
read-cache: split index file version specific functionality
read-cache: move index v2 specific functions to their own file
read-cache: Re-read index if index file changed
read-cache: add index reading api
make sure partially read index is not changed
dir.c: use index api
tree.c: use index api
name-hash.c: use index api
grep.c: Use index api
ls-files.c: use the index api
read-cache: make read_blob_data_from_index use index api
documentation: add documentation of the index-v5 file format
read-cache: make in-memory format aware of stat_crc
read-cache: read index-v5
read-cache: read resolve-undo data
read-cache: read cache-tree in index-v5
read-cache: write index-v5
read-cache: write index-v5 cache-tree data
read-cache: write resolve-undo data for index-v5 update-index.c: rewrite index when index-version is given
Thomas Rast (1):
p0003-index.sh: add perf test for the index formats
Documentation/technical/index-file-format-v5.txt | 296 +++++ Makefile | 3 + builtin/grep.c | 71 +- builtin/ls-files.c | 213 ++- builtin/update-index.c | 8 +- cache-tree.c | 2 +- cache-tree.h | 6 + cache.h | 158 ++- dir.c | 33 +- name-hash.c | 11 +- read-cache-v2.c | 651 +++++++++ read-cache-v5.c | 1536 ++++++++++++++++++++++ read-cache.c | 752 ++++------- read-cache.h | 69 + t/perf/p0003-index.sh | 59 + t/t2104-update-index-skip-worktree.sh | 1 + test-index-version.c | 7 +- tree.c | 38 +- 18 files changed, 3183 insertions(+), 731 deletions(-) create mode 100644 Documentation/technical/index-file-format-v5.txt create mode 100644 read-cache-v2.c create mode 100644 read-cache-v5.c create mode 100644 read-cache.h create mode 100755 t/perf/p0003-index.sh
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 1/22 t2104: Don't fail for index versions other than [23]
t2104 currently checks for the exact index version 2 or 3, depending if there is a skip-worktree flag or not. Other index versions do not use extended flags and thus cannot be tested for version changes.
Make this test update the index to version 2 at the beginning of the test. Testing the skip-worktree flags for the default index format is still covered by t7011 and t7012.
--- t/t2104-update-index-skip-worktree.sh | 1 + 1 file changed, 1 insertion(+)
t/t2104-update-index-skip-worktree.sh +1 -0
@@ -22,6 +22,7 @@ H sub/2 EOF test_expect_success 'setup' ' + git update-index --index-version=2 && mkdir sub && touch ./1 ./2 sub/1 sub/2 && git add 1 2 sub/1 sub/2 &&
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 2/22 read-cache: split index file version specific functionality
In reply to Thomas GummererSplit index file version specific functionality to their own functions, to prepare for moving the index file version specific parts to their own file. This makes it easier to add a new index file format later.
--- cache.h | 5 +- read-cache.c | 130 +++++++++++++++++++++++++++++++++------------------ test-index-version.c | 2 +- 3 files changed, 90 insertions(+), 47 deletions(-)
cache.h +4 -1
@@ -100,9 +100,12 @@ unsigned long git_deflate_bound(git_zstream *, unsigned long);*/ #define CACHE_SIGNATURE 0x44495243/* "DIRC" */ -struct cache_header { +struct cache_version_header {unsigned int hdr_signature;unsigned int hdr_version; +}; + +struct cache_header {unsigned int hdr_entries; };
read-cache.c +85 -45
200 lines not on this page · raw message
test-index-version.c +1 -1
@@ -2,7 +2,7 @@int main (int argc,char **argv) { -struct cache_header hdr; +struct cache_version_header hdr;int version;memset (&hdr,0 ,sizeof (hdr));
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 3/22 read-cache: move index v2 specific functions to their own file
In reply to Thomas GummererMove index version 2 specific functions to their own file. The non-index specific functions will be in read-cache.c, while the index version 2 specific functions will be in read-cache-v2.c.
--- Makefile | 2 + cache.h | 16 +- read-cache-v2.c | 556 +++++++++++++++++++++++++++++++++++++++++++++++++ read-cache.c | 575 ++++----------------------------------------------- read-cache.h | 57 +++++ test-index-version.c | 5 + 6 files changed, 661 insertions(+), 550 deletions(-) create mode 100644 read-cache-v2.c create mode 100644 read-cache.h
Makefile +2 -0
@@ -711,6 +711,7 @@ LIB_H += progress.h LIB_H += prompt.h LIB_H += quote.h LIB_H += reachable.h +LIB_H += read-cache.h LIB_H += reflog-walk.h LIB_H += refs.h LIB_H += remote.h
@@ -854,6 +855,7 @@ LIB_OBJS += prompt.o LIB_OBJS += quote.o LIB_OBJS += reachable.o LIB_OBJS += read-cache.o +LIB_OBJS += read-cache-v2.o LIB_OBJS += reflog-walk.o LIB_OBJS += refs.o LIB_OBJS += remote.o
cache.h +3 -13
@@ -95,19 +95,8 @@ unsigned long git_deflate_bound(git_zstream *, unsigned long);*/ #define DEFAULT_GIT_PORT 9418 -/* -* Basic data structures for the directory cache -*/ #define CACHE_SIGNATURE 0x44495243/* "DIRC" */ -struct cache_version_header { -unsigned int hdr_signature; -unsigned int hdr_version; -}; - -struct cache_header { -unsigned int hdr_entries; -};#define INDEX_FORMAT_LB 2#define INDEX_FORMAT_UB 4
@@ -280,6 +269,7 @@ struct index_state { initialized :1 ;struct hash_table name_hash;struct hash_table dir_hash; +struct index_ops *ops; };extern struct index_state the_index;
@@ -489,8 +479,8 @@ extern void *read_blob_data_from_index(struct index_state *, const char *, unsig#define CE_MATCH_RACY_IS_DIRTY 02/* do stat comparison even if CE_SKIP_WORKTREE is true */ #define CE_MATCH_IGNORE_SKIP_WORKTREE 04 -extern int ie_match_stat (const struct index_state *,const struct cache_entry *,struct stat *,unsigned int ); -extern int ie_modified (const struct index_state *,const struct cache_entry *,struct stat *,unsigned int ); +extern int ie_match_stat (struct index_state *,const struct cache_entry *,struct stat *,unsigned int ); +extern int ie_modified (struct index_state *,const struct cache_entry *,struct stat *,unsigned int );#define PATHSPEC_ONESTAR 1/* the pathspec pattern sastisfies GFNM_ONESTAR */
read-cache-v2.c new file +556 -0
556 lines not on this page · raw message
read-cache.c +38 -537
694 lines not on this page · raw message
read-cache.h new file +57 -0
57 lines not on this page · raw message
test-index-version.c +5 -0
@@ -1,5 +1,10 @@#include "cache.h" +struct cache_version_header { +unsigned int hdr_signature; +unsigned int hdr_version; +}; +int main (int argc,char **argv) {struct cache_version_header hdr;
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 4/22 read-cache: Re-read index if index file changed
In reply to Thomas GummererAdd the possibility of re-reading the index file, if it changed while reading.
The index file might change during the read, causing outdated information to be displayed. We check if the index file changed by using its stat data as heuristic.
--- read-cache.c | 91 +++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 57 insertions(+), 34 deletions(-)
read-cache.c +57 -34
@@ -1275,11 +1275,31 @@ int read_index(struct index_state *istate)return read_index_from (istate,get_index_file ()); } +static int index_changed (struct stat *st_old,struct stat *st_new) +{ +if (st_old->st_mtime != st_new->st_mtime || +#if !defined (__CYGWIN__) + st_old->st_uid != st_new->st_uid || + st_old->st_gid != st_new->st_gid || + st_old->st_ino != st_new->st_ino || +#endif +#if USE_NSEC +ST_MTIME_NSEC (*st_old) !=ST_MTIME_NSEC (*st_new) || +#endif +#if USE_STDEV + st_old->st_dev != st_new->st_dev || +#endif + st_old->st_size != st_new->st_size) +return 1 ; + +return 0 ; +} +/* remember to discard_cache() before reading a different cache! */ int read_index_from (struct index_state *istate,const char *path) { -int fd; -struct stat st; +int fd, err, i; +struct stat st_old, st_new;struct cache_version_header *hdr;void *mmap;size_t mmap_size;
@@ -1291,41 +1311,44 @@ int read_index_from(struct index_state *istate, const char *path) errno =ENOENT ; istate->timestamp.sec =0 ; istate->timestamp.nsec =0 ; +for (i =0 ; i <50 ; i++) { + err =0 ; + fd =open (path,O_RDONLY ); +if (fd <0 ) { +if (errno ==ENOENT ) +return 0 ; +die_errno ("index file open failed" ); + } - fd =open (path,O_RDONLY ); -if (fd <0 ) { -if (errno ==ENOENT ) -return 0 ; -die_errno ("index file open failed" ); +if (fstat (fd, &st_old)) +die_errno ("cannot stat the open index" ); + + errno =EINVAL ; + mmap_size =xsize_t (st_old.st_size); + mmap =xmmap (NULL , mmap_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); +close (fd); +if (mmap ==MAP_FAILED ) +die_errno ("unable to map index file" ); + + hdr = mmap; +if (verify_hdr_version (istate, hdr, mmap_size) <0 ) + err =1 ; + +if (istate->ops->verify_hdr (mmap, mmap_size) <0 ) + err =1 ; + +if (istate->ops->read_index (istate, mmap, mmap_size) <0 ) + err =1 ; + istate->timestamp.sec = st_old.st_mtime; + istate->timestamp.nsec =ST_MTIME_NSEC (st_old); +if (lstat (path, &st_new)) +die_errno ("cannot stat the open index" ); + +munmap (mmap, mmap_size); +if (!index_changed (&st_old, &st_new) && !err) +return istate->cache_nr; } -if (fstat (fd, &st)) -die_errno ("cannot stat the open index" ); - - errno =EINVAL ; - mmap_size =xsize_t (st.st_size); - mmap =xmmap (NULL , mmap_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); -close (fd); -if (mmap ==MAP_FAILED ) -die_errno ("unable to map index file" ); - - hdr = mmap; -if (verify_hdr_version (istate, hdr, mmap_size) <0 ) - goto unmap; - -if (istate->ops->verify_hdr (mmap, mmap_size) <0 ) - goto unmap; - -if (istate->ops->read_index (istate, mmap, mmap_size) <0 ) - goto unmap; - istate->timestamp.sec = st.st_mtime; - istate->timestamp.nsec =ST_MTIME_NSEC (st); - -munmap (mmap, mmap_size); -return istate->cache_nr; - -unmap: -munmap (mmap, mmap_size);die ("index file corrupt" ); }
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 5/22 read-cache: add index reading api
In reply to Thomas GummererAdd an api for access to the index file. Currently there is only a very basic api for accessing the index file, which only allows a full read of the index, and lets the users of the data filter it. The new index api gives the users the possibility to use only part of the index and provides functions for iterating over and accessing cache entries.
This simplifies future improvements to the in-memory format, as changes will be concentrated on one file, instead of the whole git source code.
--- cache.h | 57 +++++++++++++++++++++++++++++- read-cache-v2.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++-- read-cache.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- read-cache.h | 12 ++++++- 4 files changed, 263 insertions(+), 10 deletions(-)
cache.h +56 -1
@@ -127,7 +127,8 @@ struct cache_entry {unsigned int ce_flags;unsigned int ce_namelen;unsigned char sha1[20 ]; -struct cache_entry *next; +struct cache_entry *next;/* used by name_hash */ +struct cache_entry *next_ce;/* used to keep a list of cache entries */ char name[FLEX_ARRAY ];/* more */ };
@@ -258,6 +259,32 @@ static inline unsigned int canon_mode(unsigned int mode)#define cache_entry_size (len) (offsetof(struct cache_entry,name) + (len) + 1) +/* +* Options by which the index should be filtered when read partially. +* +* pathspec: The pathspec which the index entries have to match +* seen: Used to return the seen parameter from match_pathspec() +* max_prefix, max_prefix_len: These variables are set to the longest +* common prefix, the length of the longest common prefix of the +* given pathspec +* +* read_staged: used to indicate if the conflicted entries (entries +* with a stage) should be included +* read_cache_tree: used to indicate if the cache-tree should be read +* read_resolve_undo: used to indicate if the resolve undo data should +* be read +*/ +struct filter_opts { +const char **pathspec; +char *seen; +char *max_prefix; +int max_prefix_len; + +int read_staged; +int read_cache_tree; +int read_resolve_undo; +}; +struct index_state {struct cache_entry **cache;unsigned int version;
@@ -270,6 +297,8 @@ struct index_state {struct hash_table name_hash;struct hash_table dir_hash;struct index_ops *ops; +struct internal_ops *internal_ops; +struct filter_opts *filter_opts; };extern struct index_state the_index;
@@ -311,6 +340,17 @@ extern void free_name_hash(struct index_state *istate);#define unmerge_cache_entry_at (at) unmerge_index_entry_at(&the_index, at)#define unmerge_cache (pathspec) unmerge_index(&the_index, pathspec)#define read_blob_data_from_cache (path, sz) read_blob_data_from_index(&the_index, (path), (sz)) + +/* index api */ +#define read_cache_filtered (opts) read_index_filtered(&the_index, (opts)) +#define read_cache_filtered_from (path, opts) read_index_filtered_from(&the_index, (path), (opts)) +#define get_cache_entry_by_name (name, namelen, ce) \ + get_index_entry_by_name(&the_index, (name), (namelen), (ce)) +#define for_each_cache_entry (fn, cb_data) \ + for_each_index_entry(&the_index, (fn), (cb_data)) +#define next_cache_entry (ce) next_index_entry(ce) +#define cache_change_filter_opts (opts) index_change_filter_opts(&the_index, (opts)) +#define sort_cache () sort_index(&the_index)#endif enum object_type {
@@ -438,6 +478,21 @@ extern int init_db(const char *template_dir, unsigned int flags); } \ }while (0 ) +/* index api */ +extern int read_index_filtered(struct index_state *, struct filter_opts *opts); +extern int read_index_filtered_from (struct index_state *,const char *path,struct filter_opts *opts); +extern int get_index_entry_by_name (struct index_state *,const char *name,int namelen, +struct cache_entry **ce); +extern struct cache_entry *next_index_entry (struct cache_entry *ce); +void index_change_filter_opts (struct index_state *istate,struct filter_opts *opts); +void sort_index (struct index_state *istate); + +typedef int each_cache_entry_fn (struct cache_entry *ce,void *); + +extern int for_each_index_entry (struct index_state *istate, +each_cache_entry_fn ,void *); + +/* Initialize and use the cache information */ extern int read_index (struct index_state *);extern int read_index_preload (struct index_state *,const char **pathspec);
read-cache-v2.c +94 -2
144 lines not on this page · raw message
read-cache.c +102 -6
158 lines not on this page · raw message
read-cache.h +11 -1
@@ -24,11 +24,21 @@ struct cache_version_header {struct index_ops {int (*match_stat_basic)(const struct cache_entry *ce,struct stat *st,int changed);int (*verify_hdr)(void *mmap,unsigned long size); -int (*read_index)(struct index_state *istate,void *mmap,unsigned long mmap_size); +int (*read_index)(struct index_state *istate,void *mmap,unsigned long mmap_size, +struct filter_opts *opts);int (*write_index)(struct index_state *istate,int newfd); +void (*index_change_filter_opts)(struct index_state *istate,struct filter_opts *opts); +}; + +struct internal_ops { +int (*for_each_index_entry)(struct index_state *istate,each_cache_entry_fn fn,void *cb_data); +int (*get_index_entry_by_name)(struct index_state *istate,const char *name,int namelen, +struct cache_entry **ce); +void (*sort_index)(struct index_state *istate); };extern struct index_ops v2_ops; +extern struct internal_ops v2_internal_ops;#ifndef NEEDS_ALIGNED_ACCESS #define ntoh_s (var) ntohs(var)
Signature · 1 line
1.8.3.453.g1dfc63d
Re: [PATCH 05/22] read-cache: add index reading api
Quoted 22 lines · Thomas Gummerer
Add an api for access to the index file. Currently there is only a very basic api for accessing the index file, which only allows a full read of the index, and lets the users of the data filter it. The new index api gives the users the possibility to use only part of the index and provides functions for iterating over and accessing cache entries.
This simplifies future improvements to the in-memory format, as changes will be concentrated on one file, instead of the whole git source code.
Signed-off-by Thomas Gummerer <[email protected]>--- cache.h | 57 +++++++++++++++++++++++++++++- read-cache-v2.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++-- read-cache.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- read-cache.h | 12 ++++++- 4 files changed, 263 insertions(+), 10 deletions(-)cache.h +0
-0
cache.h struct cache_entry { · line 131
Quoted 6 lines
cache.h +2
-1@@ -127,7 +127,8 @@ struct cache_entry {unsigned int ce_flags;unsigned int ce_namelen;unsigned char sha1[20 ]; -struct cache_entry *next; +struct cache_entry *next;/* used by name_hash */ +struct cache_entry *next_ce;/* used to keep a list of cache entries */
The reader often needs to rewind the read-pointer partially while walking the index (e.g. next_cache_entry() in unpack-trees.c and how the o->cache_bottom position is used throughout the subsystem). I am not sure if this singly-linked list is a good way to go.
cache.h static inline unsigned int canon_mode(unsigned int mode) · line 269
Quoted 8 lines
cache.h +8
-0+/* +* Options by which the index should be filtered when read partially. +* +* pathspec: The pathspec which the index entries have to match +* seen: Used to return the seen parameter from match_pathspec() +* max_prefix, max_prefix_len: These variables are set to the longest +* common prefix, the length of the longest common prefix of the +* given pathspec
These probably should use "struct pathspec" abstration, not just the "array of raw strings", no?
Re: [PATCH 05/22] read-cache: add index reading api
Quoted 19 lines · Junio C Hamano
Quoted 17 lines · Thomas Gummerer
Add an api for access to the index file. Currently there is only a very basic api for accessing the index file, which only allows a full read of the index, and lets the users of the data filter it. The new index api gives the users the possibility to use only part of the index and provides functions for iterating over and accessing cache entries.
This simplifies future improvements to the in-memory format, as changes will be concentrated on one file, instead of the whole git source code.
Signed-off-by Thomas Gummerer <[email protected]>--- cache.h | 57 +++++++++++++++++++++++++++++- read-cache-v2.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++-- read-cache.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- read-cache.h | 12 ++++++- 4 files changed, 263 insertions(+), 10 deletions(-)
Quoted 16 lines
cache.h struct cache_entry { · line 131
Quoted 11 lines
cache.h +2
-1@@ -127,7 +127,8 @@ struct cache_entry {unsigned int ce_flags;unsigned int ce_namelen;unsigned char sha1[20 ]; -struct cache_entry *next; +struct cache_entry *next;/* used by name_hash */ +struct cache_entry *next_ce;/* used to keep a list of cache entries */ The reader often needs to rewind the read-pointer partially while walking the index (e.g.
next_cache_entry()inunpack-trees.cand how theo->cache_bottomposition is used throughout the subsystem). I am not sure if this singly-linked list is a good way to go.
I'm not very familiar with the unpack-trees code, but from a quick look the pointer (or position in the cache) is always only moved forward. A problem I do see though is skipping a number of entries at once. An example for that below:
int matches;
matches = cache_tree_matches_traversal(o->src_index->cache_tree,
names, info);
/*
* Everything under the name matches; skip the
* entire hierarchy. diff_index_cached codepath
* special cases D/F conflicts in such a way that
* it does not do any look-ahead, so this is safe.
*/
if (matches) {
o->cache_bottom += matches;
return mask;
}This could probably be transformed into something like skip_cache_tree_matches(cache-tree, names, info);
I'll take some time to familiarize myself with the unpack-trees code to see if I can find a better solution than this, and if there are more pitfalls.
Quoted 11 lines
cache.h static inline unsigned int canon_mode(unsigned int mode) · line 269
Quoted 8 lines
cache.h +8
-0+/* +* Options by which the index should be filtered when read partially. +* +* pathspec: The pathspec which the index entries have to match +* seen: Used to return the seen parameter from match_pathspec() +* max_prefix, max_prefix_len: These variables are set to the longest +* common prefix, the length of the longest common prefix of the +* given pathspec These probably should use "struct pathspec" abstration, not just the "array of raw strings", no?
Yes, thanks, that's probably a good idea.
Re: [PATCH 05/22] read-cache: add index reading api
Quoted 7 lines · Thomas Gummerer
Quoted 4 lines
The reader often needs to rewind the read-pointer partially while walking the index (e.g.
next_cache_entry()inunpack-trees.cand how theo->cache_bottomposition is used throughout the subsystem). I am not sure if this singly-linked list is a good way to go.I'm not very familiar with the unpack-trees code, but from a quick look the pointer (or position in the cache) is always only moved forward.
I am more worried about o->cache_bottom processing, where it currently is an index into an array.
With your ce->next_in_list_of_read_entries change, a natural rewrite would be to point at the ce with o->cache_bottom, but then that would mean you cannot in-place replace the entries like we used to be able to in an array based implementation.
But your series does not seem to touch unpack-trees yet, so I may be worried too much before it becomes necessary.
Re: [PATCH 05/22] read-cache: add index reading api
Quoted 13 lines · Junio C Hamano
Quoted 7 lines · Thomas Gummerer
Quoted 4 lines
The reader often needs to rewind the read-pointer partially while walking the index (e.g.
next_cache_entry()inunpack-trees.cand how theo->cache_bottomposition is used throughout the subsystem). I am not sure if this singly-linked list is a good way to go.I'm not very familiar with the unpack-trees code, but from a quick look the pointer (or position in the cache) is always only moved forward.
I am more worried about
o->cache_bottomprocessing, where it currently is an index into an array.
Quoted 7 lines
With your
ce->next_in_list_of_read_entrieschange, a natural rewrite would be to point at the ce witho->cache_bottom, but then that would mean you cannot in-place replace the entries like we used to be able to in an array based implementation.But your series does not seem to touch unpack-trees yet, so I may be worried too much before it becomes necessary.
Yes, you're right, as Duy mentioned in the other email I just responded to it makes sense to keep the index around for now.
I looked at the unpack-trees code a bit, and adding a new api and hiding index_state->cache[] will probably be a bit harder to do than I originally thought, so it's best to keep that around for now, as we're still able to get the benefits from partial loading even if it's not hidden.
Re: [PATCH 05/22] read-cache: add index reading api
In reply to Thomas GummererQuoted 31 lines · Thomas Gummerer
Add an api for access to the index file. Currently there is only a very basic api for accessing the index file, which only allows a full read of the index, and lets the users of the data filter it. The new index api gives the users the possibility to use only part of the index and provides functions for iterating over and accessing cache entries.
This simplifies future improvements to the in-memory format, as changes will be concentrated on one file, instead of the whole git source code.
Signed-off-by Thomas Gummerer <[email protected]>--- cache.h | 57 +++++++++++++++++++++++++++++- read-cache-v2.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++-- read-cache.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- read-cache.h | 12 ++++++- 4 files changed, 263 insertions(+), 10 deletions(-)cache.h +2
-1@@ -127,7 +127,8 @@ struct cache_entry {unsigned int ce_flags;unsigned int ce_namelen;unsigned char sha1[20 ]; -struct cache_entry *next; +struct cache_entry *next;/* used by name_hash */ +struct cache_entry *next_ce;/* used to keep a list of cache entries */ char name[FLEX_ARRAY ];/* more */ };
Quoted 1 line
From what I read, doing
ce = start;
while (ce) { do(something); ce = next_cache_entry(ce); }is the same as
i = start_index;
while (i < active_nr) { ce = active_cache[i]; do(something); i++; }What's the advantage of using the former over the latter? Do you plan to eliminate the latter loop (by hiding "struct cache_entry **cache;" from public index_state structure?
--
Duy
Re: [PATCH 05/22] read-cache: add index reading api
Quoted 37 lines · Duy Nguyen
Quoted 22 lines · Thomas Gummerer
Add an api for access to the index file. Currently there is only a very basic api for accessing the index file, which only allows a full read of the index, and lets the users of the data filter it. The new index api gives the users the possibility to use only part of the index and provides functions for iterating over and accessing cache entries.
This simplifies future improvements to the in-memory format, as changes will be concentrated on one file, instead of the whole git source code.
Signed-off-by Thomas Gummerer <[email protected]>--- cache.h | 57 +++++++++++++++++++++++++++++- read-cache-v2.c | 96 +++++++++++++++++++++++++++++++++++++++++++++++-- read-cache.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- read-cache.h | 12 ++++++- 4 files changed, 263 insertions(+), 10 deletions(-)cache.h +0
-0cache.h struct cache_entry { · line 133
Quoted 8 lines
cache.h +2
-1@@ -127,7 +127,8 @@ struct cache_entry {unsigned int ce_flags;unsigned int ce_namelen;unsigned char sha1[20 ]; -struct cache_entry *next; +struct cache_entry *next;/* used by name_hash */ +struct cache_entry *next_ce;/* used to keep a list of cache entries */ char name[FLEX_ARRAY ];/* more */ };From what I read, doing
ce = start; while (ce) { do(something); ce = next_cache_entry(ce); }
Quoted 8 lines
is the same as
i = start_index; while (i < active_nr) { ce = active_cache[i]; do(something); i++; }What's the advantage of using the former over the latter? Do you plan to eliminate the latter loop (by hiding "struct
cache_entry**cache;" from publicindex_statestructure?
Yes, I wanted to eliminate the latter loop, because it depends on the in-memory format of the index. By moving all direct accesses of index_state->cache to an api it gets easier to change the in-memory format. I played a bit with a tree-based in-memory format [1], which represents the on-disk format of index-v5 more closely, making modifications and partial-loading simpler.
I've tried switching all those loops to api calls, but that would make the api too bloated because there is a lot of those loops. I found it more sensible to do it this way, leaving the loops how they are, while making future changes to the in-memory format a lot simpler.
[1] https://github.com/tgummerer/git/blob/index-v5api/read-cache-v5.c#L17
Re: [PATCH 05/22] read-cache: add index reading api
In reply to Thomas GummererQuoted 36 lines · Thomas Gummerer
cache.h +28
-0+/* +* Options by which the index should be filtered when read partially. +* +* pathspec: The pathspec which the index entries have to match +* seen: Used to return the seen parameter from match_pathspec() +* max_prefix, max_prefix_len: These variables are set to the longest +* common prefix, the length of the longest common prefix of the +* given pathspec +* +* read_staged: used to indicate if the conflicted entries (entries +* with a stage) should be included +* read_cache_tree: used to indicate if the cache-tree should be read +* read_resolve_undo: used to indicate if the resolve undo data should +* be read +*/ +struct filter_opts { +const char **pathspec; +char *seen; +char *max_prefix; +int max_prefix_len; + +int read_staged; +int read_cache_tree; +int read_resolve_undo; +}; +struct index_state {struct cache_entry **cache;unsigned int version;@@ -270,6 +297,8 @@ struct index_state {struct hash_table name_hash;struct hash_table dir_hash;struct index_ops *ops; +struct internal_ops *internal_ops; +struct filter_opts *filter_opts; };
...
Quoted 18 lines
read-cache.c +4
-3-/* remember to discard_cache() before reading a different cache! */ -int read_index_from (struct index_state *istate,const char *path) + +int read_index_filtered_from (struct index_state *istate,const char *path, +struct filter_opts *opts) {int fd, err, i;struct stat st_old, st_new;@@ -1337,7 +1425,7 @@ int read_index_from(struct index_state *istate, const char *path)if (istate->ops->verify_hdr (mmap, mmap_size) <0 ) err =1 ; -if (istate->ops->read_index (istate, mmap, mmap_size) <0 ) +if (istate->ops->read_index (istate, mmap, mmap_size, opts) <0 ) err =1 ; istate->timestamp.sec = st_old.st_mtime; istate->timestamp.nsec =ST_MTIME_NSEC (st_old);
read-cache.c int read_index_from(struct index_state *istate, const char *path) · line 1439
Quoted 7 lines
read-cache.c +1
-0@@ -1345,6 +1433,7 @@ int read_index_from(struct index_state *istate, const char *path)die_errno ("cannot stat the open index" );munmap (mmap, mmap_size); + istate->filter_opts = opts;if (!index_changed (&st_old, &st_new) && !err)return istate->cache_nr; }
Putting filter_opts in index_state feels like a bad design. Iterator information should be separated from the iterated object, so that two callers can walk through the same index without stepping on each other (I'm not talking about multithreading, a caller may walk a bit, then the other caller starts walking, then the former caller resumes walking again in a call chain).
--
Duy
Re: [PATCH 05/22] read-cache: add index reading api
Quoted 60 lines · Duy Nguyen
Quoted 36 lines · Thomas Gummerer
cache.h +28
-0+/* +* Options by which the index should be filtered when read partially. +* +* pathspec: The pathspec which the index entries have to match +* seen: Used to return the seen parameter from match_pathspec() +* max_prefix, max_prefix_len: These variables are set to the longest +* common prefix, the length of the longest common prefix of the +* given pathspec +* +* read_staged: used to indicate if the conflicted entries (entries +* with a stage) should be included +* read_cache_tree: used to indicate if the cache-tree should be read +* read_resolve_undo: used to indicate if the resolve undo data should +* be read +*/ +struct filter_opts { +const char **pathspec; +char *seen; +char *max_prefix; +int max_prefix_len; + +int read_staged; +int read_cache_tree; +int read_resolve_undo; +}; +struct index_state {struct cache_entry **cache;unsigned int version;@@ -270,6 +297,8 @@ struct index_state {struct hash_table name_hash;struct hash_table dir_hash;struct index_ops *ops; +struct internal_ops *internal_ops; +struct filter_opts *filter_opts; };...
Quoted 20 lines
read-cache.c +4
-3-/* remember to discard_cache() before reading a different cache! */ -int read_index_from (struct index_state *istate,const char *path) + +int read_index_filtered_from (struct index_state *istate,const char *path, +struct filter_opts *opts) {int fd, err, i;struct stat st_old, st_new;@@ -1337,7 +1425,7 @@ int read_index_from(struct index_state *istate, const char *path)if (istate->ops->verify_hdr (mmap, mmap_size) <0 ) err =1 ; -if (istate->ops->read_index (istate, mmap, mmap_size) <0 ) +if (istate->ops->read_index (istate, mmap, mmap_size, opts) <0 ) err =1 ; istate->timestamp.sec = st_old.st_mtime; istate->timestamp.nsec =ST_MTIME_NSEC (st_old);@@ -1345,6 +1433,7 @@ int read_index_from(struct index_state *istate, const char *path)die_errno ("cannot stat the open index" );
Quoted 12 lines
read-cache.c int read_index_from(struct index_state *istate, const char *path) · line 1439
Quoted 5 lines
read-cache.c +1
-0munmap (mmap, mmap_size); + istate->filter_opts = opts;if (!index_changed (&st_old, &st_new) && !err)return istate->cache_nr; }Putting
filter_optsinindex_statefeels like a bad design. Iterator information should be separated from the iterated object, so that two callers can walk through the same index without stepping on each other (I'm not talking about multithreading, a caller may walk a bit, then the other caller starts walking, then the former caller resumes walking again in a call chain).
Yes, you're right. We need the filter_opts to see what part of the index has been loaded [1] and which part has been skipped, but it shouldn't be used for filtering in the for_each_index_entry function.
I think there should be two versions of the for_each_index_entry
function then, where the for_each_index_entry function would behave the
same way as the for_each_index_entry_filtered function with thefilter_opts parameter set to NULL:for_each_index_entry_filtered(struct index_state *, each_cache_entry_fn, void *cb_data, struct filter_opts *) for_each_index_entry(struct index_state *, each_cache_entry_fn, void *cb_data)
Both of them then should call index_change_filter_opts to make sure all the entries that are needed are loaded in the in-memory format.
Does that make sense?
[1] That is only important for the new index-v5 file format, which can be loaded partially. The filter_opts could always be set to NULL, as the whole index is always loaded anyway.
Re: [PATCH 05/22] read-cache: add index reading api
Quoted 19 lines · Thomas Gummerer
Quoted 6 lines · Duy Nguyen
Putting
filter_optsinindex_statefeels like a bad design. Iterator information should be separated from the iterated object, so that two callers can walk through the same index without stepping on each other (I'm not talking about multithreading, a caller may walk a bit, then the other caller starts walking, then the former caller resumes walking again in a call chain).Yes, you're right. We need the
filter_optsto see what part of the index has been loaded [1] and which part has been skipped, but it shouldn't be used for filtering in thefor_each_index_entryfunction.I think there should be two versions of the
for_each_index_entry
function then, where thefor_each_index_entryfunction would behave the
same way as thefor_each_index_entry_filteredfunction with thefilter_optsparameter set to NULL:for_each_index_entry_filtered(struct index_state *, each_cache_entry_fn, void *cb_data, struct filter_opts *)for_each_index_entry(struct index_state *, each_cache_entry_fn, void *cb_data)
Quoted 4 lines
Both of them then should call
index_change_filter_optsto make sure all the entries that are needed are loaded in the in-memory format.Does that make sense?
Hmm.. I was confused actually (documentation on the api would help greatly). If you already filter at load time, I don't think you need to match again. The caller asked for filter and it should know what's in there so for_each_index_entry just goes through all entries without extra match_pathspec. Or is that what next_index_entry for? match_pathspec function could be expensive when glob is involved. If the caller wants extra matching, it could do inside the callback function.
It seems you could change the filter with index_change_filter_opts. In v5 the index will be reloaded. What happens when some index entries area already modified? Do we start to have read-only index "views" and one read-write view? If partial views are always read-only, perhaps we just allow the user to create a new index_state (or view) with new filter and destroy the old one. We don't have to care about changing or separating filter in that case because the view is the iterator.
I wanted to have a tree-based iterator api, but that seems incompatible with pre-v5 (or at least adds some overhead on pre-v5 to rebuild the tree structure). It looks like using pathspec to build a list of entries, as you did, is a good way to take advantage of tree-based v5 while maintaining code compatibility with pre-v5. By the way with tree structure, you could use tree_entry_interesting in read_index_filtered_v5. I think it's more efficient than match_pathspec.
I'm still studying the code. Some of what I wrote here may be totally wrong due to my lack of understanding. I'll get back to you later if I find something else.
Quoted 3 lines
[1] That is only important for the new index-v5 file format, which can be loaded partially. The
filter_optscould always be set to NULL, as the whole index is always loaded anyway.
--
Duy
PATCH 1/1 Add documentation for the index api
Document the new index api and add examples of how it should be used instead of the old functions directly accessing the index.
---
Quoted 2 lines · Duy Nguyen
Hmm.. I was confused actually (documentation on the api would help greatly).
As promised, a draft for a documentation for the index api as it is in this series.
Documentation/technical/api-in-core-index.txt | 108 +++++++++++++++++++++++++-
1 file changed, 106 insertions(+), 2 deletions(-)
Documentation/technical/api-in-core-index.txt +106 -2
@@ -1,14 +1,116 @@ in-core index API ================= +Reading API +----------- + +`read_index()`:: + Read the whole index file from disk. + +`index_name_pos(name, namelen)`:: + Find a cache_entry with name in the index. Returns pos if an + entry is matched exactly and -pos-1 if an entry is matched + partially. + e.g. + index: + file1 + file2 + path/file1 + zzz + + index_name_pos("path/file1", 10) returns 2, while + index_name_pos("path", 4) returns -1 + +`read_index_filtered(opts)`:: + This method behaves differently for index-v2 and index-v5. + + For index-v2 it simply reads the whole index as read_index() + does, so we are sure we don't have to reload anything if the + user wants a different filter. It also sets the filter_opts + in the index_state, which is used to limit the results when + iterating over the index with for_each_index_entry(). + + The whole index is read to avoid the need to eventually + re-read the index later, because the performance is no + different when reading it partially. + + For index-v5 it creates an adjusted_pathspec to filter the + reading. First all the directory entries are read and then + the cache_entries in the directories that match the adjusted + pathspec are read. The filter_opts in the index_state are set + to filter out the rest of the cache_entries that are matched + by the adjusted pathspec but not by the pathspec given. The + rest of the index entries are filtered out when iterating over + the cache with for_each_index_entries. + +`get_index_entry_by_name(name, namelen, &ce)`:: + Returns a cache_entry matched by the name, returned via the + &ce parameter. If a cache entry is matched exactly, 1 is + returned, otherwise 0. For an example see index_name_pos(). + This function should be used instead of the index_name_pos() + function to retrieve cache entries. + +`for_each_index_entry(fn, cb_data)`:: + Iterates over all cache_entries in the index filtered by + filter_opts in the index_stat. For each cache entry fn is + executed with cb_data as callback data. From within the loop + do `return 0` to continue, or `return 1` to break the loop. + +`next_index_entry(ce)`:: + Returns the cache_entry that follows after ce + +`index_change_filter_opts(opts)`:: + This function again has a slightly different functionality for + index-v2 and index-v5. + + For index-v2 it simply changes the filter_opts, so + for_each_index_entry uses the changed index_opts, to iterate + over a different set of cache entries. + + For index-v5 it refreshes the index if the filter_opts have + changed and sets the new filter_opts in the index state, again + to iterate over a different set of cache entries as with + index-v2. + + This has some optimization potential, in the case that the + opts get stricter (less of the index should be read) it + doesn't have to reload anything, but currently does. + +Using the new index api +----------------------- + +Currently loops over a specific set of index entry were written as: + i = start_index; + while (i < active_nr) { ce = active_cache[i]; do(something); i++; } + +they should be rewritten to: + ce = start; + while (ce) { do(something); ce = next_cache_entry(ce); } + +which is the equivalent operation but hides the in-memory format of +the index from the user. + +For getting a cache entry get_cache_entry_by_name() should be used +instead of cache_name_pos(). e.g.: + int pos = cache_name_pos(name, namelen); + struct cache_entry *ce = active_cache[pos]; + if (pos < 0) { do(something) } + else { do(somethingelse) } + +should be written as: + struct cache_entry *ce; + int ret = get_cache_entry_by_name(name, namelen, &ce); + if (!ret) { do(something) } + else { do(somethingelse) } + +TODO +---- Talk about <read-cache.c> and <cache-tree.c>, things like: * cache -> the_index macros -* read_index() * write_index() * ie_match_stat() and ie_modified(); how they are different and when to use which. -* index_name_pos() * remove_index_entry_at() * remove_file_from_index() * add_file_to_index()
@@ -18,4 +120,6 @@ Talk about <read-cache.c> and <cache-tree.c>, things like: * cache_tree_invalidate_path() * cache_tree_update() + + (JC, Linus)
--
1.8.3.453.g1dfc63d
Re: [PATCH 5.5/22] Add documentation for the index api
Quoted 2 lines · Thomas Gummerer
As promised, a draft for a documentation for the index api as it is in this series.
First of all, it may be a good idea to acknowledge index_state->cache[] as part of the API for now. Not hiding it simplifies a few things (no need for new next_ce field, no worries about rewinding in unpack-trees..). Supporting partial loading (and maybe partial update in some cases) with this API and index_state->cache[] part of the API are good enough for me. We can do another tree-based API or something update later when it's formed (I looked at your index-v5api branch but I don't think a tree-based api was there, my concern is how much extra head pre-v5 has to pay to use tree-based api).
Quoted 11 lines
+
read_index_filtered(opts)::Documentation/technical/api-in-core-index.txt +10
-0+ This method behaves differently for index-v2 and index-v5. + + For index-v2 it simply reads the whole index as read_index() + does, so we are sure we don't have to reload anything if the + user wants a different filter. It also sets the filter_opts + in the index_state, which is used to limit the results when + iterating over the index with for_each_index_entry(). + + The whole index is read to avoid the need to eventually + re-read the index later, because the performance is no
Documentation/technical/api-in-core-index.txt line 44
Quoted 10 lines
Documentation/technical/api-in-core-index.txt +10
-0+ different when reading it partially. + + For index-v5 it creates an adjusted_pathspec to filter the + reading. First all the directory entries are read and then + the cache_entries in the directories that match the adjusted + pathspec are read. The filter_opts in the index_state are set + to filter out the rest of the cache_entries that are matched + by the adjusted pathspec but not by the pathspec given. The + rest of the index entries are filtered out when iterating over + the cache with for_each_index_entries.
You can state in the API that the input pathspec is used as a hint to load only a portion of the index. read_index_filtered may load _more_ than necessary. It's the caller's responsibility to verify again which is matched and which is not. That's how read_directory is done. I think it gives you more liberty in loading strategy. It's already true for v2 because full index is loaded regardless of the given pathspec. In the end, we have a linear list (from public view) of cache entries, accessible via index_state->cache[].
If you happen to know that certain entries match the given pathspec, you could help the caller avoid match_pathspec'ing again by set a bit in ce_flags. To know which entry exists in the index and which is new, use another flag. Most reader code won't change if we do it this way, all match_pathspec() remain where they are.
Documentation/technical/api-in-core-index.txt line 57
Quoted 5 lines
+
for_each_index_entry(fn, cb_data)::Documentation/technical/api-in-core-index.txt +4
-0+ Iterates over all cache_entries in the index filtered by + filter_opts in the index_stat. For each cache entry fn is + executed with cb_data as callback data. From within the loop + do `return 0` to continue, or `return 1` to break the loop.
Because we don't attempt to hide index_state->cache[], this one may be for convenience, the user is not required to convert to it. Actually I think this may be slower because of the cost of calling function pointer.
Quoted 2 lines
+
next_index_entry(ce)::
+ Returns thecache_entrythat follows after ce
next_ce field and this method may be gone too, just access index_state->cache[]
Quoted 6 lines
+
index_change_filter_opts(opts)::Documentation/technical/api-in-core-index.txt +5
-0+ This function again has a slightly different functionality for + index-v2 and index-v5. + + For index-v2 it simply changes the filter_opts, so + for_each_index_entry uses the changed index_opts, to iterate
Documentation/technical/api-in-core-index.txt line 77
Quoted 10 lines
Documentation/technical/api-in-core-index.txt +10
-0+ over a different set of cache entries. + + For index-v5 it refreshes the index if the filter_opts have + changed and sets the new filter_opts in the index state, again + to iterate over a different set of cache entries as with + index-v2. + + This has some optimization potential, in the case that the + opts get stricter (less of the index should be read) it + doesn't have to reload anything, but currently does.
The only use case I see so far is converting a partial index_state back to a full one. Apart from doing so in order to write the new index, I think some operation (like rename tracking in diff or unpack-trees) may expect full index. I think we should support that. I doubt we need to change pathspec to something different than the one we used to load the index. When a user passes a pathspec to a command, the user expects the command to operate on that set only, not outside.
If you take the input pathspec at loading just as a hint, you could load all related directory blocks and all files in those blocks, so that expanding to full index is simply adding more files from missing directory blocks (and their files). An advantage of not strictly follow the input pathspec.
Some thoughts about the writing api.
In think we should avoid automatically converting partial index into a full one before writing. Push that back to the caller and die() when asked to update partial index. They know at what point the index may be updated and even what part of it may be updated. I think all commands fall into two categories, tree-wide updates (merge, checkout...) and limited by the user-given pathspec. "what part to be updated" is not so hard to determine.
If the caller promises not to update or read outside certain pathspec (part of API), we don't really need to load full index until write_index is called. At that point I think we also know what directory blocks are completely untouched by the caller (by promise) and could copy them over from the old index byte-by-byte (or something close, some offsets may be recalculated). That may keep tree compiling cost proportional to the number of changed entries, not the number of entries in index.
There is another partial write case that's not covered this round (or was it discussed and discarded?): refreshing the index. This operation could be treated as a standalone, special one: refresh and update the index file directly without waiting until write_index is called. There are some commands that follow this scheme by doing update_index_if_able() after refresh_index(). Those will be cheaper with v5 because we don't need write a full new index.
--
Duy
Re: [PATCH 5.5/22] Add documentation for the index api
Quoted 4 lines · Duy Nguyen
Quoted 2 lines · Thomas Gummerer
As promised, a draft for a documentation for the index api as it is in this series.
Quoted 10 lines
First of all, it may be a good idea to acknowledge index_state->cache[] as part of the API for now. Not hiding it simplifies a few things (no need for new
next_cefield, no worries about rewinding in unpack-trees..). Supporting partial loading (and maybe partial update in some cases) with this API and index_state->cache[] part of the API are good enough for me. We can do another tree-based API or something update later when it's formed (I looked at your index-v5api branch but I don't think a tree-based api was there, my concern is how much extra head pre-v5 has to pay to use tree-based api).
Yes, I think you're right, that simplifies everything a lot, while we still can have partial loading. Hiding index_state->cache[] was mainly thought for future changes for the in-memory format, but I think that will not be happening for a while.
Quoted 22 lines
Quoted 11 lines
+
read_index_filtered(opts)::Documentation/technical/api-in-core-index.txt +10
-0+ This method behaves differently for index-v2 and index-v5. + + For index-v2 it simply reads the whole index as read_index() + does, so we are sure we don't have to reload anything if the + user wants a different filter. It also sets the filter_opts + in the index_state, which is used to limit the results when + iterating over the index with for_each_index_entry(). + + The whole index is read to avoid the need to eventually + re-read the index later, because the performance is noDocumentation/technical/api-in-core-index.txt line 44
Quoted 10 lines
Documentation/technical/api-in-core-index.txt +10
-0+ different when reading it partially. + + For index-v5 it creates an adjusted_pathspec to filter the + reading. First all the directory entries are read and then + the cache_entries in the directories that match the adjusted + pathspec are read. The filter_opts in the index_state are set + to filter out the rest of the cache_entries that are matched + by the adjusted pathspec but not by the pathspec given. The + rest of the index entries are filtered out when iterating over + the cache with for_each_index_entries.
Quoted 8 lines
You can state in the API that the input pathspec is used as a hint to load only a portion of the index.
read_index_filteredmay load _more_ than necessary. It's the caller's responsibility to verify again which is matched and which is not. That's howread_directoryis done. I think it gives you more liberty in loading strategy. It's already true for v2 because full index is loaded regardless of the given pathspec. In the end, we have a linear list (from public view) of cache entries, accessible via index_state->cache[].
Yes, and it's also partly true for index-v5, as the full content of a directory is loaded even if only some files it it match the pathspec that's given.
Quoted 3 lines
If you happen to know that certain entries match the given pathspec, you could help the caller avoid match_pathspec'ing again by set a bit in
ce_flags.
I currently don't know which entries do match the pathspec from just reading the index file, additional calls would be needed. I don't think that would be worth the overhead.
Quoted 3 lines
To know which entry exists in the index and which is
new, use another flag. Most reader code won't change if we do it this way, allmatch_pathspec()remain where they are.
Hrm you mean to know which cache entries are added (or changed) in the in-memory index and will have to be written later? I'm not sure I understand correctly what you mean here.
Quoted 10 lines
Documentation/technical/api-in-core-index.txt line 57
Quoted 5 lines
+
for_each_index_entry(fn, cb_data)::Documentation/technical/api-in-core-index.txt +4
-0+ Iterates over all cache_entries in the index filtered by + filter_opts in the index_stat. For each cache entry fn is + executed with cb_data as callback data. From within the loop + do `return 0` to continue, or `return 1` to break the loop.Because we don't attempt to hide index_state->cache[], this one may be for convenience, the user is not required to convert to it. Actually I think this may be slower because of the cost of calling function pointer.
Yes right, I think you're right. In fact I just tested it, and it's slightly slower.
I still think it would make sense to keep it around, for the callers that want the cache filtered exactly by the filter_opts, for convenience as you said.
Quoted 4 lines
Quoted 2 lines
+
next_index_entry(ce)::
+ Returns thecache_entrythat follows after ce
next_cefield and this method may be gone too, just access index_state->cache[]
Yes, this makes no sense when we're not hiding index_state->cache[]. The same goes for the get_index_entry_by_name function, which is essentially the same as using index_name_pos and then getting the cache entry from index_state->cache[].
Quoted 17 lines
Quoted 6 lines
+
index_change_filter_opts(opts)::Documentation/technical/api-in-core-index.txt +5
-0+ This function again has a slightly different functionality for + index-v2 and index-v5. + + For index-v2 it simply changes the filter_opts, so + for_each_index_entry uses the changed index_opts, to iterateDocumentation/technical/api-in-core-index.txt line 77
Quoted 10 lines
Documentation/technical/api-in-core-index.txt +10
-0+ over a different set of cache entries. + + For index-v5 it refreshes the index if the filter_opts have + changed and sets the new filter_opts in the index state, again + to iterate over a different set of cache entries as with + index-v2. + + This has some optimization potential, in the case that the + opts get stricter (less of the index should be read) it + doesn't have to reload anything, but currently does.
Quoted 7 lines
The only use case I see so far is converting a partial
index_stateback to a full one. Apart from doing so in order to write the new index, I think some operation (like rename tracking in diff or unpack-trees) may expect full index. I think we should support that. I doubt we need to change pathspec to something different than the one we used to load the index. When a user passes a pathspec to a command, the user expects the command to operate on that set only, not outside.
One application was in ls-files, where we strip the trailing slash from the pathspecs for submodules. But when we let the caller filter the rest out it's not needed anymore. We load all entries without the trailing slash anyway.
Quoted 5 lines
If you take the input pathspec at loading just as a hint, you could load all related directory blocks and all files in those blocks, so that expanding to full index is simply adding more files from missing directory blocks (and their files). An advantage of not strictly follow the input pathspec.
I actually already do that with the adjusted pathspec. Even with index-v5 currently there are some more entries loaded than actually matched by the pathspecs. Expanding to the full index still takes some work, but should be possible.
Quoted 9 lines
Some thoughts about the writing api.
In think we should avoid automatically converting partial index into a full one before writing. Push that back to the caller and
die()when asked to update partial index. They know at what point the index may be updated and even what part of it may be updated. I think all commands fall into two categories, tree-wide updates (merge, checkout...) and limited by the user-given pathspec. "what part to be updated" is not so hard to determine.
Hrm this is only true if index entries are added or removed, not if they are only changed. If they are only changed we can write a partially read index once we have partial writing. For now it would make sense to just die() though, until we have that in place.
Quoted 8 lines
If the caller promises not to update or read outside certain pathspec (part of API), we don't really need to load full index until
write_indexis called. At that point I think we also know what directory blocks are completely untouched by the caller (by promise) and could copy them over from the old index byte-by-byte (or something close, some offsets may be recalculated). That may keep tree compiling cost proportional to the number of changed entries, not the number of entries in index.
Yes that would make sense. I think that should go in a follow-up series though as it would be quite some work.
Quoted 7 lines
There is another partial write case that's not covered this round (or was it discussed and discarded?): refreshing the index. This operation could be treated as a standalone, special one: refresh and update the index file directly without waiting until
write_indexis called. There are some commands that follow this scheme by doingupdate_index_if_able()afterrefresh_index(). Those will be cheaper with v5 because we don't need write a full new index.
I don't think it was discussed yet. Partial reading will need a change to the lock-file structure though, so I think it's a little more complicated.
Thanks for your comments, I'll try to address them and send a new series in a couple of days.
Re: [PATCH 5.5/22] Add documentation for the index api
Quoted 7 lines · Thomas Gummerer
Quoted 3 lines
If you happen to know that certain entries match the given pathspec, you could help the caller avoid match_pathspec'ing again by set a bit in
ce_flags.I currently don't know which entries do match the pathspec from just reading the index file, additional calls would be needed. I don't think that would be worth the overhead.
Yeah I now see that you select what to load in v5 with the adjusted pathspec, not the input pathspec. Originally I thought you match the input pathspec against every file entry in the index :P Your adjusted pathspec looks like what common_prefix is for. It's cheaper than creating adjusted_pathspec from match_pathspec and reduces loading in major cases, where glob is not used.
Still, creating an adjusted pathspec this way looks iffy. You need to understand pathspec in order to strip the filename part out to match the directory match only. An alternative is use tree_entry_interesting. It goes along well with tree traversal and can be used to match directories with original pathspec. Once you see it matches an entry in a directory, you could skip matching the rest of the files and load the whole directory. read_index_filtered_v5 and read_entries may need some tweaking though. I'll try it and post a patch later if I succeed.
Quoted 7 lines
Quoted 3 lines
To know which entry exists in the index and which is
new, use another flag. Most reader code won't change if we do it this way, allmatch_pathspec()remain where they are.Hrm you mean to know which cache entries are added (or changed) in the in-memory index and will have to be written later? I'm not sure I understand correctly what you mean here.
Oh.. The "to know.." sentence was nonsense. We probably don't need to know. We may track changed entries for partial writing, but let's leave that out for now.
Quoted 25 lines
Quoted 17 lines
Quoted 6 lines
+
index_change_filter_opts(opts)::Documentation/technical/api-in-core-index.txt +5
-0+ This function again has a slightly different functionality for + index-v2 and index-v5. + + For index-v2 it simply changes the filter_opts, so + for_each_index_entry uses the changed index_opts, to iterateDocumentation/technical/api-in-core-index.txt line 77
Quoted 10 lines
Documentation/technical/api-in-core-index.txt +10
-0+ over a different set of cache entries. + + For index-v5 it refreshes the index if the filter_opts have + changed and sets the new filter_opts in the index state, again + to iterate over a different set of cache entries as with + index-v2. + + This has some optimization potential, in the case that the + opts get stricter (less of the index should be read) it + doesn't have to reload anything, but currently does.Quoted 7 lines
The only use case I see so far is converting a partial
index_stateback to a full one. Apart from doing so in order to write the new index, I think some operation (like rename tracking in diff or unpack-trees) may expect full index. I think we should support that. I doubt we need to change pathspec to something different than the one we used to load the index. When a user passes a pathspec to a command, the user expects the command to operate on that set only, not outside.
Quoted 4 lines
One application was in ls-files, where we strip the trailing slash from the pathspecs for submodules. But when we let the caller filter the rest out it's not needed anymore. We load all entries without the trailing slash anyway.
That submodule trailing slash stripping code will be moved away soon (I've been working on it for some time now). There's similar code in pathspec.c. I hope by the time this series becomes a candidate for 'next', those pathspec manipulation is already gone. For strip_trailing_slash_from_submodules, peeking in index file for a few entries is probably ok. For check_path_for_gitlink, full index is loaded until we figure out a clever way.
Quoted 10 lines
Quoted 9 lines
Some thoughts about the writing api.
In think we should avoid automatically converting partial index into a full one before writing. Push that back to the caller and
die()when asked to update partial index. They know at what point the index may be updated and even what part of it may be updated. I think all commands fall into two categories, tree-wide updates (merge, checkout...) and limited by the user-given pathspec. "what part to be updated" is not so hard to determine.
Quoted 3 lines
Hrm this is only true if index entries are added or removed, not if they are only changed. If they are only changed we can write a partially read index once we have partial writing.
Yep. We can detect if changes are updates only, no additions nor removals. If so do partial write, else full write. These little details are hidden from the user, as long as they keep their promise about read/write regions.
Quoted 1 line
For now it would make sense to just
die()though, until we have that in place.
Agreed.
--
Duy
Re: [PATCH 5.5/22] Add documentation for the index api
Quoted 16 lines · Duy Nguyen
Quoted 7 lines · Thomas Gummerer
Quoted 3 lines
If you happen to know that certain entries match the given pathspec, you could help the caller avoid match_pathspec'ing again by set a bit in
ce_flags.I currently don't know which entries do match the pathspec from just reading the index file, additional calls would be needed. I don't think that would be worth the overhead.
Yeah I now see that you select what to load in v5 with the adjusted pathspec, not the input pathspec. Originally I thought you match the input pathspec against every file entry in the index :P Your adjusted pathspec looks like what
common_prefixis for. It's cheaper than creatingadjusted_pathspecfrommatch_pathspecand reduces loading in major cases, where glob is not used.
Quoted 9 lines
Still, creating an adjusted pathspec this way looks iffy. You need to understand pathspec in order to strip the filename part out to match the directory match only. An alternative is use
tree_entry_interesting. It goes along well with tree traversal and can be used to match directories with original pathspec. Once you see it matches an entry in a directory, you could skip matching the rest of the files and load the whole directory.read_index_filtered_v5andread_entriesmay need some tweaking though. I'll try it and post a patch later if I succeed.
Hrm, I played around a bit with this idea, but I couldn't figure out how to make it work. For it to work we would still have to load some entries in a directory at least? Or is there a way to match the directories, which I just haven't figured out yet?
Quoted 11 lines
Quoted 7 lines
Quoted 3 lines
To know which entry exists in the index and which is
new, use another flag. Most reader code won't change if we do it this way, allmatch_pathspec()remain where they are.Hrm you mean to know which cache entries are added (or changed) in the in-memory index and will have to be written later? I'm not sure I understand correctly what you mean here.
Oh.. The "to know.." sentence was nonsense. We probably don't need to know. We may track changed entries for partial writing, but let's leave that out for now.
Ok, makes sense.
Quoted 30 lines
Quoted 25 lines
Quoted 17 lines
Quoted 6 lines
+
index_change_filter_opts(opts)::Documentation/technical/api-in-core-index.txt +5
-0+ This function again has a slightly different functionality for + index-v2 and index-v5. + + For index-v2 it simply changes the filter_opts, so + for_each_index_entry uses the changed index_opts, to iterateDocumentation/technical/api-in-core-index.txt line 77
Quoted 10 lines
Documentation/technical/api-in-core-index.txt +10
-0+ over a different set of cache entries. + + For index-v5 it refreshes the index if the filter_opts have + changed and sets the new filter_opts in the index state, again + to iterate over a different set of cache entries as with + index-v2. + + This has some optimization potential, in the case that the + opts get stricter (less of the index should be read) it + doesn't have to reload anything, but currently does.Quoted 7 lines
The only use case I see so far is converting a partial
index_stateback to a full one. Apart from doing so in order to write the new index, I think some operation (like rename tracking in diff or unpack-trees) may expect full index. I think we should support that. I doubt we need to change pathspec to something different than the one we used to load the index. When a user passes a pathspec to a command, the user expects the command to operate on that set only, not outside.Quoted 4 lines
One application was in ls-files, where we strip the trailing slash from the pathspecs for submodules. But when we let the caller filter the rest out it's not needed anymore. We load all entries without the trailing slash anyway.
Quoted 7 lines
That submodule trailing slash stripping code will be moved away soon (I've been working on it for some time now). There's similar code in
pathspec.c. I hope by the time this series becomes a candidate for 'next', those pathspec manipulation is already gone. Forstrip_trailing_slash_from_submodules, peeking in index file for a few entries is probably ok. Forcheck_path_for_gitlink, full index is loaded until we figure out a clever way.
Ah great, for now I'll just not use the for_each_index_entry function in ls-files, and then change the code later once the stripping code is moved away.
Quoted 24 lines
Quoted 10 lines
Quoted 9 lines
Some thoughts about the writing api.
In think we should avoid automatically converting partial index into a full one before writing. Push that back to the caller and
die()when asked to update partial index. They know at what point the index may be updated and even what part of it may be updated. I think all commands fall into two categories, tree-wide updates (merge, checkout...) and limited by the user-given pathspec. "what part to be updated" is not so hard to determine.Quoted 3 lines
Hrm this is only true if index entries are added or removed, not if they are only changed. If they are only changed we can write a partially read index once we have partial writing.
Yep. We can detect if changes are updates only, no additions nor removals. If so do partial write, else full write. These little details are hidden from the user, as long as they keep their promise about read/write regions.
Quoted 1 line
For now it would make sense to just
die()though, until we have that in place.Agreed.
--
Duy
Re: [PATCH 5.5/22] Add documentation for the index api
Quoted 28 lines · Thomas Gummerer
Quoted 16 lines · Duy Nguyen
Quoted 7 lines · Thomas Gummerer
Quoted 3 lines
If you happen to know that certain entries match the given pathspec, you could help the caller avoid match_pathspec'ing again by set a bit in
ce_flags.I currently don't know which entries do match the pathspec from just reading the index file, additional calls would be needed. I don't think that would be worth the overhead.
Yeah I now see that you select what to load in v5 with the adjusted pathspec, not the input pathspec. Originally I thought you match the input pathspec against every file entry in the index :P Your adjusted pathspec looks like what
common_prefixis for. It's cheaper than creatingadjusted_pathspecfrommatch_pathspecand reduces loading in major cases, where glob is not used.Quoted 9 lines
Still, creating an adjusted pathspec this way looks iffy. You need to understand pathspec in order to strip the filename part out to match the directory match only. An alternative is use
tree_entry_interesting. It goes along well with tree traversal and can be used to match directories with original pathspec. Once you see it matches an entry in a directory, you could skip matching the rest of the files and load the whole directory.read_index_filtered_v5andread_entriesmay need some tweaking though. I'll try it and post a patch later if I succeed.
Quoted 4 lines
Hrm, I played around a bit with this idea, but I couldn't figure out how to make it work. For it to work we would still have to load some entries in a directory at least? Or is there a way to match the directories, which I just haven't figured out yet?
Yes you have to load some entries first. Even if a directory does not match, we only know until at least the first file in the directory. OK there might be problems because tree_entry_interesting expects all entries in a directory to be memcmp sorted, without trailing slash for subdirectories. I need to check again if v5 sort order is compatible.. --
Duy
Re: [PATCH 5.5/22] Add documentation for the index api
Quoted 7 lines · Duy Nguyen
Quoted 5 lines · Thomas Gummerer
Duy Nguyen <[email protected]> writes:
Hrm, I played around a bit with this idea, but I couldn't figure out how to make it work. For it to work we would still have to load some entries in a directory at least? Or is there a way to match the directories, which I just haven't figured out yet?
Quoted 5 lines
Yes you have to load some entries first. Even if a directory does not match, we only know until at least the first file in the directory. OK there might be problems because
tree_entry_interestingexpects all entries in a directory to be memcmp sorted, without trailing slash for subdirectories. I need to check again if v5 sort order is compatible..
Not gonna work (at least not simple) because we have to mix directories and files again. The way directory entries are ordered makes it hard (or less efficient) to get the list of immediate subdirs of a dir. I think I understand now why you need adjusted_pathspec.. --
Duy
Re: [PATCH 05/22] read-cache: add index reading api
In reply to Duy NguyenQuoted 25 lines · Duy Nguyen
Quoted 19 lines · Thomas Gummerer
Quoted 6 lines · Duy Nguyen
Putting
filter_optsinindex_statefeels like a bad design. Iterator information should be separated from the iterated object, so that two callers can walk through the same index without stepping on each other (I'm not talking about multithreading, a caller may walk a bit, then the other caller starts walking, then the former caller resumes walking again in a call chain).Yes, you're right. We need the
filter_optsto see what part of the index has been loaded [1] and which part has been skipped, but it shouldn't be used for filtering in thefor_each_index_entryfunction.I think there should be two versions of the
for_each_index_entry
function then, where thefor_each_index_entryfunction would behave the
same way as thefor_each_index_entry_filteredfunction with thefilter_optsparameter set to NULL:for_each_index_entry_filtered(struct index_state *, each_cache_entry_fn, void *cb_data, struct filter_opts *)for_each_index_entry(struct index_state *, each_cache_entry_fn, void *cb_data)Quoted 4 lines
Both of them then should call
index_change_filter_optsto make sure all the entries that are needed are loaded in the in-memory format.Does that make sense?
Quoted 8 lines
Hmm.. I was confused actually (documentation on the api would help greatly). If you already filter at load time, I don't think you need to match again. The caller asked for filter and it should know what's in there so
for_each_index_entryjust goes through all entries without extramatch_pathspec. Or is that whatnext_index_entryfor?match_pathspecfunction could be expensive when glob is involved. If the caller wants extra matching, it could do inside the callback function.
Yes, a documentation would be good. I'll try to write something better later today, when I have some more time. In the meantime I'll just outline what the functions do here shortly:
read_index_filtered(opts): This method behaves differently for index-v2
and index-v5.
For index-v2 it simply reads the whole index as read_cache() does, so we are sure we don't have to reload anything if the user wants a different filter.
For index-v5 it creates an adjusted pathspec to and reads all directories that are matched by them.
get_index_entry_by_name(name, namelen, &ce): Returns a cache_entry
matched by name via the &ce parameter. If a cache_entry is matched exactly 1 is returned.
Name may also be a path, in which case it returns 0 and the first cache_entry in that path. e.g. we have:
...
path/file1
....
in the index and name is "path", than it returns 0 and the path/file1 cache_entry. If name is "path/file1" on the other hand it returns 1 and the path/file1 cache_entry.
for_each_index_entry(fn, cb_data): Iterates over all cache_entries in
the index filtered by filter_opts in the index_state, and executes fn for each of them with the cb_data as callback data.
next_index_entry(ce): Returns the cache_entry that follows after ce
index_change_filter_opts(opts): For index-v2 it simply changes the
filter_opts, so for_each_index_entry uses the changed index_opts. For index-v5 it refreshes the index if the filter_opts have changed. This has some optimization potential, in the case that the opts get stricter (less of the index should be read) it doesn't have to reload anything.
I'm not sure what's in the cache, because the whole index is in the cache if the on-disk format is index-v2 and the index is filtered by the adjusted_pathspec if the on-disk format is index-v5. That's what I need the extra match_pathspec for. But yes, that could also be left to the caller.
Hope that makes it a little clearer.
Quoted 7 lines
It seems you could change the filter with
index_change_filter_opts. In v5 the index will be reloaded. What happens when some index entries area already modified? Do we start to have read-only index "views" and one read-write view? If partial views are always read-only, perhaps we just allow the user to create a newindex_state(or view) with new filter and destroy the old one. We don't have to care about changing or separating filter in that case because the view is the iterator.
The read-write part is mostly covered by the next patch (6/22). Before changing the index, the filter_opts always have to be set to NULL, using index_change_filter_opts and therefore use the whole index. This is currently hard to improve, because we always need the whole index when we write it. Changing this only makes sense once we have partial writing too.
So in principle the index_change_filter_opts function implements those views.
Even with partial writing we have to distinguish if a cache_entry has been added/removed, in which case a full rewrite is necessary or if a cache_entry has simply been modified (it's content changed), in which case we could replace it in place.
Quoted 8 lines
I wanted to have a tree-based iterator api, but that seems incompatible with pre-v5 (or at least adds some overhead on pre-v5 to rebuild the tree structure). It looks like using pathspec to build a list of entries, as you did, is a good way to take advantage of tree-based v5 while maintaining code compatibility with pre-v5. By the way with tree structure, you could use
tree_entry_interestinginread_index_filtered_v5. I think it's more efficient thanmatch_pathspec.
Yes, that's why I decided to keep the current in-memory format for now. Once an api is in place I think it will be easier to explore the tree-based format, without having to change the format all over the place.
Thanks, I will take a look at tree_entry_interesting later.
Quoted 9 lines
I'm still studying the code. Some of what I wrote here may be totally wrong due to my lack of understanding. I'll get back to you later if I find something else.
Quoted 3 lines
[1] That is only important for the new index-v5 file format, which can be loaded partially. The
filter_optscould always be set to NULL, as the whole index is always loaded anyway.--
Duy
PATCH 6/22 make sure partially read index is not changed
In reply to Thomas GummererA partially read index file currently cannot be written to disk. Make sure that never happens, by re-reading the index file if the index file wasn't read completely before changing the in-memory index.
--- builtin/update-index.c | 4 ++++ cache.h | 4 +++- read-cache-v2.c | 3 +++ read-cache.c | 8 ++++++++ 4 files changed, 18 insertions(+), 1 deletion(-)
builtin/update-index.c +4 -0
@@ -49,6 +49,8 @@ static int mark_ce_flags(const char *path, int flag, int mark)int namelen =strlen (path);int pos =cache_name_pos (path, namelen);if (0 <= pos) { +if (active_cache_partially_read) +cache_change_filter_opts (NULL );if (mark) active_cache[pos]->ce_flags |= flag;else
@@ -253,6 +255,8 @@ static void chmod_path(int flip, const char *path) pos =cache_name_pos (path,strlen (path));if (pos <0 ) goto fail; +if (active_cache_partially_read) +cache_change_filter_opts (NULL ); ce = active_cache[pos]; mode = ce->ce_mode;if (!S_ISREG (mode))
cache.h +3 -1
@@ -293,7 +293,8 @@ struct index_state {struct cache_tree *cache_tree;struct cache_time timestamp; unsigned name_hash_initialized : 1, - initialized : 1; + initialized : 1, + partially_read : 1;struct hash_table name_hash;struct hash_table dir_hash;struct index_ops *ops;
@@ -315,6 +316,7 @@ extern void free_name_hash(struct index_state *istate);#define active_alloc (the_index.cache_alloc)#define active_cache_changed (the_index.cache_changed)#define active_cache_tree (the_index.cache_tree) +#define active_cache_partially_read (the_index.partially_read)#define read_cache () read_index(&the_index)#define read_cache_from (path) read_index_from(&the_index, (path))
read-cache-v2.c +3 -0
@@ -273,6 +273,7 @@ static int read_index_v2(struct index_state *istate, void *mmap, src_offset +=8 ; src_offset += extsize; } + istate->partially_read =0 ;return 0 ; unmap:munmap (mmap, mmap_size);
@@ -495,6 +496,8 @@ static int write_index_v2(struct index_state *istate, int newfd)struct stat st;struct strbuf previous_name_buf =STRBUF_INIT , *previous_name; +if (istate->partially_read) +die ("BUG: index: cannot write a partially read index" );for (i = removed = extended =0 ; i < entries; i++) {if (cache[i]->ce_flags &CE_REMOVE ) removed++;
read-cache.c +8 -0
@@ -30,6 +30,8 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache {struct cache_entry *old = istate->cache[nr]; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );remove_name_hash (istate, old);set_index_entry (istate, nr, ce); istate->cache_changed =1 ;
@@ -467,6 +469,8 @@ int remove_index_entry_at(struct index_state *istate, int pos) {struct cache_entry *ce = istate->cache[pos]; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );record_resolve_undo (istate, ce);remove_name_hash (istate, ce); istate->cache_changed =1 ;
@@ -978,6 +982,8 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti {int pos; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );if (option &ADD_CACHE_JUST_APPEND ) pos = istate->cache_nr;else {
@@ -1184,6 +1190,8 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p/* If we are doing --really-refresh that * means the index is not valid anymore. */ +if (istate->partially_read) +index_change_filter_opts (istate,NULL ); ce->ce_flags &= ~CE_VALID ; istate->cache_changed =1 ; }
Signature · 1 line
1.8.3.453.g1dfc63d
Re: [PATCH 06/22] make sure partially read index is not changed
Quoted 3 lines · Thomas Gummerer
A partially read index file currently cannot be written to disk. Make sure that never happens, by re-reading the index file if the index file wasn't read completely before changing the in-memory index.
I am not quite sure what you are trying to do.
In operations that modify the index (replace_index_entry(), remove_index_entry_at(), etc.) you lift the filter_ops and keep partially_read flag still on. In the write-out codepath, you have an assert to make sure the caller has cleared the partially_read flag. A natural way to clear the flag is to re-read the index from the file, but then you can easily lose the modifications. Should there be another safety that says "calling read_index() with the partially_read flag on is a bug" or something?
Also shouldn't the flag be cleared upon discard_index()? If it is done there, you probably would not need to clear it in read_index().
Quoted 114 lines
Signed-off-by Thomas Gummerer <[email protected]>--- builtin/update-index.c | 4 ++++ cache.h | 4 +++- read-cache-v2.c | 3 +++ read-cache.c | 8 ++++++++ 4 files changed, 18 insertions(+), 1 deletion(-)builtin/update-index.c +4
-0@@ -49,6 +49,8 @@ static int mark_ce_flags(const char *path, int flag, int mark)int namelen =strlen (path);int pos =cache_name_pos (path, namelen);if (0 <= pos) { +if (active_cache_partially_read) +cache_change_filter_opts (NULL );if (mark) active_cache[pos]->ce_flags |= flag;else @@ -253,6 +255,8 @@ static void chmod_path(int flip, const char *path) pos =cache_name_pos (path,strlen (path));if (pos <0 ) goto fail; +if (active_cache_partially_read) +cache_change_filter_opts (NULL ); ce = active_cache[pos]; mode = ce->ce_mode;if (!S_ISREG (mode))cache.h +3
-1@@ -293,7 +293,8 @@ struct index_state {struct cache_tree *cache_tree;struct cache_time timestamp; unsigned name_hash_initialized : 1, - initialized : 1; + initialized : 1, + partially_read : 1;struct hash_table name_hash;struct hash_table dir_hash;struct index_ops *ops;@@ -315,6 +316,7 @@ extern void free_name_hash(struct index_state *istate);#define active_alloc (the_index.cache_alloc)#define active_cache_changed (the_index.cache_changed)#define active_cache_tree (the_index.cache_tree) +#define active_cache_partially_read (the_index.partially_read)#define read_cache () read_index(&the_index)#define read_cache_from (path) read_index_from(&the_index, (path))read-cache-v2.c +3
-0@@ -273,6 +273,7 @@ static int read_index_v2(struct index_state *istate, void *mmap, src_offset +=8 ; src_offset += extsize; } + istate->partially_read =0 ;return 0 ; unmap:munmap (mmap, mmap_size);@@ -495,6 +496,8 @@ static int write_index_v2(struct index_state *istate, int newfd)struct stat st;struct strbuf previous_name_buf =STRBUF_INIT , *previous_name; +if (istate->partially_read) +die ("BUG: index: cannot write a partially read index" );for (i = removed = extended =0 ; i < entries; i++) {if (cache[i]->ce_flags &CE_REMOVE ) removed++;read-cache.c +8
-0@@ -30,6 +30,8 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache {struct cache_entry *old = istate->cache[nr]; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );remove_name_hash (istate, old);set_index_entry (istate, nr, ce); istate->cache_changed =1 ;@@ -467,6 +469,8 @@ int remove_index_entry_at(struct index_state *istate, int pos) {struct cache_entry *ce = istate->cache[pos]; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );record_resolve_undo (istate, ce);remove_name_hash (istate, ce); istate->cache_changed =1 ;@@ -978,6 +982,8 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti {int pos; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );if (option &ADD_CACHE_JUST_APPEND ) pos = istate->cache_nr;else {@@ -1184,6 +1190,8 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p/* If we are doing --really-refresh that * means the index is not valid anymore. */ +if (istate->partially_read) +index_change_filter_opts (istate,NULL ); ce->ce_flags &= ~CE_VALID ; istate->cache_changed =1 ; }
Re: [PATCH 06/22] make sure partially read index is not changed
Quoted 15 lines · Junio C Hamano
Quoted 3 lines · Thomas Gummerer
A partially read index file currently cannot be written to disk. Make sure that never happens, by re-reading the index file if the index file wasn't read completely before changing the in-memory index.
I am not quite sure what you are trying to do.
In operations that modify the index (
replace_index_entry(),remove_index_entry_at(), etc.) you lift thefilter_opsand keeppartially_readflag still on. In the write-out codepath, you have an assert to make sure the caller has cleared thepartially_readflag. A natural way to clear the flag is to re-read the index from the file, but then you can easily lose the modifications.
Quoted 2 lines
Also shouldn't the flag be cleared upon
discard_index()? If it is done there, you probably would not need to clear it inread_index().
Hrm, maybe the code isn't quite clear enough here, or maybe the patch should come directly before (16/22) read-cache: read index-v5 to be more clear.
The flag is always set to 0 in read_index_v2, as the whole index is always read and therefore it never needs to be reset. With read_index_v5 on the other hand the flag is set when the filter_opts are different than NULL.
But thinking about it, the flag is actually not necessary at all. The filter_opts should simply be checked for NULL for the assert and they should also be set to NULL on discard_index. Will fix this in the next version. Thanks.
Quoted 3 lines
Should
there be another safety that says "callingread_index()with thepartially_readflag on is a bug" or something?
I'm not sure. I think it doesn't hurt, as we discard the index when we change the index_ops. At the moment I can't think of a case where where calling read_index() could be used when it's partially read without discarding the cache first. I'll add it in the next version.
Quoted 114 lines
Quoted 114 lines
Signed-off-by Thomas Gummerer <[email protected]>--- builtin/update-index.c | 4 ++++ cache.h | 4 +++- read-cache-v2.c | 3 +++ read-cache.c | 8 ++++++++ 4 files changed, 18 insertions(+), 1 deletion(-)builtin/update-index.c +4
-0@@ -49,6 +49,8 @@ static int mark_ce_flags(const char *path, int flag, int mark)int namelen =strlen (path);int pos =cache_name_pos (path, namelen);if (0 <= pos) { +if (active_cache_partially_read) +cache_change_filter_opts (NULL );if (mark) active_cache[pos]->ce_flags |= flag;else @@ -253,6 +255,8 @@ static void chmod_path(int flip, const char *path) pos =cache_name_pos (path,strlen (path));if (pos <0 ) goto fail; +if (active_cache_partially_read) +cache_change_filter_opts (NULL ); ce = active_cache[pos]; mode = ce->ce_mode;if (!S_ISREG (mode))cache.h +3
-1@@ -293,7 +293,8 @@ struct index_state {struct cache_tree *cache_tree;struct cache_time timestamp; unsigned name_hash_initialized : 1, - initialized : 1; + initialized : 1, + partially_read : 1;struct hash_table name_hash;struct hash_table dir_hash;struct index_ops *ops;@@ -315,6 +316,7 @@ extern void free_name_hash(struct index_state *istate);#define active_alloc (the_index.cache_alloc)#define active_cache_changed (the_index.cache_changed)#define active_cache_tree (the_index.cache_tree) +#define active_cache_partially_read (the_index.partially_read)#define read_cache () read_index(&the_index)#define read_cache_from (path) read_index_from(&the_index, (path))read-cache-v2.c +3
-0@@ -273,6 +273,7 @@ static int read_index_v2(struct index_state *istate, void *mmap, src_offset +=8 ; src_offset += extsize; } + istate->partially_read =0 ;return 0 ; unmap:munmap (mmap, mmap_size);@@ -495,6 +496,8 @@ static int write_index_v2(struct index_state *istate, int newfd)struct stat st;struct strbuf previous_name_buf =STRBUF_INIT , *previous_name; +if (istate->partially_read) +die ("BUG: index: cannot write a partially read index" );for (i = removed = extended =0 ; i < entries; i++) {if (cache[i]->ce_flags &CE_REMOVE ) removed++;read-cache.c +8
-0@@ -30,6 +30,8 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache {struct cache_entry *old = istate->cache[nr]; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );remove_name_hash (istate, old);set_index_entry (istate, nr, ce); istate->cache_changed =1 ;@@ -467,6 +469,8 @@ int remove_index_entry_at(struct index_state *istate, int pos) {struct cache_entry *ce = istate->cache[pos]; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );record_resolve_undo (istate, ce);remove_name_hash (istate, ce); istate->cache_changed =1 ;@@ -978,6 +982,8 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti {int pos; +if (istate->partially_read) +index_change_filter_opts (istate,NULL );if (option &ADD_CACHE_JUST_APPEND ) pos = istate->cache_nr;else {@@ -1184,6 +1190,8 @@ int refresh_index(struct index_state *istate, unsigned int flags, const char **p/* If we are doing --really-refresh that * means the index is not valid anymore. */ +if (istate->partially_read) +index_change_filter_opts (istate,NULL ); ce->ce_flags &= ~CE_VALID ; istate->cache_changed =1 ; }
PATCH 7/22 dir.c: use index api
In reply to Thomas Gummerer--- dir.c | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-)
dir.c +15 -18
@@ -468,19 +468,19 @@ void add_exclude(const char *string, const char *base,static void *read_skip_worktree_file_from_index (const char *path,size_t *size) { -int pos, len; +int len;unsigned long sz;enum object_type type;void *data;struct index_state *istate = &the_index; +struct cache_entry *ce; len =strlen (path); - pos =index_name_pos (istate, path, len); -if (pos <0 ) +if (!get_index_entry_by_name (istate, path, len, &ce))return NULL ; -if (!ce_skip_worktree (istate->cache[pos])) +if (!ce_skip_worktree (ce))return NULL ; - data =read_sha1_file (istate->cache[pos]->sha1, &type, &sz); + data =read_sha1_file (ce->sha1, &type, &sz);if (!data || type !=OBJ_BLOB ) {free (data);return NULL ;
@@ -968,16 +968,13 @@ static enum exist_status directory_exists_in_index_icase(const char *dirname, in*/ static enum exist_status directory_exists_in_index (const char *dirname,int len) { -int pos; +struct cache_entry *ce;if (ignore_case)return directory_exists_in_index_icase (dirname, len); - pos =cache_name_pos (dirname, len); -if (pos <0 ) - pos = -pos-1 ; -while (pos < active_nr) { -struct cache_entry *ce = active_cache[pos++]; +get_cache_entry_by_name (dirname, len, &ce); +while (ce) {unsigned char endchar;if (strncmp (ce->name, dirname, len))
@@ -989,6 +986,7 @@ static enum exist_status directory_exists_in_index(const char *dirname, int len)return index_directory;if (!endchar &&S_ISGITLINK (ce->ce_mode))return index_gitdir; + ce =next_cache_entry (ce); }return index_nonexistent; }
@@ -1114,7 +1112,6 @@ static int exclude_matches_pathspec(const char *path, int len,static int get_index_dtype (const char *path,int len) { -int pos;struct cache_entry *ce; ce =cache_name_exists (path, len,0 );
@@ -1131,18 +1128,18 @@ static int get_index_dtype(const char *path, int len) }/* Try to look it up as a directory */ - pos =cache_name_pos (path, len); -if (pos >=0 ) +if (get_cache_entry_by_name (path, len, &ce));return DT_UNKNOWN ; - pos = -pos-1 ; -while (pos < active_nr) { - ce = active_cache[pos++]; + +while (ce) {if (strncmp (ce->name, path, len))break ;if (ce->name[len] >'/' )break ; -if (ce->name[len] <'/' ) +if (ce->name[len] <'/' ) { + ce =next_cache_entry (ce);continue ; + }if (!ce_uptodate (ce))break ;/* continue? */ return DT_DIR ;
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 8/22 tree.c: use index api
In reply to Thomas Gummerer--- tree.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-)
tree.c +20 -18
@@ -128,20 +128,28 @@ int read_tree_recursive(struct tree *tree,return ret; } -static int cmp_cache_name_compare (const void *a_,const void *b_) + +struct read_tree_data { +read_tree_fn_t fn; +int stage; +}; + +int get_read_tree_fn (struct cache_entry *ce,void *cb_data) { -const struct cache_entry *ce1, *ce2; +struct read_tree_data *data = cb_data; - ce1 = *((const struct cache_entry **)a_); - ce2 = *((const struct cache_entry **)b_); -return cache_name_stage_compare (ce1->name, ce1->ce_namelen,ce_stage (ce1), - ce2->name, ce2->ce_namelen,ce_stage (ce2)); +if (ce_stage (ce) == data->stage) { + data->fn = read_one_entry; +return 0 ; + } +return 1 ; }int read_tree (struct tree *tree,int stage,struct pathspec *match) {read_tree_fn_t fn =NULL ; -int i, err; +int err; +struct read_tree_data rtd;/* * Currently the only existing callers of this function all
@@ -158,11 +166,10 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)* do it the original slow way, otherwise, append and then * sort at the end. */ -for (i =0 ; !fn && i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -if (ce_stage (ce) == stage) - fn = read_one_entry; - } + rtd.fn = fn; + rtd.stage = stage; +for_each_cache_entry (get_read_tree_fn, &rtd); + fn = rtd.fn;if (!fn) fn = read_one_entry_quick;
@@ -170,12 +177,7 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)if (fn == read_one_entry || err)return err; -/* -* Sort the cache entry -- we need to nuke the cache tree, though. -*/ -cache_tree_free (&active_cache_tree); -qsort (active_cache, active_nr,sizeof (active_cache[0 ]), - cmp_cache_name_compare); +sort_cache ();return 0 ; }
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 9/22 name-hash.c: use index api
In reply to Thomas Gummerer--- name-hash.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
name-hash.c +7 -4
@@ -144,16 +144,19 @@ static void hash_index_entry(struct index_state *istate, struct cache_entry *ce)add_dir_entry (istate, ce); } -static void lazy_init_name_hash (struct index_state *istate) +static int hash_entry (struct cache_entry *ce,void *istate) { -int nr; +hash_index_entry ((struct index_state *)istate, ce); +return 0 ; +} +static void lazy_init_name_hash (struct index_state *istate) +{if (istate->name_hash_initialized)return ;if (istate->cache_nr)preallocate_hash (&istate->name_hash, istate->cache_nr); -for (nr =0 ; nr < istate->cache_nr; nr++) -hash_index_entry (istate, istate->cache[nr]); +for_each_index_entry (istate, hash_entry, istate); istate->name_hash_initialized =1 ; }
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 10/22 grep.c: Use index api
In reply to Thomas Gummerer--- builtin/grep.c | 71 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 37 insertions(+), 34 deletions(-)
builtin/grep.c +37 -34
@@ -368,41 +368,33 @@ static void run_pager(struct grep_opt *opt, const char *prefix)free (argv); } -static int grep_cache (struct grep_opt *opt,const struct pathspec *pathspec,int cached) +struct grep_opts { +struct grep_opt *opt; +const struct pathspec *pathspec; +int cached; +int hit; +}; + +static int grep_cache (struct cache_entry *ce,void *cb_data) { -int hit =0 ; -int nr; -read_cache (); +struct grep_opts *opts = cb_data; -for (nr =0 ; nr < active_nr; nr++) { -struct cache_entry *ce = active_cache[nr]; -if (!S_ISREG (ce->ce_mode)) -continue ; -if (!match_pathspec_depth (pathspec, ce->name,ce_namelen (ce),0 ,NULL )) -continue ; -/* -* If CE_VALID is on, we assume worktree file and its cache entry -* are identical, even if worktree file has been modified, so use -* cache version instead -*/ -if (cached || (ce->ce_flags &CE_VALID ) ||ce_skip_worktree (ce)) { -if (ce_stage (ce)) -continue ; - hit |=grep_sha1 (opt, ce->sha1, ce->name,0 , ce->name); - } -else - hit |=grep_file (opt, ce->name); -if (ce_stage (ce)) { -do { - nr++; - }while (nr < active_nr && - !strcmp (ce->name, active_cache[nr]->name)); - nr--;/* compensate for loop control */ - } -if (hit && opt->status_only) -break ; - } -return hit; +if (!S_ISREG (ce->ce_mode)) +return 0 ; +if (!match_pathspec_depth (opts->pathspec, ce->name,ce_namelen (ce),0 ,NULL )) +return 0 ; +/* +* If CE_VALID is on, we assume worktree file and its cache entry +* are identical, even if worktree file has been modified, so use +* cache version instead +*/ +if (opts->cached || (ce->ce_flags &CE_VALID ) ||ce_skip_worktree (ce)) + opts->hit |=grep_sha1 (opts->opt, ce->sha1, ce->name,0 , ce->name); +else + opts->hit |=grep_file (opts->opt, ce->name); +if (opts->hit && opts->opt->status_only) +return 1 ; +return 0 ; } static int grep_tree(struct grep_opt *opt, const struct pathspec *pathspec,
@@ -895,10 +887,21 @@ int cmd_grep(int argc, const char **argv, const char *prefix) }else if (0 <= opt_exclude) {die (_ ("--[no-]exclude-standard cannot be used for tracked contents." )); }else if (!list.nr) { +struct grep_opts opts; +struct filter_opts *filter_opts =xmalloc (sizeof (*filter_opts)); +if (!cached)setup_work_tree (); - hit =grep_cache (&opt, &pathspec, cached); +memset (filter_opts,0 ,sizeof (*filter_opts)); + filter_opts->pathspec = pathspec.raw; + opts.opt = &opt; + opts.pathspec = &pathspec; + opts.cached = cached; + opts.hit =0 ; +read_cache_filtered (filter_opts); +for_each_cache_entry (grep_cache, &opts); + hit = opts.hit; }else {if (cached)die (_ ("both --cached and trees are given." ));
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 11/22 ls-files.c: use the index api
In reply to Thomas Gummerer--- builtin/ls-files.c | 213 +++++++++++++++++++++++++---------------------------- 1 file changed, 100 insertions(+), 113 deletions(-)
builtin/ls-files.c +100 -113
@@ -88,36 +88,35 @@ static void show_killed_files(struct dir_struct *dir)for (i =0 ; i < dir->nr; i++) {struct dir_entry *ent = dir->entries[i];char *cp, *sp; -int pos, len, killed =0 ; +int len, killed =0 ;for (cp = ent->name; cp - ent->name < ent->len; cp = sp +1 ) { +struct cache_entry *ce; + sp =strchr (cp,'/' );if (!sp) {/* If ent->name is prefix of an entry in the * cache, it will be killed. */ - pos =cache_name_pos (ent->name, ent->len); -if (0 <= pos) +if (get_cache_entry_by_name (ent->name, ent->len, &ce))die ("bug in show-killed-files" ); - pos = -pos -1 ; -while (pos < active_nr && -ce_stage (active_cache[pos])) - pos++;/* skip unmerged */ -if (active_nr <= pos) +while (ce &&ce_stage (ce)) + ce =next_cache_entry (ce); +if (!ce)break ;/* pos points at a name immediately after * ent->name in the cache. Does it expect * ent->name to be a directory? */ - len =ce_namelen (active_cache[pos]); + len =ce_namelen (ce);if ((ent->len < len) && - !strncmp (active_cache[pos]->name, + !strncmp (ce->name, ent->name, ent->len) && - active_cache[pos]->name[ent->len] =='/' ) + ce->name[ent->len] =='/' ) killed =1 ;break ; } -if (0 <=cache_name_pos (ent->name, sp - ent->name)) { +if (get_cache_entry_by_name (ent->name, sp - ent->name, &ce)) {/* If any of the leading directories in * ent->name is registered in the cache, * ent->name will be killed.
@@ -213,10 +212,43 @@ static int ce_excluded(struct dir_struct *dir, struct cache_entry *ce)return is_excluded (dir, ce->name, &dtype); } -static void show_files (struct dir_struct *dir) +static int show_cached_stage (struct cache_entry *ce,void *cb_data) { -int i; +struct dir_struct *dir = cb_data; + +if ((dir->flags &DIR_SHOW_IGNORED ) && !ce_excluded (dir, ce)) +return 0 ; +if (show_unmerged && !ce_stage (ce)) +return 0 ; +if (ce->ce_flags &CE_UPDATE ) +return 0 ; +show_ce_entry (ce_stage (ce) ? tag_unmerged : + (ce_skip_worktree (ce) ? tag_skip_worktree : tag_cached), ce); +return 0 ; +} +static int show_deleted_modified (struct cache_entry *ce,void *cb_data) +{ +struct stat st; +int err; +struct dir_struct *dir = cb_data; + +if ((dir->flags &DIR_SHOW_IGNORED ) && !ce_excluded (dir, ce)) +return 0 ; +if (ce->ce_flags &CE_UPDATE ) +return 0 ; +if (ce_skip_worktree (ce)) +return 0 ; + err =lstat (ce->name, &st); +if (show_deleted && err) +show_ce_entry (tag_removed, ce); +if (show_modified &&ce_modified (ce, &st,0 )) +show_ce_entry (tag_modified, ce); +return 0 ; +} + +static void show_files (struct dir_struct *dir) +{/* For cached/deleted files we don't need to even do the readdir */ if (show_others || show_killed) {fill_directory (dir, pathspec);
@@ -225,66 +257,18 @@ static void show_files(struct dir_struct *dir)if (show_killed)show_killed_files (dir); } -if (show_cached || show_stage) { -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -if ((dir->flags &DIR_SHOW_IGNORED ) && - !ce_excluded (dir, ce)) -continue ; -if (show_unmerged && !ce_stage (ce)) -continue ; -if (ce->ce_flags &CE_UPDATE ) -continue ; -show_ce_entry (ce_stage (ce) ? tag_unmerged : - (ce_skip_worktree (ce) ? tag_skip_worktree : tag_cached), ce); - } - } -if (show_deleted || show_modified) { -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -struct stat st; -int err; -if ((dir->flags &DIR_SHOW_IGNORED ) && - !ce_excluded (dir, ce)) -continue ; -if (ce->ce_flags &CE_UPDATE ) -continue ; -if (ce_skip_worktree (ce)) -continue ; - err =lstat (ce->name, &st); -if (show_deleted && err) -show_ce_entry (tag_removed, ce); -if (show_modified &&ce_modified (ce, &st,0 )) -show_ce_entry (tag_modified, ce); - } - } +if (show_cached | show_stage) +for_each_cache_entry (show_cached_stage, dir); +if (show_deleted | show_modified) +for_each_cache_entry (show_deleted_modified, dir); } -/* -* Prune the index to only contain stuff starting with "prefix" -*/ -static void prune_cache (const char *prefix) +static int hoist_unmerged (struct cache_entry *ce,void *cb_data) { -int pos =cache_name_pos (prefix, max_prefix_len); -unsigned int first, last; - -if (pos <0 ) - pos = -pos-1 ; -memmove (active_cache, active_cache + pos, - (active_nr - pos) *sizeof (struct cache_entry *)); - active_nr -= pos; - first =0 ; - last = active_nr; -while (last > first) { -int next = (last + first) >>1 ; -struct cache_entry *ce = active_cache[next]; -if (!strncmp (ce->name, prefix, max_prefix_len)) { - first = next+1 ; -continue ; - } - last = next; - } - active_nr = last; +if (!ce_stage (ce)) +return 0 ; + ce->ce_flags |=CE_STAGEMASK ; +return 0 ; }static void strip_trailing_slash_from_submodules (void )
@@ -292,16 +276,38 @@ static void strip_trailing_slash_from_submodules(void)const char **p;for (p = pathspec; *p !=NULL ; p++) { -int len =strlen (*p), pos; +int len =strlen (*p); +struct cache_entry *ce;if (len <1 || (*p)[len -1 ] !='/' )continue ; - pos =cache_name_pos (*p, len -1 ); -if (pos >=0 &&S_ISGITLINK (active_cache[pos]->ce_mode)) +if (get_cache_entry_by_name (*p, len -1 , &ce) &&S_ISGITLINK (ce->ce_mode)) *p =xstrndup (*p, len -1 ); } } +int mark_entry_to_show (struct cache_entry *ce,void *cb_data) +{ +struct cache_entry *last_stage0 = cb_data; +switch (ce_stage (ce)) { +case 0 : + last_stage0 = ce; +/* fallthru */ +default : +return 0 ; +case 1 : +/* +* If there is stage #0 entry for this, we do not +* need to show it. We use CE_UPDATE bit to mark +* such an entry. +*/ +if (last_stage0 && + !strcmp (last_stage0->name, ce->name)) + ce->ce_flags |=CE_UPDATE ; + } +return 0 ; +} +/* * Read the tree specified with --with-tree option * (typically, HEAD) into stage #1 and then
@@ -316,7 +322,6 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)unsigned char sha1[20 ];struct pathspec pathspec;struct cache_entry *last_stage0 =NULL ; -int i;if (get_sha1 (tree_name, sha1))die ("tree-ish %s not found." , tree_name);
@@ -325,12 +330,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)die ("bad tree-ish %s" , tree_name);/* Hoist the unmerged entries up to stage #3 to make room */ -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -if (!ce_stage (ce)) -continue ; - ce->ce_flags |=CE_STAGEMASK ; - } +for_each_cache_entry (hoist_unmerged,NULL );if (prefix) {static const char *(matchbuf[2 ]);
@@ -343,25 +343,7 @@ void overlay_tree_on_cache(const char *tree_name, const char *prefix)if (read_tree (tree,1 , &pathspec))die ("unable to read tree entries %s" , tree_name); -for (i =0 ; i < active_nr; i++) { -struct cache_entry *ce = active_cache[i]; -switch (ce_stage (ce)) { -case 0 : - last_stage0 = ce; -/* fallthru */ -default : -continue ; -case 1 : -/* -* If there is stage #0 entry for this, we do not -* need to show it. We use CE_UPDATE bit to mark -* such an entry. -*/ -if (last_stage0 && - !strcmp (last_stage0->name, ce->name)) - ce->ce_flags |=CE_UPDATE ; - } - } +for_each_cache_entry (mark_entry_to_show, last_stage0); }int report_path_error (const char *ps_matched,const char **pathspec,const char *prefix)
@@ -457,6 +439,7 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix)struct dir_struct dir;struct exclude_list *el;struct string_list exclude_list =STRING_LIST_INIT_NODUP ; +struct filter_opts *opts =xmalloc (sizeof (*opts));struct option builtin_ls_files_options[] = { {OPTION_CALLBACK ,'z' ,NULL ,NULL ,NULL ,N_ ("paths are separated with NUL character" ),
@@ -522,9 +505,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) prefix_len =strlen (prefix);git_config (git_default_config,NULL ); -if (read_cache () <0 ) -die ("index file corrupt" ); - argc =parse_options (argc, argv, prefix, builtin_ls_files_options, ls_files_usage,0 ); el =add_exclude_list (&dir,EXC_CMDL ,"--exclude option" );
@@ -557,14 +537,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) pathspec =get_pathspec (prefix, argv); -/* be nice with submodule paths ending in a slash */ -if (pathspec) -strip_trailing_slash_from_submodules (); - -/* Find common prefix for all pathspec's */ - max_prefix =common_prefix (pathspec); - max_prefix_len = max_prefix ?strlen (max_prefix) :0 ; -/* Treat unmatching pathspec elements as errors */ if (pathspec && error_unmatch) {int num;
@@ -573,6 +545,23 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) ps_matched =xcalloc (1 , num); } +memset (opts,0 ,sizeof (*opts)); + opts->pathspec = pathspec; + opts->read_staged =1 ; +if (show_resolve_undo) + opts->read_resolve_undo =1 ; +read_cache_filtered (opts); + +if (pathspec) { +strip_trailing_slash_from_submodules (); + opts->pathspec = pathspec; +cache_change_filter_opts (opts); + } + +/* Find common prefix for all pathspec's */ + max_prefix = opts->max_prefix; + max_prefix_len = opts->max_prefix_len; +if ((dir.flags &DIR_SHOW_IGNORED ) && !exc_given)die ("ls-files --ignored needs some exclude pattern" );
@@ -581,8 +570,6 @@ int cmd_ls_files(int argc, const char **argv, const char *cmd_prefix) show_killed || show_modified || show_resolve_undo)) show_cached =1 ; -if (max_prefix) -prune_cache (max_prefix);if (with_tree) {/* * Basic sanity check; show-stages and show-unmerged
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 12/22 read-cache: make read_blob_data_from_index use index api
In reply to Thomas Gummerer--- read-cache.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
read-cache.c +10 -12
@@ -1588,29 +1588,27 @@ int index_name_is_other(const struct index_state *istate, const char *name,void *read_blob_data_from_index (struct index_state *istate,const char *path,unsigned long *size) { -int pos, len; +int ret, len;unsigned long sz;enum object_type type;void *data; +struct cache_entry *ce; len =strlen (path); - pos =index_name_pos (istate, path, len); -if (pos <0 ) { + ret =get_index_entry_by_name (istate, path, len, &ce); +if (!ret) {/* * We might be in the middle of a merge, in which * case we would read stage #2 (ours). */ -int i; -for (i = -pos -1 ; - (pos <0 && i < istate->cache_nr && - !strcmp (istate->cache[i]->name, path)); - i++) -if (ce_stage (istate->cache[i]) ==2 ) - pos = i; +for (; !ret && ce && !strcmp (ce->name, path); ce =next_index_entry (ce)) +if (ce_stage (ce) ==2 ) + ret =1 ; + } -if (pos <0 ) +if (!ret)return NULL ; - data =read_sha1_file (istate->cache[pos]->sha1, &type, &sz); + data =read_sha1_file (ce->sha1, &type, &sz);if (!data || type !=OBJ_BLOB ) {free (data);return NULL ;
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 13/22 documentation: add documentation of the index-v5 file format
In reply to Thomas GummererAdd a documentation of the index file format version 5 to Documentation/technical.
--- Documentation/technical/index-file-format-v5.txt | 296 +++++++++++++++++++++++ 1 file changed, 296 insertions(+) create mode 100644 Documentation/technical/index-file-format-v5.txt
Documentation/technical/index-file-format-v5.txt new file +296 -0
@@ -0,0 +1,296 @@ +GIT index format +================ + +== The git index + + The git index file (.git/index) documents the status of the files + in the git staging area. + + The staging area is used for preparing commits, merging, etc. + +== The git index file format + + All binary numbers are in network byte order. Version 5 is described + here. The index file consists of various sections. They appear in + the following order in the file. + + - header: the description of the index format, including it's signature, + version and various other fields that are used internally. + + - diroffsets (ndir entries of "direcotry offset"): A 4-byte offset + relative to the beginning of the "direntries block" (see below) + for each of the ndir directories in the index, sorted by pathname + (of the directory it's pointing to). [1] + + - direntries (ndir entries of "directory offset"): A directory entry + for each of the ndir directories in the index, sorted by pathname + (see below). [2] + + - fileoffsets (nfile entries of "file offset"): A 4-byte offset + relative to the beginning of the fileentries block (see below) + for each of the nfile files in the index. [1] + + - fileentries (nfile entries of "file entry"): A file entry for + each of the nfile files in the index (see below). + + - crdata: A number of entries for conflicted data/resolved conflicts + (see below). + + - Extensions (Currently none, see below in the future) + + Extensions are identified by signature. Optional extensions can + be ignored if GIT does not understand them. + + GIT supports an arbitrary number of extension, but currently none + is implemented. [3] + + extsig (32-bits): extension signature. If the first byte is 'A'..'Z' + the extension is optional and can be ignored. + + extsize (32-bits): size of the extension, excluding the header + (extsig, extsize, extchecksum). + + extchecksum (32-bits): crc32 checksum of the extension signature + and size. + + - Extension data. + +== Header + sig (32-bits): Signature: + The signature is { 'D', 'I', 'R', 'C' } (stands for "dircache") + + vnr (32-bits): Version number: + The current supported versions are 2, 3, 4 and 5. + + ndir (32-bits): number of directories in the index. + + nfile (32-bits): number of file entries in the index. + + fblockoffset (32-bits): offset to the file block, relative to the + beginning of the file. + + - Offset to the extensions. + + nextensions (32-bits): number of extensions. + + extoffset (32-bits): offset to the extension. (Possibly none, as + many as indicated in the 4-byte number of extensions) + + headercrc (32-bits): crc checksum including the header and the + offsets to the extensions. + + +== Directory offsets (diroffsets) + + diroffset (32-bits): offset to the directory relative to the beginning + of the index file. There are ndir + 1 offsets in the diroffset table, + the last is pointing to the end of the last direntry. With this last + entry, we are able to replace the strlen of when reading the directory + name, by calculating it from diroffset[n+1]-diroffset[n]-61. 61 is the + size of the directory data, which follows each each directory + the + crc sum + the NUL byte. + + This part is needed for making the directory entries bisectable and + thus allowing a binary search. + +== Directory entry (direntries) + + Directory entries are sorted in lexicographic order by the name + of their path starting with the root. + + pathname (variable length, nul terminated): relative to top level + directory (without the leading slash). '/' is used as path + separator. A string of length 0 ('') indicates the root directory. + The special path components ".", and ".." (without quotes) are + disallowed. The path also includes a trailing slash. [9] + + foffset (32-bits): offset to the lexicographically first file in + the file offsets (fileoffsets), relative to the beginning of + the fileoffset block. + + cr (32-bits): offset to conflicted/resolved data at the end of the + index. 0 if there is no such data. [4] + + ncr (32-bits): number of conflicted/resolved data entries at the + end of the index if the offset is non 0. If cr is 0, ncr is + also 0. + + nsubtrees (32-bits): number of subtrees this tree has in the index. + + nfiles (32-bits): number of files in the directory, that are in + the index. + + nentries (32-bits): number of entries in the index that is covered + by the tree this entry represents. (-1 if the entry is invalid). + This number includes all the files in this tree, recursively. + + objname (160-bits): object name for the object that would result + from writing this span of index as a tree. This is only valid + if nentries is valid, meaning the cache-tree is valid. + + flags (16-bits): 'flags' field split into (high to low bits) (For + D/F conflicts) + + stage (2-bits): stage of the directory during merge + + 14-bit unused + + dircrc (32-bits): crc32 checksum for each directory entry. + + The last 24 bytes (4-byte number of entries + 160-bit object name) are + for the cache tree. An entry can be in an invalidated state which is + represented by having -1 in the entry_count field. + + The entries are written out in the top-down, depth-first order. The + first entry represents the root level of the repository, followed by + the first subtree - let's call it A - of the root level, followed by + the first subtree of A, ... There is no prefix compression for + directories. + +== File offsets (fileoffsets) + + fileoffset (32-bits): offset to the file relative to the beginning of + the fileentries block. + + This part is needed for making the file entries bisectable and + thus allowing a binary search. There are nfile + 1 offsets in the + fileoffset table, the last is pointing to the end of the last + fileentry. With this last entry, we can replace the strlen when + reading each filename, by calculating its length with the offsets. + +== File entry (fileentries) + + File entries are sorted in ascending order on the name field, after the + respective offset given by the directory entries. All file names are + prefix compressed, meaning the file name is relative to the directory. + + filename (variable length, nul terminated). The exact encoding is + undefined, but the filename cannot contain a NUL byte (iow, the same + encoding as a UNIX pathname). + + flags (16-bits): 'flags' field split into (high to low bits) + + assumevalid (1-bit): assume-valid flag + + intenttoadd (1-bit): intent-to-add flag, used by "git add -N". + Extended flag in index v3. + + stage (2-bit): stage of the file during merge + + skipworktree (1-bit): skip-worktree flag, used by sparse checkout. + Extended flag in index v3. + + smudged (1-bit): indicates if the file is racily smudged. + + 10-bit unused, must be zero [6] + + mode (16-bits): file mode, split into (high to low bits) + + objtype (4-bits): object type + valid values in binary are 1000 (regular file), 1010 (symbolic + link) and 1110 (gitlink) + + 3-bit unused + + permission (9-bits): unix permission. Only 0755 and 0644 are valid + for regular files. Symbolic links and gitlinks have value 0 in + this field. + + mtimes (32-bits): mtime seconds, the last time a file's data changed + this is stat(2) data + + mtimens (32-bits): mtime nanosecond fractions + this is stat(2) data + + file size (32-bits): The on-disk size, trucated to 32-bit. + this is stat(2) data + + statcrc (32-bits): crc32 checksum over ctime seconds, ctime + nanoseconds, ino, dev, uid, gid (All stat(2) data + except mtime and file size). If the statcrc is 0 it will + be ignored. [7] + + objhash (160-bits): SHA-1 for the represented object + + entrycrc (32-bits): crc32 checksum for the file entry. The crc code + includes the offset to the offset to the file, relative to the + beginning of the file. + +== Conflict data + + A conflict is represented in the index as a set of higher stage entries. + These entries are stored at the end of the index. When a conflict is + resolved (e.g. with "git add path"). A bit is flipped, to indicate that + the conflict is resolved, but the entries will be kept, so that + conflicts can be recreated (e.g. with "git checkout -m", in case users + want to redo a conflict resolution from scratch. + + The first part of a conflict (usually stage 1) will be stored both in + the entries part of the index and in the conflict part. All other parts + will only be stored in the conflict part. + + filename (variable length, nul terminated): filename of the entry, + relative to its containing directory). + + nfileconflicts (32-bits): number of conflicts for the file [8] + + flags (nfileconflicts entries of "flags") (16-bits): 'flags' field + split into: + + conflicted (1-bit): conflicted state (conflicted/resolved) (1 if + conflicted) + + stage (2-bits): stage during merge. + + 13-bit unused + + entry_mode (nfileconflicts entries of "entry mode") (16-bits): + octal numbers, entry mode of eache entry in the different stages. + (How many is defined by the 4-byte number before) + + objectnames (nfileconflicts entries of "object name") (160-bits): + object names of the different stages. + + conflictcrc (32-bits): crc32 checksum over conflict data. + +== Design explanations + +[1] The directory and file offsets are included in the index format + to enable bisectability of the index, for binary searches.Updating + a single entry and partial reading will benefit from this. + +[2] The directories are saved in their own block, to be able to + quickly search for a directory in the index. They include a + offset to the (lexically) first file in the directory. + +[3] The data of the cache-tree extension and the resolve undo + extension is now part of the index itself, but if other extensions + come up in the future, there is no need to change the index, they + can simply be added at the end. + +[4] To avoid rewrites of the whole index when there are conflicts or + conflicts are being resolved, conflicted data will be stored at + the end of the index. To mark the conflict resolved, just a bit + has to be flipped. The data will still be there, if a user wants + to redo the conflict resolution. + +[5] Since only 4 modes are effectively allowed in git but 32-bit are + used to store them, having a two bit flag for the mode is enough + and saves 4 byte per entry. + +[6] The length of the file name was dropped, since each file name is + nul terminated anyway. + +[7] Since all stat data (except mtime and ctime) is just used for + checking if a file has changed a checksum of the data is enough. + In addition to that Thomas Rast suggested ctime could be ditched + completely (core.trustctime=false) and thus included in the + checksum. This would save 24 bytes per index entry, which would + be about 4 MB on the Webkit index. + (Thanks for the suggestion to Michael Haggerty) + +[8] Since there can be more stage #1 entries, it is necessary to know + the number of conflict data entries there are. + +[9] As Michael Haggerty pointed out on the mailing list, storing the + trailing slash will simplify a few operations.
Signature · 1 line
1.8.3.453.g1dfc63d
Re: [PATCH 13/22] documentation: add documentation of the index-v5 file format
Quoted 47 lines · Thomas Gummerer
+== File entry (fileentries)
Documentation/technical/index-file-format-v5.txt new file +46
-0+ + File entries are sorted in ascending order on the name field, after the + respective offset given by the directory entries. All file names are + prefix compressed, meaning the file name is relative to the directory. + + filename (variable length, nul terminated). The exact encoding is + undefined, but the filename cannot contain a NUL byte (iow, the same + encoding as a UNIX pathname). + + flags (16-bits): 'flags' field split into (high to low bits) + + assumevalid (1-bit): assume-valid flag + + intenttoadd (1-bit): intent-to-add flag, used by "git add -N". + Extended flag in index v3. + + stage (2-bit): stage of the file during merge + + skipworktree (1-bit): skip-worktree flag, used by sparse checkout. + Extended flag in index v3. + + smudged (1-bit): indicates if the file is racily smudged. + + 10-bit unused, must be zero [6] + + mode (16-bits): file mode, split into (high to low bits) + + objtype (4-bits): object type + valid values in binary are 1000 (regular file), 1010 (symbolic + link) and 1110 (gitlink) + + 3-bit unused + + permission (9-bits): unix permission. Only 0755 and 0644 are valid + for regular files. Symbolic links and gitlinks have value 0 in + this field. + + mtimes (32-bits): mtime seconds, the last time a file's data changed + this is stat(2) data + + mtimens (32-bits): mtime nanosecond fractions + this is stat(2) data + + file size (32-bits): The on-disk size, trucated to 32-bit. + this is stat(2) data +
Documentation/technical/index-file-format-v5.txt line 217
Quoted 10 lines
Documentation/technical/index-file-format-v5.txt new file +10
-0+ statcrc (32-bits): crc32 checksum over ctime seconds, ctime + nanoseconds, ino, dev, uid, gid (All stat(2) data + except mtime and file size). If the statcrc is 0 it will + be ignored. [7] + + objhash (160-bits): SHA-1 for the represented object + + entrycrc (32-bits): crc32 checksum for the file entry. The crc code + includes the offset to the offset to the file, relative to the + beginning of the file.
Question about the possibility of updating index file directly. If git updates a few fields of an entry (but not entrycrc yet) and crashes, the entry would become corrupt because its entrycrc does not match the content. What do we do? Do we need to save a copy of the entry somewhere in the index file (maybe in the conflict data section), so that the reader can recover the index? Losing the index because of bugs is big deal in my opinion. pre-v5 never faces this because we keep the original copy til the end.
Maybe entrycrc should not cover stat fields and statcrc. It would make refreshing safer. If the above happens during refresh, only statcrc is corrupt and we can just refresh the entry. entrycrc still says the other fields are good (and they are).
--
Duy
Re: [PATCH 13/22] documentation: add documentation of the index-v5 file format
Quoted 68 lines · Duy Nguyen
Quoted 47 lines · Thomas Gummerer
+== File entry (fileentries)
Documentation/technical/index-file-format-v5.txt new file +46
-0+ + File entries are sorted in ascending order on the name field, after the + respective offset given by the directory entries. All file names are + prefix compressed, meaning the file name is relative to the directory. + + filename (variable length, nul terminated). The exact encoding is + undefined, but the filename cannot contain a NUL byte (iow, the same + encoding as a UNIX pathname). + + flags (16-bits): 'flags' field split into (high to low bits) + + assumevalid (1-bit): assume-valid flag + + intenttoadd (1-bit): intent-to-add flag, used by "git add -N". + Extended flag in index v3. + + stage (2-bit): stage of the file during merge + + skipworktree (1-bit): skip-worktree flag, used by sparse checkout. + Extended flag in index v3. + + smudged (1-bit): indicates if the file is racily smudged. + + 10-bit unused, must be zero [6] + + mode (16-bits): file mode, split into (high to low bits) + + objtype (4-bits): object type + valid values in binary are 1000 (regular file), 1010 (symbolic + link) and 1110 (gitlink) + + 3-bit unused + + permission (9-bits): unix permission. Only 0755 and 0644 are valid + for regular files. Symbolic links and gitlinks have value 0 in + this field. + + mtimes (32-bits): mtime seconds, the last time a file's data changed + this is stat(2) data + + mtimens (32-bits): mtime nanosecond fractions + this is stat(2) data + + file size (32-bits): The on-disk size, trucated to 32-bit. + this is stat(2) data +Documentation/technical/index-file-format-v5.txt line 217
Quoted 10 lines
Documentation/technical/index-file-format-v5.txt new file +10
-0+ statcrc (32-bits): crc32 checksum over ctime seconds, ctime + nanoseconds, ino, dev, uid, gid (All stat(2) data + except mtime and file size). If the statcrc is 0 it will + be ignored. [7] + + objhash (160-bits): SHA-1 for the represented object + + entrycrc (32-bits): crc32 checksum for the file entry. The crc code + includes the offset to the offset to the file, relative to the + beginning of the file.Question about the possibility of updating index file directly. If git updates a few fields of an entry (but not entrycrc yet) and crashes, the entry would become corrupt because its entrycrc does not match the content. What do we do? Do we need to save a copy of the entry somewhere in the index file (maybe in the conflict data section), so that the reader can recover the index? Losing the index because of bugs is big deal in my opinion. pre-v5 never faces this because we keep the original copy til the end.
Quoted 4 lines
Maybe entrycrc should not cover stat fields and statcrc. It would make refreshing safer. If the above happens during refresh, only statcrc is corrupt and we can just refresh the entry. entrycrc still says the other fields are good (and they are).
The original idea was to change the lock-file for partial writing to make it work for this case. The exact structure of the file still has to be defined, but generally it would be done in the following steps:
1. Write the changed entry to the lock-file
2. Change the entry in the index
3. If we succeed delete the lock-file (commit the transaction)
If git crashes, and leaves the index corrupted, we can recover the information from the lock-file and write the new information to the index file and then delete the lock-file.
Re: [PATCH 13/22] documentation: add documentation of the index-v5 file format
Quoted 18 lines · Thomas Gummerer
Quoted 9 lines
Question about the possibility of updating index file directly. If git updates a few fields of an entry (but not entrycrc yet) and crashes, the entry would become corrupt because its entrycrc does not match the content. What do we do? Do we need to save a copy of the entry somewhere in the index file (maybe in the conflict data section), so that the reader can recover the index? Losing the index because of bugs is big deal in my opinion. pre-v5 never faces this because we keep the original copy til the end.
Quoted 4 lines
Maybe entrycrc should not cover stat fields and statcrc. It would make refreshing safer. If the above happens during refresh, only statcrc is corrupt and we can just refresh the entry. entrycrc still says the other fields are good (and they are).
The original idea was to change the lock-file for partial writing to make it work for this case. The exact structure of the file still has to be defined, but generally it would be done in the following steps:
Quoted 7 lines
1. Write the changed entry to the lock-file
2. Change the entry in the index
3. If we succeed delete the lock-file (commit the transaction)If git crashes, and leaves the index corrupted, we can recover the information from the lock-file and write the new information to the index file and then delete the lock-file.
Ah makes sense. Still concerned about refreshing though. Updated files are usually few while refreshed files could be a lot more, increasing the cost at #1.
--
Duy
Re: [PATCH 13/22] documentation: add documentation of the index-v5 file format
Quoted 23 lines · Duy Nguyen
Quoted 22 lines · Thomas Gummerer
Quoted 9 lines
Question about the possibility of updating index file directly. If git updates a few fields of an entry (but not entrycrc yet) and crashes, the entry would become corrupt because its entrycrc does not match the content. What do we do? Do we need to save a copy of the entry somewhere in the index file (maybe in the conflict data section), so that the reader can recover the index? Losing the index because of bugs is big deal in my opinion. pre-v5 never faces this because we keep the original copy til the end.
Quoted 4 lines
Maybe entrycrc should not cover stat fields and statcrc. It would make refreshing safer. If the above happens during refresh, only statcrc is corrupt and we can just refresh the entry. entrycrc still says the other fields are good (and they are).
The original idea was to change the lock-file for partial writing to make it work for this case. The exact structure of the file still has to be defined, but generally it would be done in the following steps:
1. Write the changed entry to the lock-file
2. Change the entry in the index
3. If we succeed delete the lock-file (commit the transaction)
Quoted 7 lines
Quoted 3 lines
If git crashes, and leaves the index corrupted, we can recover the information from the lock-file and write the new information to the index file and then delete the lock-file.
Ah makes sense. Still concerned about refreshing though. Updated files are usually few while refreshed files could be a lot more, increasing the cost at #1.
Any idea how common refreshing a big part of the cache is? If it's not to common, I'd prefer to leave the stat data and stat crc in the entrycrc, as we can inform the user if something is wrong with the index, be it from git failing, or from disk corruption.
On the other hand if refresh_cache is relatively common and usually changes a big part of the index we should leave them out, as git can still run correctly with incorrect stat data, but takes a little longer, because it may have to check the file contents. That will be trade-off to make here.
Re: [PATCH 13/22] documentation: add documentation of the index-v5 file format
Quoted 29 lines · Thomas Gummerer
Quoted 27 lines · Duy Nguyen
Quoted 25 lines · Thomas Gummerer
Quoted 9 lines
Question about the possibility of updating index file directly. If git updates a few fields of an entry (but not entrycrc yet) and crashes, the entry would become corrupt because its entrycrc does not match the content. What do we do? Do we need to save a copy of the entry somewhere in the index file (maybe in the conflict data section), so that the reader can recover the index? Losing the index because of bugs is big deal in my opinion. pre-v5 never faces this because we keep the original copy til the end.
Quoted 4 lines
Maybe entrycrc should not cover stat fields and statcrc. It would make refreshing safer. If the above happens during refresh, only statcrc is corrupt and we can just refresh the entry. entrycrc still says the other fields are good (and they are).
The original idea was to change the lock-file for partial writing to make it work for this case. The exact structure of the file still has to be defined, but generally it would be done in the following steps:
1. Write the changed entry to the lock-file
2. Change the entry in the index
3. If we succeed delete the lock-file (commit the transaction)If git crashes, and leaves the index corrupted, we can recover the information from the lock-file and write the new information to the index file and then delete the lock-file.
Quoted 5 lines
Quoted 3 lines
Ah makes sense. Still concerned about refreshing though. Updated files are usually few while refreshed files could be a lot more, increasing the cost at #1.
Any idea how common refreshing a big part of the cache is?
No, probably not common. Anyone who does "find|xargs touch" deserves to be punished. Files can be edited, then reverted by an editor, but there should not be many of those. The only sensible case is "git checkout <path>" with lots of modified files. But that can't happen often.
Quoted 9 lines
If it's not to common, I'd prefer to leave the stat data and stat crc in the entrycrc, as we can inform the user if something is wrong with the index, be it from git failing, or from disk corruption.
On the other hand if
refresh_cacheis relatively common and usually changes a big part of the index we should leave them out, as git can still run correctly with incorrect stat data, but takes a little longer, because it may have to check the file contents. That will be trade-off to make here.
--
Duy
PATCH 14/22 read-cache: make in-memory format aware of stat_crc
In reply to Thomas GummererMake the in-memory format aware of the stat_crc used by index-v5. It is simply ignored by index version prior to v5.
--- cache.h | 1 + read-cache.c | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+)
cache.h +1 -0
@@ -127,6 +127,7 @@ struct cache_entry {unsigned int ce_flags;unsigned int ce_namelen;unsigned char sha1[20 ]; +uint32_t ce_stat_crc;struct cache_entry *next;/* used by name_hash */ struct cache_entry *next_ce;/* used to keep a list of cache entries */ char name[FLEX_ARRAY ];/* more */
read-cache.c +25 -0
@@ -108,6 +108,29 @@ int match_stat_data(const struct stat_data *sd, struct stat *st)return changed; } +static uint32_t calculate_stat_crc (struct cache_entry *ce) +{ +unsigned int ctimens =0 ; +uint32_t stat, stat_crc; + + stat =htonl (ce->ce_stat_data.sd_ctime.sec); + stat_crc =crc32 (0 , (Bytef *)&stat,4 ); +#ifdef USE_NSEC + ctimens = ce->ce_stat_data.sd_ctime.nsec; +#endif + stat =htonl (ctimens); + stat_crc =crc32 (stat_crc, (Bytef *)&stat,4 ); + stat =htonl (ce->ce_stat_data.sd_ino); + stat_crc =crc32 (stat_crc, (Bytef *)&stat,4 ); + stat =htonl (ce->ce_stat_data.sd_dev); + stat_crc =crc32 (stat_crc, (Bytef *)&stat,4 ); + stat =htonl (ce->ce_stat_data.sd_uid); + stat_crc =crc32 (stat_crc, (Bytef *)&stat,4 ); + stat =htonl (ce->ce_stat_data.sd_gid); + stat_crc =crc32 (stat_crc, (Bytef *)&stat,4 ); +return stat_crc; +} +/* * This only updates the "non-critical" parts of the directory * cache, ie the parts that aren't tracked by GIT, and only used
@@ -122,6 +145,8 @@ void fill_stat_cache_info(struct cache_entry *ce, struct stat *st)if (S_ISREG (st->st_mode))ce_mark_uptodate (ce); + + ce->ce_stat_crc =calculate_stat_crc (ce); }static int ce_compare_data (const struct cache_entry *ce,struct stat *st)
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 15/22 read-cache: read index-v5
In reply to Thomas GummererMake git read the index file version 5 without complaining.
This version of the reader doesn't read neither the cache-tree nor the resolve undo data, but doesn't choke on an index that includes such data.
--- Makefile | 1 + cache.h | 75 ++++++- read-cache-v5.c | 658 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ read-cache.h | 1 + 4 files changed, 734 insertions(+), 1 deletion(-) create mode 100644 read-cache-v5.c
Makefile +1 -0
@@ -856,6 +856,7 @@ LIB_OBJS += quote.o LIB_OBJS += reachable.o LIB_OBJS += read-cache.o LIB_OBJS += read-cache-v2.o +LIB_OBJS += read-cache-v5.o LIB_OBJS += reflog-walk.o LIB_OBJS += refs.o LIB_OBJS += remote.o
cache.h +74 -1
116 lines not on this page · raw message
read-cache-v5.c new file +658 -0
658 lines not on this page · raw message
read-cache.h +1 -0
@@ -39,6 +39,7 @@ struct internal_ops {extern struct index_ops v2_ops;extern struct internal_ops v2_internal_ops; +extern struct index_ops v5_ops;#ifndef NEEDS_ALIGNED_ACCESS #define ntoh_s (var) ntohs(var)
Signature · 1 line
1.8.3.453.g1dfc63d
Re: [PATCH 15/22] read-cache: read index-v5
Quoted 18 lines · Thomas Gummerer
Make git read the index file version 5 without complaining.
This version of the reader doesn't read neither the cache-tree nor the resolve undo data, but doesn't choke on an index that includes such data.
---read-cache-v5.c new file +6
-0@@ -0,0 +1,658 @@ +static struct directory_entry *read_directories (unsigned int *dir_offset, +unsigned int *dir_table_offset, +void *mmap, +int mmap_size) +{ +int i, ondisk_directory_size;
read-cache-v5.c line 16
Quoted 10 lines
read-cache-v5.c new file +10
-0+uint32_t *filecrc, *beginning, *end; +struct directory_entry *current =NULL ; +struct ondisk_directory_entry *disk_de; +struct directory_entry *de; +unsigned int data_len, len; +char *name; + +/* Length of pathname + nul byte for termination + size of +* members of ondisk_directory_entry. (Just using the size +* of the stuct doesn't work, because there may be padding
s/stuct/struct/
read-cache-v5.c line 234
Quoted 2 lines
read-cache-v5.c new file +2
-0+* bytes for the struct) +*/
Also:
/*
* Format multi-line comment
* like this.
*/
Remaining multi-line comments appear to be formatted correctly.
Re: [PATCH 15/22] read-cache: read index-v5
Quoted 35 lines · Eric Sunshine
Quoted 18 lines · Thomas Gummerer
Make git read the index file version 5 without complaining.
This version of the reader doesn't read neither the cache-tree nor the resolve undo data, but doesn't choke on an index that includes such data.
---read-cache-v5.c new file +6
-0@@ -0,0 +1,658 @@ +static struct directory_entry *read_directories (unsigned int *dir_offset, +unsigned int *dir_table_offset, +void *mmap, +int mmap_size) +{ +int i, ondisk_directory_size;read-cache-v5.c line 16
Quoted 10 lines
read-cache-v5.c new file +10
-0+uint32_t *filecrc, *beginning, *end; +struct directory_entry *current =NULL ; +struct ondisk_directory_entry *disk_de; +struct directory_entry *de; +unsigned int data_len, len; +char *name; + +/* Length of pathname + nul byte for termination + size of +* members of ondisk_directory_entry. (Just using the size +* of the stuct doesn't work, because there may be padding s/stuct/struct/
read-cache-v5.c line 234
Quoted 2 lines
read-cache-v5.c new file +2
-0+* bytes for the struct) +*/
Quoted 8 lines
Also:
/*
* Format multi-line comment
* like this.
*/Remaining multi-line comments appear to be formatted correctly.
Thanks for catching this and the other typos.
PATCH 16/22 read-cache: read resolve-undo data
In reply to Thomas GummererMake git read the resolve-undo data from the index.
Since the resolve-undo data is joined with the conflicts in the ondisk format of the index file version 5, conflicts and resolved data is read at the same time, and the resolve-undo data is then converted to the in-memory format.
--- read-cache-v5.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+)
read-cache-v5.c +39 -0
@@ -1,5 +1,6 @@#include "cache.h" #include "read-cache.h" +#include "string-list.h" #include "resolve-undo.h" #include "cache-tree.h" #include "dir.h"
@@ -447,6 +448,43 @@ static int read_conflicts(struct conflict_entry **head,return 0 ; } +static void resolve_undo_convert_v5 (struct index_state *istate, +struct conflict_entry *conflict) +{ +int i; + +while (conflict) { +struct string_list_item *lost; +struct resolve_undo_info *ui; +struct conflict_part *cp; + +if (conflict->entries && + (conflict->entries->flags &CONFLICT_CONFLICTED ) !=0 ) { + conflict = conflict->next; +continue ; + } +if (!istate->resolve_undo) { + istate->resolve_undo =xcalloc (1 ,sizeof (struct string_list )); + istate->resolve_undo->strdup_strings =1 ; + } + + lost =string_list_insert (istate->resolve_undo, conflict->name); +if (!lost->util) + lost->util =xcalloc (1 ,sizeof (*ui)); + ui = lost->util; + + cp = conflict->entries; +for (i =0 ; i <3 ; i++) + ui->mode[i] =0 ; +while (cp) { + ui->mode[conflict_stage (cp) -1 ] = cp->entry_mode; +hashcpy (ui->sha1[conflict_stage (cp) -1 ], cp->sha1); + cp = cp->next; + } + conflict = conflict->next; + } +} + static int read_entries(struct index_state *istate, struct directory_entry **de, unsigned int *entry_offset, void **mmap, unsigned long mmap_size, unsigned int *nr,
@@ -460,6 +498,7 @@ static int read_entries(struct index_state *istate, struct directory_entry **de, conflict_queue =NULL ;if (read_conflicts (&conflict_queue, *de, mmap, mmap_size) <0 )return -1 ; +resolve_undo_convert_v5 (istate, conflict_queue);for (i =0 ; i < (*de)->de_nfiles; i++) { if (read_entry(&ce, *de,
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 17/22 read-cache: read cache-tree in index-v5
In reply to Thomas GummererSince the cache-tree data is saved as part of the directory data, we already read it at the beginning of the index. The cache-tree is only converted from this directory data.
The cache-tree data is arranged in a tree, with the children sorted by pathlen at each node, while the ondisk format is sorted lexically. So we have to rebuild this format from the on-disk directory list.
--- cache-tree.c | 2 +- cache-tree.h | 6 ++++ read-cache-v5.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-)
cache-tree.c +1 -1
@@ -31,7 +31,7 @@ void cache_tree_free(struct cache_tree **it_p) *it_p =NULL ; } -static int subtree_name_cmp (const char *one,int onelen, +int subtree_name_cmp (const char *one,int onelen,const char *two,int twolen) {if (onelen < twolen)
cache-tree.h +6 -0
@@ -21,10 +21,16 @@ struct cache_tree {struct cache_tree_sub **down; }; +struct directory_queue { +struct directory_queue *down; +struct directory_entry *de; +}; +struct cache_tree *cache_tree (void );void cache_tree_free (struct cache_tree **);void cache_tree_invalidate_path (struct cache_tree *,const char *);struct cache_tree_sub *cache_tree_sub (struct cache_tree *,const char *); +int subtree_name_cmp (const char *,int ,const char *,int );void cache_tree_write (struct strbuf *,struct cache_tree *root);struct cache_tree *cache_tree_read (const char *buffer,unsigned long size);
read-cache-v5.c +100 -0
118 lines not on this page · raw message
Signature · 1 line
1.8.3.453.g1dfc63d
Re: [PATCH 17/22] read-cache: read cache-tree in index-v5
Quoted 15 lines · Thomas Gummerer
Since the cache-tree data is saved as part of the directory data, we already read it at the beginning of the index. The cache-tree is only converted from this directory data.
The cache-tree data is arranged in a tree, with the children sorted by pathlen at each node, while the ondisk format is sorted lexically. So we have to rebuild this format from the on-disk directory list.
Signed-off-by Thomas Gummerer <[email protected]>---
read-cache-v5.c +0
-0
read-cache-v5.c static int read_conflicts(struct conflict_entry **head, · line 452
Quoted 5 lines
read-cache-v5.c +2
-0@@ -448,6 +448,103 @@ static int read_conflicts(struct conflict_entry **head,return 0 ; } +/* +* This function modifys the directory argument that is given to it.
s/modifys/modifies/
Quoted 19 lines
read-cache-v5.c +16
-0+* Don't use it if the directory entries are still needed after. +*/ +static struct cache_tree *cache_tree_convert_v5 (struct directory_entry *de) +{ +struct directory_queue *queue; + +if (!de->de_nentries) +return NULL ; + queue =xcalloc (1 ,sizeof (struct directory_queue )); + queue[0 ].de = de; + queue[0 ].down =xcalloc (de->de_nsubtrees,sizeof (struct directory_queue )); + +sort_directories (de, queue[0 ].down); +return convert_one (queue,0 ); +} +static void resolve_undo_convert_v5 (struct index_state *istate,struct conflict_entry *conflict) {
PATCH 18/22 read-cache: write index-v5
In reply to Thomas GummererWrite the index version 5 file format to disk. This version doesn't write the cache-tree data and resolve-undo data to the file.
The main work is done when filtering out the directories from the current in-memory format, where in the same turn also the conflicts and the file data is calculated.
--- cache.h | 8 + read-cache-v5.c | 594 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- read-cache.c | 11 +- read-cache.h | 1 + 4 files changed, 611 insertions(+), 3 deletions(-)
cache.h +8 -0
@@ -581,6 +581,7 @@ extern int unmerged_index(const struct index_state *);extern int verify_path (const char *path);extern struct cache_entry *index_name_exists (struct index_state *istate,const char *name,int namelen,int igncase);extern int index_name_pos (const struct index_state *,const char *name,int namelen); +extern struct directory_entry *init_directory_entry (char *pathname,int len);#define ADD_CACHE_OK_TO_ADD 1/* Ok to add */ #define ADD_CACHE_OK_TO_REPLACE 2/* Ok to replace file/directory */ #define ADD_CACHE_SKIP_DFCHECK 4/* Ok to skip DF conflict checks */
@@ -1379,6 +1380,13 @@ static inline ssize_t write_str_in_full(int fd, const char *str)return write_in_full (fd, str,strlen (str)); } +/* index-v5 helper functions */ +extern char *super_directory (const char *filename); +extern void insert_directory_entry (struct directory_entry *,struct hash_table *,int *,unsigned int *,uint32_t ); +extern void add_conflict_to_directory_entry (struct directory_entry *,struct conflict_entry *); +extern void add_part_to_conflict_entry (struct directory_entry *,struct conflict_entry *,struct conflict_part *); +extern struct conflict_entry *create_new_conflict (char *,int ,int ); +/* pager.c */ extern void setup_pager (void );extern const char *pager_program;
read-cache-v5.c +593 -1
603 lines not on this page · raw message
read-cache.c +9 -2
@@ -108,7 +108,7 @@ int match_stat_data(const struct stat_data *sd, struct stat *st)return changed; } -static uint32_t calculate_stat_crc (struct cache_entry *ce) +uint32_t calculate_stat_crc (struct cache_entry *ce) {unsigned int ctimens =0 ;uint32_t stat, stat_crc;
@@ -232,6 +232,8 @@ static void set_istate_ops(struct index_state *istate)if (istate->version >=2 && istate->version <=4 ) istate->ops = &v2_ops; +if (istate->version ==5 ) + istate->ops = &v5_ops; } int ce_match_stat_basic(struct index_state *istate,
@@ -1311,7 +1313,12 @@ static int verify_hdr_version(struct index_state *istate, hdr_version =ntohl (hdr->hdr_version);if (hdr_version <INDEX_FORMAT_LB ||INDEX_FORMAT_UB < hdr_version)return error ("bad index version %d" , hdr_version); - istate->ops = &v2_ops; + +if (hdr_version >=2 && hdr_version <=4 ) + istate->ops = &v2_ops; +else if (hdr_version ==5 ) + istate->ops = &v5_ops; +return 0 ; }
read-cache.h +1 -0
@@ -66,3 +66,4 @@ extern int ce_match_stat_basic(struct index_state *istate, const struct cache_en struct stat *st);extern int is_racy_timestamp (const struct index_state *istate,const struct cache_entry *ce);extern void set_index_entry (struct index_state *istate,int nr,struct cache_entry *ce); +extern uint32_t calculate_stat_crc (struct cache_entry *ce);
Signature · 1 line
1.8.3.453.g1dfc63d
Re: [PATCH 18/22] read-cache: write index-v5
Quoted 39 lines · Thomas Gummerer
Write the index version 5 file format to disk. This version doesn't write the cache-tree data and resolve-undo data to the file.
The main work is done when filtering out the directories from the current in-memory format, where in the same turn also the conflicts and the file data is calculated.
Helped-by Nguyen Thai Ngoc Duy <[email protected]>Helped-by Thomas Rast <[email protected]>Signed-off-by Thomas Gummerer <[email protected]>---
read-cache-v5.c +24
-0+static int write_index_v5 (struct index_state *istate,int newfd) +{ +struct cache_version_header hdr; +struct cache_header hdr_v5; +struct cache_entry **cache = istate->cache; +struct directory_entry *de; +struct ondisk_directory_entry *ondisk; +int entries = istate->cache_nr; +int i, removed, non_conflicted, total_dir_len, ondisk_directory_size; +int total_file_len, conflict_offset, offset_to_offset; +unsigned int ndir; +uint32_t crc; + +if (istate->partially_read) +die ("BUG: index: cannot write a partially read index" ); + +for (i = removed =0 ; i < entries; i++) { +if (cache[i]->ce_flags &CE_REMOVE ) + removed++; + } + hdr.hdr_signature =htonl (CACHE_SIGNATURE ); + hdr.hdr_version =htonl (istate->version); + hdr_v5.hdr_nfile =htonl (entries - removed); + hdr_v5.hdr_nextension =htonl (0 );/* Currently no extensions are supported */
read-cache-v5.c static void index_change_filter_opts_v5(struct index_state *istate, struct filte · line 1337
Quoted 10 lines
read-cache-v5.c +10
-0+ + non_conflicted =0 ; + total_dir_len =0 ; + total_file_len =0 ; + de =compile_directory_data (istate, entries, &ndir, &non_conflicted, + &total_dir_len, &total_file_len); + hdr_v5.hdr_ndir =htonl (ndir); + +/* +* This is needed because the compiler aligns structs to sizes multipe
s/multipe/multiple/
Quoted 45 lines
read-cache-v5.c +45
-0+* of 4 +*/ + ondisk_directory_size =sizeof (ondisk->flags) + +sizeof (ondisk->foffset) + +sizeof (ondisk->cr) + +sizeof (ondisk->ncr) + +sizeof (ondisk->nsubtrees) + +sizeof (ondisk->nfiles) + +sizeof (ondisk->nentries) + +sizeof (ondisk->sha1); + hdr_v5.hdr_fblockoffset =htonl (sizeof (hdr) +sizeof (hdr_v5) +4 + + (ndir +1 ) *4 + + total_dir_len + + ndir * (ondisk_directory_size +4 ) + + (non_conflicted +1 ) *4 ); + + crc =0 ; +if (ce_write (&crc, newfd, &hdr,sizeof (hdr)) <0 ) +return -1 ; +if (ce_write (&crc, newfd, &hdr_v5,sizeof (hdr_v5)) <0 ) +return -1 ; + crc =htonl (crc); +if (ce_write (NULL , newfd, &crc,4 ) <0 ) +return -1 ; + + conflict_offset =sizeof (hdr) +sizeof (hdr_v5) +4 + + (ndir +1 ) *4 + + total_dir_len + + ndir * (ondisk_directory_size +4 ) + + (non_conflicted +1 ) *4 + + total_file_len + + non_conflicted * (sizeof (struct ondisk_cache_entry ) +4 ); +if (write_directories (de, newfd, conflict_offset) <0 ) +return -1 ; + offset_to_offset =sizeof (hdr) +sizeof (hdr_v5) +4 + + (ndir +1 ) *4 + + total_dir_len + + ndir * (ondisk_directory_size +4 ); +if (write_entries (istate, de, entries, newfd, offset_to_offset) <0 ) +return -1 ; +if (write_conflicts (istate, de, newfd) <0 ) +return -1 ; +return ce_flush (newfd); +} +
PATCH 19/22 read-cache: write index-v5 cache-tree data
In reply to Thomas GummererWrite the cache-tree data for the index version 5 file format. The in-memory cache-tree data is converted to the ondisk format, by adding it to the directory entries, that were compiled from the cache-entries in the step before.
--- read-cache-v5.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+)
read-cache-v5.c +53 -0
@@ -960,6 +960,57 @@ static struct conflict_entry *create_conflict_entry_from_ce(struct cache_entry *return create_new_conflict (ce->name,ce_namelen (ce), pathlen); } +static void convert_one_to_ondisk_v5 (struct hash_table *table,struct cache_tree *it, +const char *path,int pathlen,uint32_t crc) +{ +int i; +struct directory_entry *found, *search; + + crc =crc32 (crc, (Bytef *)path, pathlen); + found =lookup_hash (crc, table); + search = found; +while (search &&strcmp (path, search->pathname + search->de_pathlen -strlen (path)) !=0 ) + search = search->next_hash; +if (!search) +return ; +/* +* The number of subtrees is already calculated by +* compile_directory_data, therefore we only need to +* add the entry_count +*/ + search->de_nentries = it->entry_count; +if (0 <= it->entry_count) +hashcpy (search->sha1, it->sha1); +if (strcmp (path,"" ) !=0 ) + crc =crc32 (crc, (Bytef *)"/" ,1 ); + +#if DEBUG +if (0 <= it->entry_count) +fprintf (stderr,"cache-tree <%.*s> (%d ent, %d subtree) %s\n" , + pathlen, path, it->entry_count, it->subtree_nr, +sha1_to_hex (it->sha1)); +else +fprintf (stderr,"cache-tree <%.*s> (%d subtree) invalid\n" , + pathlen, path, it->subtree_nr); +#endif + +for (i =0 ; i < it->subtree_nr; i++) { +struct cache_tree_sub *down = it->down[i]; +if (i) { +struct cache_tree_sub *prev = it->down[i-1 ]; +if (subtree_name_cmp (down->name, down->namelen, + prev->name, prev->namelen) <=0 ) +die ("fatal - unsorted cache subtree" ); + } +convert_one_to_ondisk_v5 (table, down->cache_tree, down->name, down->namelen, crc); + } +} + +static void cache_tree_to_ondisk_v5 (struct hash_table *table,struct cache_tree *root) +{ +convert_one_to_ondisk_v5 (table, root,"" ,0 ,0 ); +} + static struct directory_entry *compile_directory_data(struct index_state *istate, int nfile, unsigned int *ndir,
@@ -1065,6 +1116,8 @@ static struct directory_entry *compile_directory_data(struct index_state *istate previous_entry->next = no_subtrees; } } +if (istate->cache_tree) +cache_tree_to_ondisk_v5 (&table, istate->cache_tree);return de; }
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 20/22 read-cache: write resolve-undo data for index-v5
In reply to Thomas GummererMake git read the resolve-undo data from the index.
Since the resolve-undo data is joined with the conflicts in the ondisk format of the index file version 5, conflicts and resolved data is read at the same time, and the resolve-undo data is then converted to the in-memory format.
--- read-cache-v5.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 94 insertions(+)
read-cache-v5.c +94 -0
@@ -1011,6 +1011,99 @@ static void cache_tree_to_ondisk_v5(struct hash_table *table, struct cache_treeconvert_one_to_ondisk_v5 (table, root,"" ,0 ,0 ); } +static void resolve_undo_to_ondisk_v5 (struct hash_table *table, +struct string_list *resolve_undo, +unsigned int *ndir,int *total_dir_len, +struct directory_entry *de) +{ +struct string_list_item *item; +struct directory_entry *search; + +if (!resolve_undo) +return ; +for_each_string_list_item (item, resolve_undo) { +struct conflict_entry *conflict_entry; +struct resolve_undo_info *ui = item->util; +char *super; +int i, dir_len, len; +uint32_t crc; +struct directory_entry *found, *current, *new_tree; + +if (!ui) +continue ; + + super =super_directory (item->string); +if (!super) + dir_len =0 ; +else + dir_len =strlen (super); + crc =crc32 (0 , (Bytef *)super, dir_len); + found =lookup_hash (crc, table); + current =NULL ; + new_tree =NULL ; + +while (!found) { +struct directory_entry *new; + + new =init_directory_entry (super, dir_len); +if (!current) + current = new; +insert_directory_entry (new, table, total_dir_len, ndir, crc); +if (new_tree !=NULL ) + new->de_nsubtrees =1 ; + new->next = new_tree; + new_tree = new; + super =super_directory (super); +if (!super) + dir_len =0 ; +else + dir_len =strlen (super); + crc =crc32 (0 , (Bytef *)super, dir_len); + found =lookup_hash (crc, table); + } + search = found; +while (search->next_hash &&strcmp (super, search->pathname) !=0 ) + search = search->next_hash; +if (search && !current) + current = search; +if (!search && !current) + current = new_tree; +if (!super && new_tree) { + new_tree->next = de->next; + de->next = new_tree; + de->de_nsubtrees++; + }else if (new_tree) { +struct directory_entry *temp; + + search = de->next; +while (strcmp (super, search->pathname)) + search = search->next; + temp = new_tree; +while (temp->next) + temp = temp->next; + search->de_nsubtrees++; + temp->next = search->next; + search->next = new_tree; + } + + len =strlen (item->string); + conflict_entry =create_new_conflict (item->string, len, current->de_pathlen); +add_conflict_to_directory_entry (current, conflict_entry); +for (i =0 ; i <3 ; i++) { +if (ui->mode[i]) { +struct conflict_part *cp; + + cp =xmalloc (sizeof (struct conflict_part )); + cp->flags = (i +1 ) <<CONFLICT_STAGESHIFT ; + cp->entry_mode = ui->mode[i]; + cp->next =NULL ; +hashcpy (cp->sha1, ui->sha1[i]); +add_part_to_conflict_entry (current, conflict_entry, cp); + } + } + } +} + static struct directory_entry *compile_directory_data(struct index_state *istate, int nfile, unsigned int *ndir,
@@ -1118,6 +1211,7 @@ static struct directory_entry *compile_directory_data(struct index_state *istate }if (istate->cache_tree)cache_tree_to_ondisk_v5 (&table, istate->cache_tree); +resolve_undo_to_ondisk_v5 (&table, istate->resolve_undo, ndir, total_dir_len, de);return de; }
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 21/22 update-index.c: rewrite index when index-version is given
In reply to Thomas GummererMake update-index always rewrite the index when a index-version is given, even if the index already has the right version. This option is used for performance testing the writer and reader.
--- builtin/update-index.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
builtin/update-index.c +2 -2
@@ -6,6 +6,7 @@#include "cache.h" #include "quote.h" #include "cache-tree.h" +#include "read-cache.h" #include "tree-walk.h" #include "builtin.h" #include "refs.h"
@@ -863,8 +864,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) preferred_index_format, INDEX_FORMAT_LB, INDEX_FORMAT_UB); -if (the_index.version != preferred_index_format) - active_cache_changed =1 ; + active_cache_changed =1 ; the_index.version = preferred_index_format; }
Signature · 1 line
1.8.3.453.g1dfc63d
PATCH 22/22 p0003-index.sh: add perf test for the index formats
In reply to Thomas GummererFrom: Thomas Rast <[email protected]>
Add a performance test for index version [23]/4/5 by using git update-index --index-version=x, thus testing both the reader and the writer speed of all index formats.
--- t/perf/p0003-index.sh | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100755 t/perf/p0003-index.sh
t/perf/p0003-index.sh new file +59 -0
@@ -0,0 +1,59 @@ +#!/bin/sh + +test_description="Tests index versions [23]/4/5" + +. ./perf-lib.sh + +test_perf_large_repo + +test_expect_success "convert to v3" " + git update-index --index-version=2 +" + +test_perf "v[23]: update-index" " + git update-index --index-version=2 >/dev/null +" + +subdir=$(git ls-files | sed 's#/[^/]*$##' | grep -v '^$' | uniq | tail -n 30 | head -1) + +test_perf "v[23]: grep nonexistent -- subdir" " + test_must_fail git grep nonexistent -- $subdir >/dev/null +" + +test_perf "v[23]: ls-files -- subdir" " + git ls-files $subdir >/dev/null +" + +test_expect_success "convert to v4" " + git update-index --index-version=4 +" + +test_perf "v4: update-index" " + git update-index --index-version=4 >/dev/null +" + +test_perf "v4: grep nonexistent -- subdir" " + test_must_fail git grep nonexistent -- $subdir >/dev/null +" + +test_perf "v4: ls-files -- subdir" " + git ls-files $subdir >/dev/null +" + +test_expect_success "convert to v5" " + git update-index --index-version=5 +" + +test_perf "v5: update-index" " + git update-index --index-version=5 >/dev/null +" + +test_perf "v5: grep nonexistent -- subdir" " + test_must_fail git grep nonexistent -- $subdir >/dev/null +" + +test_perf "v5: ls-files -- subdir" " + git ls-files $subdir >/dev/null +" + +test_done
Signature · 1 line
1.8.3.453.g1dfc63d