Split index mode for very large indexes
Subsystems (2)
cover Split index mode for very large indexes
I hinted about it earlier [1]. It now passes the test suite and with a design that I'm happy with (thanks to Junio for a suggestion about the rename problem).
From the user point of view, this reduces the writable size of index down to the number of updated files. For example my webkit index v4 is 14MB. With a fresh split, I only have to update an index of 200KB. Every file I touch will add about 80 bytes to that. As long as I don't touch every single tracked file in my worktree, I should not pay penalty for writing 14MB index file on every operation.
The read penalty is not addressed here, so I still pay 14MB hashing cost. But that's an easy problem. We could cache the validated index in a daemon. Whenever git needs to load an index, it pokes the daemon. The daemon verifies that the on-disk index still has the same signature, then sends the in-mem index to git. When git updates the index, it pokes the daemon again to update in-mem index. Next time git reads the index, it does not have to pay I/O cost any more (actually it does but the cost is hidden away when you do not have to read it yet).
The forth patch is not really necessary. I started out with a different approach that needed that abstraction. But I think it's still a nice thing to keep. The real meat starts from 0017 to 0025. In essence, the new index is more like a journal, where the real index is put away unchanged.
Doing this in other implementations should be easy (at least the reading part) and with small code change. The whole index format is retained. All you need is to read a new extension that contains two ewah-bitmaps and apply the changes to create the final index.
This is a preparation step for my untracked file cache. With writing (and later on reading) index becoming cheap, I can start to put more things in there.
[1] http://thread.gmane.org/gmane.comp.version-control.git/246471/focus=247031
Nguyễn Thái Ngọc Duy (32):
ewah: fix constness of ewah_read_mmap
ewah: delete unused ewah_read_mmap_native declaration
sequencer: do not update/refresh index if the lock cannot be held
read-cache: new API write_locked_index instead of write_index/write_cache
read-cache: relocate and unexport commit_locked_index()
read-cache: store in-memory flags in the first 12 bits of ce_flags
read-cache: be strict about "changed" in remove_marked_cache_entries()
read-cache: be specific what part of the index has changed
update-index: be specific what part of the index has changed
resolve-undo: be specific what part of the index has changed
unpack-trees: be specific what part of the index has changed
cache-tree: mark istate->cache_changed on cache tree invalidation
cache-tree: mark istate->cache_changed on cache tree update
cache-tree: mark istate->cache_changed on prime_cache_tree() entry.c: update cache_changed if refresh_cache is set in checkout_entry()
read-cache: save index SHA-1 after reading
read-cache: split-index mode
read-cache: mark new entries for split index
read-cache: save deleted entries in split index
read-cache: mark updated entries for split index
split-index: the writing part
split-index: the reading part
split-index: do not invalidate cache-tree at read time
split-index: strip pathname of on-disk replaced entries
update-index: new options to enable/disable split index mode update-index --split-index: do not split if $GIT_DIR is read only
rev-parse: add --shared-index-path to get shared index path
read-tree: force split-index mode off on --index-output
read-tree: note about dropping split-index mode or index version
read-cache: force split index mode with GIT_TEST_SPLIT_INDEX
t2104: make sure split index mode is off for the version test
t1700: new tests for split-index mode
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 1/32 ewah: fix constness of ewah_read_mmap
--- ewah/ewah_io.c | 4 ++-- ewah/ewok.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-)
ewah/ewah_io.c +2 -2
@@ -110,9 +110,9 @@ int ewah_serialize(struct ewah_bitmap *self, int fd)return ewah_serialize_to (self, write_helper, (void *)(intptr_t )fd); } -int ewah_read_mmap (struct ewah_bitmap *self,void *map,size_t len) +int ewah_read_mmap (struct ewah_bitmap *self,const void *map,size_t len) { -uint8_t *ptr = map; +const uint8_t *ptr = map;size_t i; self->bit_size =get_be32 (ptr);
ewah/ewok.h +1 -1
@@ -99,7 +99,7 @@ int ewah_serialize(struct ewah_bitmap *self, int fd);int ewah_serialize_native (struct ewah_bitmap *self,int fd);int ewah_deserialize (struct ewah_bitmap *self,int fd); -int ewah_read_mmap (struct ewah_bitmap *self,void *map,size_t len); +int ewah_read_mmap (struct ewah_bitmap *self,const void *map,size_t len);int ewah_read_mmap_native (struct ewah_bitmap *self,void *map,size_t len);uint32_t ewah_checksum (struct ewah_bitmap *self);
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 2/32 ewah: delete unused ewah_read_mmap_native declaration
In reply to Nguyễn Thái Ngọc Duy--- ewah/ewok.h | 1 - 1 file changed, 1 deletion(-)
ewah/ewok.h +0 -1
@@ -100,7 +100,6 @@ int ewah_serialize_native(struct ewah_bitmap *self, int fd);int ewah_deserialize (struct ewah_bitmap *self,int fd);int ewah_read_mmap (struct ewah_bitmap *self,const void *map,size_t len); -int ewah_read_mmap_native (struct ewah_bitmap *self,void *map,size_t len);uint32_t ewah_checksum (struct ewah_bitmap *self);
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 3/32 sequencer: do not update/refresh index if the lock cannot be held
In reply to Nguyễn Thái Ngọc Duy--- sequencer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
sequencer.c +1 -1
@@ -679,7 +679,7 @@ static void read_and_refresh_cache(struct replay_opts *opts)if (read_index_preload (&the_index,NULL ) <0 )die (_ ("git %s: failed to read the index" ),action_name (opts));refresh_index (&the_index,REFRESH_QUIET |REFRESH_UNMERGED ,NULL ,NULL ,NULL ); -if (the_index.cache_changed) { +if (the_index.cache_changed && index_fd >=0 ) {if (write_index (&the_index, index_fd) ||commit_locked_index (&index_lock))die (_ ("git %s: failed to refresh the index" ),action_name (opts));
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 4/32 read-cache: new API write_locked_index instead of write_index/write_cache
In reply to Nguyễn Thái Ngọc Duy--- builtin/add.c | 6 ++---- builtin/apply.c | 9 ++++----- builtin/checkout-index.c | 3 +-- builtin/checkout.c | 11 ++++------- builtin/clone.c | 7 +++---- builtin/commit.c | 33 ++++++++++++++------------------- builtin/merge.c | 12 ++++-------- builtin/mv.c | 7 +++---- builtin/read-tree.c | 7 +++---- builtin/reset.c | 5 ++--- builtin/rm.c | 7 +++---- builtin/update-index.c | 3 +-- cache-tree.c | 3 +-- cache.h | 6 ++++-- merge-recursive.c | 7 +++---- merge.c | 7 +++---- read-cache.c | 28 ++++++++++++++++++++++++---- rerere.c | 3 +-- sequencer.c | 12 +++++------- test-scrap-cache-tree.c | 5 ++--- 20 files changed, 87 insertions(+), 94 deletions(-)
builtin/add.c +2 -4
@@ -299,7 +299,6 @@ static int add_files(struct dir_struct *dir, int flags)int cmd_add (int argc,const char **argv,const char *prefix) {int exit_status =0 ; -int newfd;struct pathspec pathspec;struct dir_struct dir;int flags;
@@ -345,7 +344,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) add_new_files = !take_worktree_changes && !refresh_only; require_pathspec = !take_worktree_changes; - newfd =hold_locked_index (&lock_file,1 ); +hold_locked_index (&lock_file,1 ); flags = ((verbose ? ADD_CACHE_VERBOSE : 0) | (show_only ? ADD_CACHE_PRETEND : 0) |
@@ -443,8 +442,7 @@ int cmd_add(int argc, const char **argv, const char *prefix) finish:if (active_cache_changed) { -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (&lock_file)) +if (write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die (_ ("Unable to write new index file" )); }
builtin/apply.c +4 -5
@@ -3644,7 +3644,7 @@ static void build_fake_ancestor(struct patch *list, const char *filename) {struct patch *patch;struct index_state result = {NULL }; -int fd; +static struct lock_file lock;/* Once we start supporting the reverse patch, it may be * worth showing the new sha1 prefix, but until then...
@@ -3682,8 +3682,8 @@ static void build_fake_ancestor(struct patch *list, const char *filename)die ("Could not add %s to temporary index" , name); } - fd =open (filename,O_WRONLY |O_CREAT ,0666 ); -if (fd <0 ||write_index (&result, fd) ||close (fd)) +hold_lock_file_for_update (&lock, filename,LOCK_DIE_ON_ERROR ); +if (write_locked_index (&result, &lock,COMMIT_LOCK ))die ("Could not write temporary index to %s" , filename);discard_index (&result);
@@ -4501,8 +4501,7 @@ int cmd_apply(int argc, const char **argv, const char *prefix_) }if (update_index) { -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (&lock_file)) +if (write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die (_ ("Unable to write new index file" )); }
builtin/checkout-index.c +1 -2
@@ -279,8 +279,7 @@ int cmd_checkout_index(int argc, const char **argv, const char *prefix)checkout_all (prefix, prefix_length);if (0 <= newfd && - (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (&lock_file))) +write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die ("Unable to write new index file" );return 0 ; }
builtin/checkout.c +4 -7
@@ -225,7 +225,6 @@ static int checkout_paths(const struct checkout_opts *opts,int flag;struct commit *head;int errs =0 ; -int newfd;struct lock_file *lock_file;if (opts->track !=BRANCH_TRACK_UNSPECIFIED )
@@ -256,7 +255,7 @@ static int checkout_paths(const struct checkout_opts *opts, lock_file =xcalloc (1 ,sizeof (struct lock_file )); - newfd =hold_locked_index (lock_file,1 ); +hold_locked_index (lock_file,1 );if (read_cache_preload (&opts->pathspec) <0 )return error (_ ("corrupt index file" ));
@@ -352,8 +351,7 @@ static int checkout_paths(const struct checkout_opts *opts, } } -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (lock_file)) +if (write_locked_index (&the_index, lock_file,COMMIT_LOCK ))die (_ ("unable to write new index file" ));read_ref_full ("HEAD" , rev,0 , &flag);
@@ -444,8 +442,8 @@ static int merge_working_tree(const struct checkout_opts *opts, {int ret;struct lock_file *lock_file =xcalloc (1 ,sizeof (struct lock_file )); -int newfd =hold_locked_index (lock_file,1 ); +hold_locked_index (lock_file,1 );if (read_cache_preload (NULL ) <0 )return error (_ ("corrupt index file" ));
@@ -553,8 +551,7 @@ static int merge_working_tree(const struct checkout_opts *opts, } } -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (lock_file)) +if (write_locked_index (&the_index, lock_file,COMMIT_LOCK ))die (_ ("unable to write new index file" ));if (!opts->force && !opts->quiet)
builtin/clone.c +3 -4
@@ -616,7 +616,7 @@ static int checkout(void)struct unpack_trees_options opts;struct tree *tree;struct tree_desc t; -int err =0 , fd; +int err =0 ;if (option_no_checkout)return 0 ;
@@ -640,7 +640,7 @@ static int checkout(void)setup_work_tree (); lock_file =xcalloc (1 ,sizeof (struct lock_file )); - fd =hold_locked_index (lock_file,1 ); +hold_locked_index (lock_file,1 );memset (&opts,0 ,sizeof opts); opts.update =1 ;
@@ -656,8 +656,7 @@ static int checkout(void)if (unpack_trees (1 , &t, &opts) <0 )die (_ ("unable to checkout working tree" )); -if (write_cache (fd, active_cache, active_nr) || -commit_locked_index (lock_file)) +if (write_locked_index (&the_index, lock_file,COMMIT_LOCK ))die (_ ("unable to write new index file" )); err |= run_hook_le(NULL, "post-checkout", sha1_to_hex(null_sha1),
builtin/commit.c +14 -19
@@ -305,7 +305,6 @@ static void refresh_cache_or_die(int refresh_flags)static char *prepare_index (int argc,const char **argv,const char *prefix,const struct commit *current_head,int is_status) { -int fd;struct string_list partial;struct pathspec pathspec;int refresh_flags =REFRESH_QUIET ;
@@ -321,12 +320,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,if (interactive) {char *old_index_env =NULL ; - fd =hold_locked_index (&index_lock,1 ); +hold_locked_index (&index_lock,1 );refresh_cache_or_die (refresh_flags); -if (write_cache (fd, active_cache, active_nr) || -close_lock_file (&index_lock)) +if (write_locked_index (&the_index, &index_lock,CLOSE_LOCK ))die (_ ("unable to create temporary index" )); old_index_env =getenv (INDEX_ENVIRONMENT );
@@ -360,12 +358,11 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,* (B) on failure, rollback the real index. */ if (all || (also && pathspec.nr)) { - fd =hold_locked_index (&index_lock,1 ); +hold_locked_index (&index_lock,1 );add_files_to_cache (also ? prefix :NULL , &pathspec,0 );refresh_cache_or_die (refresh_flags);update_main_cache_tree (WRITE_TREE_SILENT ); -if (write_cache (fd, active_cache, active_nr) || -close_lock_file (&index_lock)) +if (write_locked_index (&the_index, &index_lock,CLOSE_LOCK ))die (_ ("unable to write new_index file" )); commit_style =COMMIT_NORMAL ;return index_lock.filename;
@@ -381,12 +378,12 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,* We still need to refresh the index here. */ if (!only && !pathspec.nr) { - fd =hold_locked_index (&index_lock,1 ); +hold_locked_index (&index_lock,1 );refresh_cache_or_die (refresh_flags);if (active_cache_changed) {update_main_cache_tree (WRITE_TREE_SILENT ); -if (write_cache (fd, active_cache, active_nr) || -commit_locked_index (&index_lock)) +if (write_locked_index (&the_index, &index_lock, +COMMIT_LOCK ))die (_ ("unable to write new_index file" )); }else {rollback_lock_file (&index_lock);
@@ -432,24 +429,22 @@ static char *prepare_index(int argc, const char **argv, const char *prefix,if (read_cache () <0 )die (_ ("cannot read the index" )); - fd =hold_locked_index (&index_lock,1 ); +hold_locked_index (&index_lock,1 );add_remove_files (&partial);refresh_cache (REFRESH_QUIET ); -if (write_cache (fd, active_cache, active_nr) || -close_lock_file (&index_lock)) +if (write_locked_index (&the_index, &index_lock,CLOSE_LOCK ))die (_ ("unable to write new_index file" )); - fd =hold_lock_file_for_update (&false_lock, -git_path ("next-index-%" PRIuMAX, - (uintmax_t )getpid ()), -LOCK_DIE_ON_ERROR ); +hold_lock_file_for_update (&false_lock, +git_path ("next-index-%" PRIuMAX, + (uintmax_t )getpid ()), +LOCK_DIE_ON_ERROR );create_base_index (current_head);add_remove_files (&partial);refresh_cache (REFRESH_QUIET ); -if (write_cache (fd, active_cache, active_nr) || -close_lock_file (&false_lock)) +if (write_locked_index (&the_index, &false_lock,CLOSE_LOCK ))die (_ ("unable to write temporary index file" ));discard_cache ();
builtin/merge.c +4 -8
@@ -657,14 +657,12 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,struct commit_list *remoteheads, struct commit *head, const char *head_arg) { - int index_fd; struct lock_file *lock = xcalloc(1, sizeof(struct lock_file)); - index_fd =hold_locked_index (lock,1 ); +hold_locked_index (lock,1 );refresh_cache (REFRESH_QUIET );if (active_cache_changed && - (write_cache (index_fd, active_cache, active_nr) || -commit_locked_index (lock))) +write_locked_index (&the_index, lock,COMMIT_LOCK ))return error (_ ("Unable to write index." ));rollback_lock_file (lock);
@@ -672,7 +670,6 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,int clean, x;struct commit *result;struct lock_file *lock =xcalloc (1 ,sizeof (struct lock_file )); -int index_fd;struct commit_list *reversed =NULL ;struct merge_options o;struct commit_list *j;
@@ -700,12 +697,11 @@ static int try_merge_strategy(const char *strategy, struct commit_list *common,for (j = common; j; j = j->next)commit_list_insert (j->item, &reversed); - index_fd =hold_locked_index (lock,1 ); +hold_locked_index (lock,1 ); clean =merge_recursive (&o, head, remoteheads->item, reversed, &result);if (active_cache_changed && - (write_cache (index_fd, active_cache, active_nr) || -commit_locked_index (lock))) +write_locked_index (&the_index, lock,COMMIT_LOCK ))die (_ ("unable to write %s" ),get_index_file ());rollback_lock_file (lock);return clean ?0 :1 ;
builtin/mv.c +3 -4
@@ -63,7 +63,7 @@ static struct lock_file lock_file;int cmd_mv (int argc,const char **argv,const char *prefix) { -int i, newfd, gitmodules_modified =0 ; +int i, gitmodules_modified =0 ;int verbose =0 , show_only =0 , force =0 , ignore_errors =0 ;struct option builtin_mv_options[] = {OPT__VERBOSE (&verbose,N_ ("be verbose" )),
@@ -85,7 +85,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)if (--argc <1 )usage_with_options (builtin_mv_usage, builtin_mv_options); - newfd =hold_locked_index (&lock_file,1 ); +hold_locked_index (&lock_file,1 );if (read_cache () <0 )die (_ ("index file corrupt" ));
@@ -275,8 +275,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)stage_updated_gitmodules ();if (active_cache_changed) { -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (&lock_file)) +if (write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die (_ ("Unable to write new index file" )); }
builtin/read-tree.c +3 -4
@@ -99,7 +99,7 @@ static struct lock_file lock_file;int cmd_read_tree (int argc,const char **argv,const char *unused_prefix) { -int i, newfd, stage =0 ; +int i, stage =0 ;unsigned char sha1[20 ];struct tree_desc t[MAX_UNPACK_TREES ];struct unpack_trees_options opts;
@@ -149,7 +149,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix) argc =parse_options (argc, argv, unused_prefix, read_tree_options, read_tree_usage,0 ); - newfd =hold_locked_index (&lock_file,1 ); +hold_locked_index (&lock_file,1 ); prefix_set = opts.prefix ?1 :0 ;if (1 < opts.merge + opts.reset + prefix_set)
@@ -233,8 +233,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)if (nr_trees ==1 && !opts.prefix)prime_cache_tree (&active_cache_tree, trees[0 ]); -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (&lock_file)) +if (write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die ("unable to write new index file" );return 0 ; }
builtin/reset.c +2 -3
@@ -351,7 +351,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)if (reset_type !=SOFT ) {struct lock_file *lock =xcalloc (1 ,sizeof (*lock)); -int newfd =hold_locked_index (lock,1 ); +hold_locked_index (lock,1 );if (reset_type ==MIXED ) {int flags = quiet ?REFRESH_QUIET :REFRESH_IN_PORCELAIN ;if (read_from_tree (&pathspec, sha1, intent_to_add))
@@ -367,8 +367,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)die (_ ("Could not reset index file to revision '%s'." ), rev); } -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (lock)) +if (write_locked_index (&the_index, lock,COMMIT_LOCK ))die (_ ("Could not write new index file." )); }
builtin/rm.c +3 -4
@@ -278,7 +278,7 @@ static struct option builtin_rm_options[] = {int cmd_rm (int argc,const char **argv,const char *prefix) { -int i, newfd; +int i;struct pathspec pathspec;char *seen;
@@ -293,7 +293,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix)if (!index_only)setup_work_tree (); - newfd =hold_locked_index (&lock_file,1 ); +hold_locked_index (&lock_file,1 );if (read_cache () <0 )die (_ ("index file corrupt" ));
@@ -427,8 +427,7 @@ int cmd_rm(int argc, const char **argv, const char *prefix) }if (active_cache_changed) { -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (&lock_file)) +if (write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die (_ ("Unable to write new index file" )); }
builtin/update-index.c +1 -2
@@ -921,8 +921,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)exit (128 );unable_to_lock_index_die (get_index_file (), lock_error); } -if (write_cache (newfd, active_cache, active_nr) || -commit_locked_index (lock_file)) +if (write_locked_index (&the_index, lock_file,COMMIT_LOCK ))die ("Unable to write new index file" ); }
cache-tree.c +1 -2
@@ -595,8 +595,7 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix) active_nr, flags) < 0)return WRITE_TREE_UNMERGED_INDEX ;if (0 <= newfd) { -if (!write_cache (newfd, active_cache, active_nr) && - !commit_lock_file (lock_file)) +if (!write_locked_index (&the_index, lock_file,COMMIT_LOCK )) newfd =-1 ; }/* Not being able to write is fine -- we are only interested
cache.h +4 -2
@@ -301,7 +301,6 @@ extern void free_name_hash(struct index_state *istate);#define read_cache_preload (pathspec) read_index_preload(&the_index, (pathspec))#define is_cache_unborn () is_index_unborn(&the_index)#define read_cache_unmerged () read_index_unmerged(&the_index) -#define write_cache (newfd, cache, entries) write_index(&the_index, (newfd))#define discard_cache () discard_index(&the_index)#define unmerged_cache () unmerged_index(&the_index)#define cache_name_pos (name, namelen) index_name_pos(&the_index,(name),(namelen))
@@ -456,12 +455,15 @@ extern int daemonize(void); }while (0 )/* Initialize and use the cache information */ +struct lock_file;extern int read_index (struct index_state *);extern int read_index_preload (struct index_state *,const struct pathspec *pathspec);extern int read_index_from (struct index_state *,const char *path);extern int is_index_unborn (struct index_state *);extern int read_index_unmerged (struct index_state *); -extern int write_index (struct index_state *,int newfd); +#define COMMIT_LOCK (1 << 0) +#define CLOSE_LOCK (1 << 1) +extern int write_locked_index (struct index_state *,struct lock_file *lock,unsigned flags);extern int discard_index (struct index_state *);extern int unmerged_index (const struct index_state *);extern int verify_path (const char *path);
merge-recursive.c +3 -4
@@ -1986,7 +1986,7 @@ int merge_recursive_generic(struct merge_options *o,const unsigned char **base_list, struct commit **result) { - int clean, index_fd; + int clean;struct lock_file *lock =xcalloc (1 ,sizeof (struct lock_file ));struct commit *head_commit =get_ref (head, o->branch1);struct commit *next_commit =get_ref (merge, o->branch2);
@@ -2003,12 +2003,11 @@ int merge_recursive_generic(struct merge_options *o, } } - index_fd =hold_locked_index (lock,1 ); +hold_locked_index (lock,1 ); clean =merge_recursive (o, head_commit, next_commit, ca, result);if (active_cache_changed && - (write_cache (index_fd, active_cache, active_nr) || -commit_locked_index (lock))) +write_locked_index (&the_index, lock,COMMIT_LOCK ))return error (_ ("Unable to write index." ));return clean ?0 :1 ;
merge.c +3 -4
@@ -66,13 +66,13 @@ int checkout_fast_forward(const unsigned char *head,struct tree *trees[MAX_UNPACK_TREES ];struct unpack_trees_options opts;struct tree_desc t[MAX_UNPACK_TREES ]; -int i, fd, nr_trees =0 ; +int i, nr_trees =0 ;struct dir_struct dir;struct lock_file *lock_file =xcalloc (1 ,sizeof (struct lock_file ));refresh_cache (REFRESH_QUIET ); - fd =hold_locked_index (lock_file,1 ); +hold_locked_index (lock_file,1 );memset (&trees,0 ,sizeof (trees));memset (&opts,0 ,sizeof (opts));
@@ -105,8 +105,7 @@ int checkout_fast_forward(const unsigned char *head, }if (unpack_trees (nr_trees, t, &opts))return -1 ; -if (write_cache (fd, active_cache, active_nr) || -commit_locked_index (lock_file)) +if (write_locked_index (&the_index, lock_file,COMMIT_LOCK ))die (_ ("unable to write new index file" ));return 0 ; }
read-cache.c +24 -4
@@ -1779,13 +1779,11 @@ static int has_racy_timestamp(struct index_state *istate)void update_index_if_able (struct index_state *istate,struct lock_file *lockfile) {if ((istate->cache_changed ||has_racy_timestamp (istate)) && - !write_index (istate, lockfile->fd)) -commit_locked_index (lockfile); -else +write_locked_index (istate, lockfile,COMMIT_LOCK ))rollback_lock_file (lockfile); } -int write_index (struct index_state *istate,int newfd) +static int do_write_index (struct index_state *istate,int newfd) {git_SHA_CTX c;struct cache_header hdr;
@@ -1877,6 +1875,28 @@ int write_index(struct index_state *istate, int newfd)return 0 ; } +static int do_write_locked_index (struct index_state *istate,struct lock_file *lock, +unsigned flags) +{ +int ret =do_write_index (istate, lock->fd); +if (ret) +return ret; +assert ((flags & (COMMIT_LOCK |CLOSE_LOCK )) != + (COMMIT_LOCK |CLOSE_LOCK )); +if (flags &COMMIT_LOCK ) +return commit_locked_index (lock); +else if (flags &CLOSE_LOCK ) +return close_lock_file (lock); +else +return ret; +} + +int write_locked_index (struct index_state *istate,struct lock_file *lock, +unsigned flags) +{ +return do_write_locked_index (istate, lock, flags); +} +/* * Read the index file that is potentially unmerged into given * index_state, dropping any unmerged entries. Returns true if
rerere.c +1 -2
@@ -492,8 +492,7 @@ static int update_paths(struct string_list *update) }if (!status && active_cache_changed) { -if (write_cache (fd, active_cache, active_nr) || -commit_locked_index (&index_lock)) +if (write_locked_index (&the_index, &index_lock,COMMIT_LOCK ))die ("Unable to write new index file" ); }else if (fd >=0 )rollback_lock_file (&index_lock);
sequencer.c +5 -7
@@ -294,11 +294,11 @@ static int do_recursive_merge(struct commit *base, struct commit *next, {struct merge_options o;struct tree *result, *next_tree, *base_tree, *head_tree; -int clean, index_fd; +int clean;const char **xopt;static struct lock_file index_lock; - index_fd =hold_locked_index (&index_lock,1 ); +hold_locked_index (&index_lock,1 );read_cache ();
@@ -319,8 +319,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next, next_tree, base_tree, &result);if (active_cache_changed && - (write_cache (index_fd, active_cache, active_nr) || -commit_locked_index (&index_lock))) +write_locked_index (&the_index, &index_lock,COMMIT_LOCK ))/* TRANSLATORS: %s will be "revert" or "cherry-pick" */ die (_ ("%s: Unable to write new index file" ),action_name (opts));rollback_lock_file (&index_lock);
@@ -675,13 +674,12 @@ static void prepare_revs(struct replay_opts *opts)static void read_and_refresh_cache (struct replay_opts *opts) {static struct lock_file index_lock; -int index_fd =hold_locked_index (&index_lock,0 ); +hold_locked_index (&index_lock,0 );if (read_index_preload (&the_index,NULL ) <0 )die (_ ("git %s: failed to read the index" ),action_name (opts));refresh_index (&the_index,REFRESH_QUIET |REFRESH_UNMERGED ,NULL ,NULL ,NULL );if (the_index.cache_changed && index_fd >=0 ) { -if (write_index (&the_index, index_fd) || -commit_locked_index (&index_lock)) +if (write_locked_index (&the_index, &index_lock,COMMIT_LOCK ))die (_ ("git %s: failed to refresh the index" ),action_name (opts)); }rollback_lock_file (&index_lock);
test-scrap-cache-tree.c +2 -3
@@ -6,12 +6,11 @@ static struct lock_file index_lock;int main (int ac,char **av) { -int fd =hold_locked_index (&index_lock,1 ); +hold_locked_index (&index_lock,1 );if (read_cache () <0 )die ("unable to read index file" ); active_cache_tree =NULL ; -if (write_cache (fd, active_cache, active_nr) - ||commit_lock_file (&index_lock)) +if (write_locked_index (&the_index, &index_lock,COMMIT_LOCK ))die ("unable to write index file" );return 0 ; }
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 5/32 read-cache: relocate and unexport commit_locked_index()
In reply to Nguyễn Thái Ngọc DuyThis function is now only used by write_locked_index(). Move it to read-cache.c (because read-cache.c will need to be aware of alternate_index_output later) and unexport it.
--- cache.h | 1 - lockfile.c | 20 -------------------- read-cache.c | 20 ++++++++++++++++++++ 3 files changed, 20 insertions(+), 21 deletions(-)
cache.h +0 -1
@@ -552,7 +552,6 @@ extern int commit_lock_file(struct lock_file *);extern void update_index_if_able (struct index_state *,struct lock_file *);extern int hold_locked_index (struct lock_file *,int ); -extern int commit_locked_index (struct lock_file *);extern void set_alternate_index_output (const char *);extern int close_lock_file (struct lock_file *);extern void rollback_lock_file (struct lock_file *);
lockfile.c +0 -20
@@ -5,7 +5,6 @@#include "sigchain.h" static struct lock_file *lock_file_list; -static const char *alternate_index_output;static void remove_lock_file (void ) {
@@ -252,25 +251,6 @@ int hold_locked_index(struct lock_file *lk, int die_on_error) : 0); } -void set_alternate_index_output (const char *name) -{ - alternate_index_output = name; -} - -int commit_locked_index (struct lock_file *lk) -{ -if (alternate_index_output) { -if (lk->fd >=0 &&close_lock_file (lk)) -return -1 ; -if (rename (lk->filename, alternate_index_output)) -return -1 ; - lk->filename[0 ] =0 ; -return 0 ; - } -else -return commit_lock_file (lk); -} -void rollback_lock_file (struct lock_file *lk) {if (lk->filename[0 ]) {
read-cache.c +20 -0
@@ -36,6 +36,7 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,#define CACHE_EXT_RESOLVE_UNDO 0x52455543/* "REUC" */ struct index_state the_index; +static const char *alternate_index_output;static void set_index_entry (struct index_state *istate,int nr,struct cache_entry *ce) {
@@ -1875,6 +1876,25 @@ static int do_write_index(struct index_state *istate, int newfd)return 0 ; } +void set_alternate_index_output (const char *name) +{ + alternate_index_output = name; +} + +static int commit_locked_index (struct lock_file *lk) +{ +if (alternate_index_output) { +if (lk->fd >=0 &&close_lock_file (lk)) +return -1 ; +if (rename (lk->filename, alternate_index_output)) +return -1 ; + lk->filename[0 ] =0 ; +return 0 ; + } +else +return commit_lock_file (lk); +} +static int do_write_locked_index (struct index_state *istate,struct lock_file *lock,unsigned flags) {
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 6/32 read-cache: store in-memory flags in the first 12 bits of ce_flags
In reply to Nguyễn Thái Ngọc DuyWe're running out of room for in-memory flags. But since b60e188 (Strip namelen out of ce_flags into a ce_namelen field - 2012-07-11), we copy the namelen (first 12 bits) to ce_namelen field. So those bits are free to use. Just make sure we do not accidentally write any in-memory flags back.
--- cache.h | 2 +- read-cache.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
cache.h +1 -1
@@ -145,7 +145,7 @@ struct cache_entry {#define CE_STAGESHIFT 12/* -* Range 0xFFFF0000 in ce_flags is divided into +* Range 0xFFFF0FFF in ce_flags is divided into * two parts: in-memory flags and on-disk ones. * Flags in CE_EXTENDED_FLAGS will get saved on-disk * if you want to save a new flag, add it in
read-cache.c +1 -1
@@ -1702,7 +1702,7 @@ static char *copy_cache_entry_to_ondisk(struct ondisk_cache_entry *ondisk, ondisk->size =htonl (ce->ce_stat_data.sd_size);hashcpy (ondisk->sha1, ce->sha1); - flags = ce->ce_flags; + flags = ce->ce_flags & ~CE_NAMEMASK ; flags |= (ce_namelen (ce) >=CE_NAMEMASK ?CE_NAMEMASK :ce_namelen (ce)); ondisk->flags =htons (flags);if (ce->ce_flags &CE_EXTENDED ) {
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 7/32 read-cache: be strict about "changed" in remove_marked_cache_entries()
In reply to Nguyễn Thái Ngọc Duyremove_marked_cache_entries() deletes entries marked with CE_REMOVE. But if there is no such entry, do not mark the index as "changed" because that could trigger an index update unnecessarily.
--- read-cache.c | 2 ++ 1 file changed, 2 insertions(+)
read-cache.c +2 -0
@@ -510,6 +510,8 @@ void remove_marked_cache_entries(struct index_state *istate)else ce_array[j++] = ce_array[i]; } +if (j == istate->cache_nr) +return ; istate->cache_changed =1 ; istate->cache_nr = j; }
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 8/32 read-cache: be specific what part of the index has changed
In reply to Nguyễn Thái Ngọc Duy--- cache.h | 4 ++++ read-cache.c | 11 ++++++----- 2 files changed, 10 insertions(+), 5 deletions(-)
cache.h +4 -0
@@ -268,6 +268,10 @@ static inline unsigned int canon_mode(unsigned int mode)#define cache_entry_size (len) (offsetof(struct cache_entry,name) + (len) + 1) +#define CE_ENTRY_CHANGED (1 << 0) +#define CE_ENTRY_REMOVED (1 << 1) +#define CE_ENTRY_ADDED (1 << 2) +struct index_state {struct cache_entry **cache;unsigned int version;
read-cache.c +6 -5
@@ -51,7 +51,7 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cacheremove_name_hash (istate, old);free (old);set_index_entry (istate, nr, ce); - istate->cache_changed =1 ; + istate->cache_changed |=CE_ENTRY_CHANGED ; }void rename_index_entry_at (struct index_state *istate,int nr,const char *new_name)
@@ -482,7 +482,7 @@ int remove_index_entry_at(struct index_state *istate, int pos)record_resolve_undo (istate, ce);remove_name_hash (istate, ce);free (ce); - istate->cache_changed =1 ; + istate->cache_changed |=CE_ENTRY_REMOVED ; istate->cache_nr--;if (pos >= istate->cache_nr)return 0 ;
@@ -512,7 +512,7 @@ void remove_marked_cache_entries(struct index_state *istate) }if (j == istate->cache_nr)return ; - istate->cache_changed =1 ; + istate->cache_changed |=CE_ENTRY_REMOVED ; istate->cache_nr = j; }
@@ -1002,7 +1002,7 @@ int add_index_entry(struct index_state *istate, struct cache_entry *ce, int opti istate->cache + pos, (istate->cache_nr - pos - 1) * sizeof(ce));set_index_entry (istate, pos, ce); - istate->cache_changed =1 ; + istate->cache_changed |=CE_ENTRY_ADDED ;return 0 ; }
@@ -1101,6 +1101,7 @@ static struct cache_entry *refresh_cache_ent(struct index_state *istate, !(ce->ce_flags & CE_VALID)) updated->ce_flags &= ~CE_VALID ; +/* istate->cache_changed is updated in the caller */ return updated; }
@@ -1182,7 +1183,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,* means the index is not valid anymore. */ ce->ce_flags &= ~CE_VALID ; - istate->cache_changed =1 ; + istate->cache_changed |=CE_ENTRY_CHANGED ; }if (quiet)continue ;
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 9/32 update-index: be specific what part of the index has changed
In reply to Nguyễn Thái Ngọc Duy--- builtin/update-index.c | 6 +++--- cache.h | 1 + 2 files changed, 4 insertions(+), 3 deletions(-)
builtin/update-index.c +3 -3
@@ -56,7 +56,7 @@ static int mark_ce_flags(const char *path, int flag, int mark)else active_cache[pos]->ce_flags &= ~flag;cache_tree_invalidate_path (active_cache_tree, path); - active_cache_changed =1 ; + active_cache_changed |=CE_ENTRY_CHANGED ;return 0 ; }return -1 ;
@@ -268,7 +268,7 @@ static void chmod_path(int flip, const char *path) goto fail; }cache_tree_invalidate_path (active_cache_tree, path); - active_cache_changed =1 ; + active_cache_changed |=CE_ENTRY_CHANGED ;report ("chmod %cx '%s'" , flip, path);return ; fail:
@@ -889,7 +889,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) INDEX_FORMAT_LB, INDEX_FORMAT_UB);if (the_index.version != preferred_index_format) - active_cache_changed =1 ; + active_cache_changed |=SOMETHING_CHANGED ; the_index.version = preferred_index_format; }
cache.h +1 -0
@@ -271,6 +271,7 @@ static inline unsigned int canon_mode(unsigned int mode)#define CE_ENTRY_CHANGED (1 << 0)#define CE_ENTRY_REMOVED (1 << 1)#define CE_ENTRY_ADDED (1 << 2) +#define SOMETHING_CHANGED (1 << 3)/* unclassified changes go here */ struct index_state {struct cache_entry **cache;
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 10/32 resolve-undo: be specific what part of the index has changed
In reply to Nguyễn Thái Ngọc Duy--- cache.h | 1 + resolve-undo.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
cache.h +1 -0
@@ -272,6 +272,7 @@ static inline unsigned int canon_mode(unsigned int mode)#define CE_ENTRY_REMOVED (1 << 1)#define CE_ENTRY_ADDED (1 << 2)#define SOMETHING_CHANGED (1 << 3)/* unclassified changes go here */ +#define RESOLVE_UNDO_CHANGED (1 << 4)struct index_state {struct cache_entry **cache;
resolve-undo.c +1 -1
@@ -110,7 +110,7 @@ void resolve_undo_clear_index(struct index_state *istate)string_list_clear (resolve_undo,1 );free (resolve_undo); istate->resolve_undo =NULL ; - istate->cache_changed =1 ; + istate->cache_changed |=RESOLVE_UNDO_CHANGED ; }int unmerge_index_entry_at (struct index_state *istate,int pos)
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 11/32 unpack-trees: be specific what part of the index has changed
In reply to Nguyễn Thái Ngọc Duy--- unpack-trees.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
unpack-trees.c +6 -2
@@ -246,7 +246,9 @@ static int verify_absent_sparse(const struct cache_entry *ce, enum unpack_trees_error_types, struct unpack_trees_options *o); -static int apply_sparse_checkout (struct cache_entry *ce,struct unpack_trees_options *o) +static int apply_sparse_checkout (struct index_state *istate, +struct cache_entry *ce, +struct unpack_trees_options *o) {int was_skip_worktree =ce_skip_worktree (ce);
@@ -254,6 +256,8 @@ static int apply_sparse_checkout(struct cache_entry *ce, struct unpack_trees_opt ce->ce_flags |=CE_SKIP_WORKTREE ; else ce->ce_flags &= ~CE_SKIP_WORKTREE; +if (was_skip_worktree !=ce_skip_worktree (ce)) + istate->cache_changed |=CE_ENTRY_CHANGED ;/* * if (!was_skip_worktree && !ce_skip_worktree()) {
@@ -1131,7 +1135,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options ret =-1 ; } -if (apply_sparse_checkout (ce, o)) { +if (apply_sparse_checkout (&o->result, ce, o)) {if (!o->show_all_errors) goto return_failed; ret =-1 ;
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 12/32 cache-tree: mark istate->cache_changed on cache tree invalidation
In reply to Nguyễn Thái Ngọc Duy--- builtin/blame.c | 2 +- builtin/update-index.c | 4 ++-- cache-tree.c | 15 +++++++++++---- cache-tree.h | 2 +- cache.h | 1 + read-cache.c | 6 +++--- unpack-trees.c | 2 +- 7 files changed, 20 insertions(+), 12 deletions(-)
builtin/blame.c +1 -1
@@ -2126,7 +2126,7 @@ static struct commit *fake_working_tree_commit(struct diff_options *opt,* right now, but someday we might optimize diff-index --cached * with cache-tree information. */ -cache_tree_invalidate_path (active_cache_tree, path); +cache_tree_invalidate_path (&the_index, path);return commit; }
builtin/update-index.c +2 -2
@@ -55,7 +55,7 @@ static int mark_ce_flags(const char *path, int flag, int mark) active_cache[pos]->ce_flags |= flag; else active_cache[pos]->ce_flags &= ~flag; -cache_tree_invalidate_path (active_cache_tree, path); +cache_tree_invalidate_path (&the_index, path); active_cache_changed |=CE_ENTRY_CHANGED ;return 0 ; }
@@ -267,7 +267,7 @@ static void chmod_path(int flip, const char *path)default : goto fail; } -cache_tree_invalidate_path (active_cache_tree, path); +cache_tree_invalidate_path (&the_index, path); active_cache_changed |=CE_ENTRY_CHANGED ;report ("chmod %cx '%s'" , flip, path);return ;
cache-tree.c +11 -4
@@ -98,7 +98,7 @@ struct cache_tree_sub *cache_tree_sub(struct cache_tree *it, const char *path)return find_subtree (it, path, pathlen,1 ); } -void cache_tree_invalidate_path (struct cache_tree *it,const char *path) +static int do_invalidate_path (struct cache_tree *it,const char *path) {/* a/b/c * ==> invalidate self
@@ -116,7 +116,7 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path)#endif if (!it) -return ; +return 0 ; slash =strchrnul (path,'/' ); namelen = slash - path; it->entry_count =-1 ;
@@ -137,11 +137,18 @@ void cache_tree_invalidate_path(struct cache_tree *it, const char *path) (it->subtree_nr - pos - 1)); it->subtree_nr--; } -return ; +return 1 ; } down =find_subtree (it, path, namelen,0 );if (down) -cache_tree_invalidate_path (down->cache_tree, slash +1 ); +do_invalidate_path (down->cache_tree, slash +1 ); +return 1 ; +} + +void cache_tree_invalidate_path (struct index_state *istate,const char *path) +{ +if (do_invalidate_path (istate->cache_tree, path)) + istate->cache_changed |=CACHE_TREE_CHANGED ; } static int verify_cache(const struct cache_entry * const *cache,
cache-tree.h +1 -1
@@ -23,7 +23,7 @@ struct cache_tree {struct cache_tree *cache_tree (void );void cache_tree_free (struct cache_tree **); -void cache_tree_invalidate_path (struct cache_tree *,const char *); +void cache_tree_invalidate_path (struct index_state *,const char *);struct cache_tree_sub *cache_tree_sub (struct cache_tree *,const char *);void cache_tree_write (struct strbuf *,struct cache_tree *root);
cache.h +1 -0
@@ -273,6 +273,7 @@ static inline unsigned int canon_mode(unsigned int mode)#define CE_ENTRY_ADDED (1 << 2)#define SOMETHING_CHANGED (1 << 3)/* unclassified changes go here */ #define RESOLVE_UNDO_CHANGED (1 << 4) +#define CACHE_TREE_CHANGED (1 << 5)struct index_state {struct cache_entry **cache;
read-cache.c +3 -3
@@ -65,7 +65,7 @@ void rename_index_entry_at(struct index_state *istate, int nr, const char *new_n new->ce_namelen = namelen;memcpy (new->name, new_name, namelen +1 ); -cache_tree_invalidate_path (istate->cache_tree, old->name); +cache_tree_invalidate_path (istate, old->name);remove_index_entry_at (istate, nr);add_index_entry (istate, new,ADD_CACHE_OK_TO_ADD |ADD_CACHE_OK_TO_REPLACE ); }
@@ -521,7 +521,7 @@ int remove_file_from_index(struct index_state *istate, const char *path)int pos =index_name_pos (istate, path,strlen (path));if (pos <0 ) pos = -pos-1 ; -cache_tree_invalidate_path (istate->cache_tree, path); +cache_tree_invalidate_path (istate, path);while (pos < istate->cache_nr && !strcmp (istate->cache[pos]->name, path))remove_index_entry_at (istate, pos);return 0 ;
@@ -939,7 +939,7 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_eint skip_df_check = option &ADD_CACHE_SKIP_DFCHECK ;int new_only = option &ADD_CACHE_NEW_ONLY ; -cache_tree_invalidate_path (istate->cache_tree, ce->name); +cache_tree_invalidate_path (istate, ce->name); pos =index_name_stage_pos (istate, ce->name,ce_namelen (ce),ce_stage (ce));/* existing match? Just replace it. */
unpack-trees.c +1 -1
@@ -1263,7 +1263,7 @@ static void invalidate_ce_path(const struct cache_entry *ce, struct unpack_trees_options *o) {if (ce) -cache_tree_invalidate_path (o->src_index->cache_tree, ce->name); +cache_tree_invalidate_path (o->src_index, ce->name); }/*
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 13/32 cache-tree: mark istate->cache_changed on cache tree update
In reply to Nguyễn Thái Ngọc Duy--- cache-tree.c | 25 +++++++++++-------------- cache-tree.h | 2 +- merge-recursive.c | 4 +--- sequencer.c | 4 +--- test-dump-cache-tree.c | 7 ++++--- 5 files changed, 18 insertions(+), 24 deletions(-)
cache-tree.c +11 -14
@@ -151,7 +151,7 @@ void cache_tree_invalidate_path(struct index_state *istate, const char *path) istate->cache_changed |=CACHE_TREE_CHANGED ; } -static int verify_cache (const struct cache_entry *const *cache, +static int verify_cache (struct cache_entry **cache,int entries,int flags) {int i, funny;
@@ -236,7 +236,7 @@ int cache_tree_fully_valid(struct cache_tree *it) } static int update_one(struct cache_tree *it, - const struct cache_entry * const *cache, + struct cache_entry **cache, int entries, const char *base, int baselen,
@@ -398,18 +398,19 @@ static int update_one(struct cache_tree *it,return i; } -int cache_tree_update (struct cache_tree *it, -const struct cache_entry *const *cache, -int entries, -int flags) +int cache_tree_update (struct index_state *istate,int flags) { -int i, skip; - i =verify_cache (cache, entries, flags); +struct cache_tree *it = istate->cache_tree; +struct cache_entry **cache = istate->cache; +int entries = istate->cache_nr; +int skip, i =verify_cache (cache, entries, flags); +if (i)return i; i =update_one (it, cache, entries,"" ,0 , &skip, flags);if (i <0 )return i; + istate->cache_changed |=CACHE_TREE_CHANGED ;return 0 ; }
@@ -597,9 +598,7 @@ int write_cache_as_tree(unsigned char *sha1, int flags, const char *prefix) was_valid =cache_tree_fully_valid (active_cache_tree);if (!was_valid) { -if (cache_tree_update (active_cache_tree, - (const struct cache_entry *const *)active_cache, - active_nr, flags) <0 ) +if (cache_tree_update (&the_index, flags) <0 )return WRITE_TREE_UNMERGED_INDEX ;if (0 <= newfd) {if (!write_locked_index (&the_index, lock_file,COMMIT_LOCK ))
@@ -698,7 +697,5 @@ int update_main_cache_tree(int flags) {if (!the_index.cache_tree) the_index.cache_tree =cache_tree (); -return cache_tree_update (the_index.cache_tree, - (const struct cache_entry *const *)the_index.cache, - the_index.cache_nr, flags); +return cache_tree_update (&the_index, flags); }
cache-tree.h +1 -1
@@ -30,7 +30,7 @@ void cache_tree_write(struct strbuf *, struct cache_tree *root);struct cache_tree *cache_tree_read (const char *buffer,unsigned long size);int cache_tree_fully_valid (struct cache_tree *); -int cache_tree_update (struct cache_tree *,const struct cache_entry *const *,int ,int ); +int cache_tree_update (struct index_state *,int );int update_main_cache_tree (int );
merge-recursive.c +1 -3
@@ -265,9 +265,7 @@ struct tree *write_tree_from_memory(struct merge_options *o) active_cache_tree =cache_tree ();if (!cache_tree_fully_valid (active_cache_tree) && -cache_tree_update (active_cache_tree, - (const struct cache_entry *const *)active_cache, - active_nr,0 ) <0 ) +cache_tree_update (&the_index,0 ) <0 )die (_ ("error building trees" )); result =lookup_tree (active_cache_tree->sha1);
sequencer.c +1 -3
@@ -371,9 +371,7 @@ static int is_index_unchanged(void) active_cache_tree =cache_tree ();if (!cache_tree_fully_valid (active_cache_tree)) -if (cache_tree_update (active_cache_tree, - (const struct cache_entry *const *)active_cache, - active_nr,0 )) +if (cache_tree_update (&the_index,0 ))return error (_ ("Unable to update cache tree\n" ));return !hashcmp (active_cache_tree->sha1, head_commit->tree->object.sha1);
test-dump-cache-tree.c +4 -3
@@ -56,11 +56,12 @@ static int dump_cache_tree(struct cache_tree *it,int main (int ac,char **av) { +struct index_state istate;struct cache_tree *another =cache_tree ();if (read_cache () <0 )die ("unable to read index file" ); -cache_tree_update (another, - (const struct cache_entry *const *)active_cache, - active_nr,WRITE_TREE_DRY_RUN ); + istate = the_index; + istate.cache_tree = another; +cache_tree_update (&istate,WRITE_TREE_DRY_RUN );return dump_cache_tree (active_cache_tree, another,"" ); }
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 14/32 cache-tree: mark istate->cache_changed on prime_cache_tree()
In reply to Nguyễn Thái Ngọc Duy--- builtin/read-tree.c | 2 +- builtin/reset.c | 2 +- cache-tree.c | 9 +++++---- cache-tree.h | 2 +- 4 files changed, 8 insertions(+), 7 deletions(-)
builtin/read-tree.c +1 -1
@@ -231,7 +231,7 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)* what came from the tree. */ if (nr_trees ==1 && !opts.prefix) -prime_cache_tree (&active_cache_tree, trees[0 ]); +prime_cache_tree (&the_index, trees[0 ]);if (write_locked_index (&the_index, &lock_file,COMMIT_LOCK ))die ("unable to write new index file" );
builtin/reset.c +1 -1
@@ -84,7 +84,7 @@ static int reset_index(const unsigned char *sha1, int reset_type, int quiet)if (reset_type ==MIXED || reset_type ==HARD ) { tree =parse_tree_indirect (sha1); -prime_cache_tree (&active_cache_tree, tree); +prime_cache_tree (&the_index, tree); }return 0 ;
cache-tree.c +5 -4
@@ -654,11 +654,12 @@ static void prime_cache_tree_rec(struct cache_tree *it, struct tree *tree) it->entry_count = cnt; } -void prime_cache_tree (struct cache_tree **it,struct tree *tree) +void prime_cache_tree (struct index_state *istate,struct tree *tree) { -cache_tree_free (it); - *it =cache_tree (); -prime_cache_tree_rec (*it, tree); +cache_tree_free (&istate->cache_tree); + istate->cache_tree =cache_tree (); +prime_cache_tree_rec (istate->cache_tree, tree); + istate->cache_changed |=CACHE_TREE_CHANGED ; }/*
cache-tree.h +1 -1
@@ -46,7 +46,7 @@ int update_main_cache_tree(int);#define WRITE_TREE_PREFIX_ERROR (-3)int write_cache_as_tree (unsigned char *sha1,int flags,const char *prefix); -void prime_cache_tree (struct cache_tree **,struct tree *); +void prime_cache_tree (struct index_state *,struct tree *);extern int cache_tree_matches_traversal (struct cache_tree *,struct name_entry *ent,struct traverse_info *info);
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 15/32 entry.c: update cache_changed if refresh_cache is set in checkout_entry()
In reply to Nguyễn Thái Ngọc DuyOther fill_stat_cache_info() is on new entries, which should set CE_ENTRY_ADDED in cache_changed, so we're safe.
--- builtin/apply.c | 8 +++++--- builtin/checkout-index.c | 1 + builtin/checkout.c | 1 + cache.h | 1 + entry.c | 2 ++ unpack-trees.c | 1 + 6 files changed, 11 insertions(+), 3 deletions(-)
builtin/apply.c +5 -3
@@ -3084,13 +3084,15 @@ static void prepare_fn_table(struct patch *patch) } } -static int checkout_target (struct cache_entry *ce,struct stat *st) +static int checkout_target (struct index_state *istate, +struct cache_entry *ce,struct stat *st) {struct checkout costate;memset (&costate,0 ,sizeof (costate)); costate.base_dir ="" ; costate.refresh_cache =1 ; + costate.istate = istate;if (checkout_entry (ce, &costate,NULL ) ||lstat (ce->name, st))return error (_ ("cannot checkout %s" ), ce->name);return 0 ;
@@ -3257,7 +3259,7 @@ static int load_current(struct image *image, struct patch *patch)if (lstat (name, &st)) {if (errno !=ENOENT )return error (_ ("%s: %s" ), name,strerror (errno)); -if (checkout_target (ce, &st)) +if (checkout_target (&the_index, ce, &st))return -1 ; }if (verify_index_match (ce, &st))
@@ -3411,7 +3413,7 @@ static int check_preimage(struct patch *patch, struct cache_entry **ce, struct s } *ce = active_cache[pos];if (stat_ret <0 ) { -if (checkout_target (*ce, st)) +if (checkout_target (&the_index, *ce, st))return -1 ; }if (!cached &&verify_index_match (*ce, st))
builtin/checkout-index.c +1 -0
@@ -135,6 +135,7 @@ static int option_parse_u(const struct option *opt,int *newfd = opt->value; state.refresh_cache =1 ; + state.istate = &the_index;if (*newfd <0 ) *newfd =hold_locked_index (&lock_file,1 );return 0 ;
builtin/checkout.c +1 -0
@@ -336,6 +336,7 @@ static int checkout_paths(const struct checkout_opts *opts,memset (&state,0 ,sizeof (state)); state.force =1 ; state.refresh_cache =1 ; + state.istate = &the_index;for (pos =0 ; pos < active_nr; pos++) {struct cache_entry *ce = active_cache[pos];if (ce->ce_flags &CE_MATCHED ) {
cache.h +1 -0
@@ -1063,6 +1063,7 @@ extern int split_ident_line(struct ident_split *, const char *, int);extern int ident_cmp (const struct ident_split *,const struct ident_split *);struct checkout { +struct index_state *istate;const char *base_dir;int base_dir_len; unsigned force:1,
entry.c +2 -0
@@ -210,9 +210,11 @@ static int write_entry(struct cache_entry *ce, finish:if (state->refresh_cache) { +assert (state->istate);if (!fstat_done)lstat (ce->name, &st);fill_stat_cache_info (ce, &st); + state->istate->cache_changed |=CE_ENTRY_CHANGED ; }return 0 ; }
unpack-trees.c +1 -0
@@ -1029,6 +1029,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options state.force =1 ; state.quiet =1 ; state.refresh_cache =1 ; + state.istate = &o->result;memset (&el,0 ,sizeof (el));if (!core_apply_sparse_checkout || !o->update)
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 16/32 read-cache: save index SHA-1 after reading
In reply to Nguyễn Thái Ngọc DuyAlso update SHA-1 after writing. If we do not do that, the second read_index() will see "initialized" variable already set and not read .git/index again, which is fine, except istate->sha1 now has a stale value.
--- cache.h | 1 + read-cache.c | 6 ++++-- unpack-trees.c | 1 + 3 files changed, 6 insertions(+), 2 deletions(-)
cache.h +1 -0
@@ -286,6 +286,7 @@ struct index_state { initialized :1 ;struct hashmap name_hash;struct hashmap dir_hash; +unsigned char sha1[20 ]; };extern struct index_state the_index;
read-cache.c +4 -2
@@ -1481,6 +1481,7 @@ int read_index_from(struct index_state *istate, const char *path)if (verify_hdr (hdr, mmap_size) <0 ) goto unmap; +hashcpy (istate->sha1, (const unsigned char *)hdr + mmap_size -20 ); istate->version =ntohl (hdr->hdr_version); istate->cache_nr =ntohl (hdr->hdr_entries); istate->cache_alloc =alloc_nr (istate->cache_nr);
@@ -1616,7 +1617,7 @@ static int write_index_ext_header(git_SHA_CTX *context, int fd, (ce_write(context, fd, &sz, 4) < 0)) ? -1 : 0; } -static int ce_flush (git_SHA_CTX *context,int fd) +static int ce_flush (git_SHA_CTX *context,int fd,unsigned char *sha1) {unsigned int left = write_buffer_len;
@@ -1634,6 +1635,7 @@ static int ce_flush(git_SHA_CTX *context, int fd)/* Append the SHA1 signature at the end */ git_SHA1_Final (write_buffer + left, context); +hashcpy (sha1, write_buffer + left); left +=20 ;return (write_in_full (fd, write_buffer, left) != left) ?-1 :0 ; }
@@ -1872,7 +1874,7 @@ static int do_write_index(struct index_state *istate, int newfd)return -1 ; } -if (ce_flush (&c, newfd) ||fstat (newfd, &st)) +if (ce_flush (&c, newfd, istate->sha1) ||fstat (newfd, &st))return -1 ; istate->timestamp.sec = (unsigned int )st.st_mtime; istate->timestamp.nsec =ST_MTIME_NSEC (st);
unpack-trees.c +1 -0
@@ -1046,6 +1046,7 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options o->result.timestamp.sec = o->src_index->timestamp.sec; o->result.timestamp.nsec = o->src_index->timestamp.nsec; o->result.version = o->src_index->version; +hashcpy (o->result.sha1, o->src_index->sha1); o->merge_size = len;mark_all_ce_unused (o->src_index);
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 17/32 read-cache: split-index mode
In reply to Nguyễn Thái Ngọc DuyThis split-index mode is designed to keep write cost proportional to the number of changes the user has made, not the size of the work tree. (Read cost is another matter, to be dealt separately.)
This mode stores index info in a pair of $GIT_DIR/index and $GIT_DIR/sharedindex.<SHA-1>. sharedindex is large and unchanged over time while "index" is smaller and updated often. Format details are in index-format.txt, although not everything is implemented in this patch.
Shared indexes are not automatically removed, because it's unclear if the shared index is needed by any (even temporary) indexes by just looking at it. After a while you'll collect stale shared indexes. The good news is one shared index is useable for long, until $GIT_DIR/index becomes too big and sluggish that the new shared index must be created.
The safest way to clean shared indexes is to turn off split index mode, so shared files are all garbage, delete them all, then turn on split index mode again.
--- Documentation/gitrepository-layout.txt | 4 ++ Documentation/technical/index-format.txt | 35 ++++++++++++ Makefile | 1 + cache.h | 3 + read-cache.c | 96 ++++++++++++++++++++++++++++++-- split-index.c (new) | 90 ++++++++++++++++++++++++++++++ split-index.h (new) | 25 +++++++++ unpack-trees.c | 4 ++ 8 files changed, 253 insertions(+), 5 deletions(-) create mode 100644 split-index.c create mode 100644 split-index.h
Documentation/gitrepository-layout.txt +4 -0
@@ -155,6 +155,10 @@ index:: The current index file for the repository. It is usually not found in a bare repository. +sharedindex.<SHA-1>:: + The shared index part, to be referenced by $GIT_DIR/index and + other temporary index files. Only valid in split index mode. + info:: Additional information about the repository is recorded in this directory.
Documentation/technical/index-format.txt +35 -0
@@ -129,6 +129,9 @@ Git index format (Version 4) In version 4, the padding after the pathname does not exist. + Interpretation of index entries in split index mode is completely + different. See below for details. + == Extensions === Cached tree
@@ -198,3 +201,35 @@ Git index format - At most three 160-bit object names of the entry in stages from 1 to 3 (nothing is written for a missing stage). +=== Split index + + In split index mode, the majority of index entries could be stored + in a separate file. This extension records the changes to be made on + top of that to produce the final index. + + The signature for this extension is { 'l', 'i, 'n', 'k' }. + + The extension consists of: + + - 160-bit SHA-1 of the shared index file. The shared index file path + is $GIT_DIR/sharedindex.<SHA-1>. If all 160 bits are zero, the + index does not require a shared index file. + + - An ewah-encoded delete bitmap, each bit represents an entry in the + shared index. If a bit is set, its corresponding entry in the + shared index will be removed from the final index. Note, because + a delete operation changes index entry positions, but we do need + original positions in replace phase, it's best to just mark + entries for removal, then do a mass deletion after replacement. + + - An ewah-encoded replace bitmap, each bit represents an entry in + the shared index. If a bit is set, its corresponding entry in the + shared index will be replaced with an entry in this index + file. All replaced entries are stored in sorted order in this + index. The first "1" bit in the replace bitmap corresponds to the + first index entry, the second "1" bit to the second entry and so + on. Replaced entries may have empty path names to save space. + + The remaining index entries after replaced ones will be added to the + final index. These added entries are also sorted by entry namme then + stage.
Makefile +1 -0
@@ -884,6 +884,7 @@ LIB_OBJS += sha1_name.o LIB_OBJS += shallow.o LIB_OBJS += sideband.o LIB_OBJS += sigchain.o +LIB_OBJS += split-index.o LIB_OBJS += strbuf.o LIB_OBJS += streaming.o LIB_OBJS += string-list.o
cache.h +3 -0
@@ -135,6 +135,7 @@ struct cache_entry {unsigned int ce_mode;unsigned int ce_flags;unsigned int ce_namelen; +unsigned int index;/* for link extension */ unsigned char sha1[20 ];char name[FLEX_ARRAY ];/* more */ };
@@ -275,12 +276,14 @@ static inline unsigned int canon_mode(unsigned int mode)#define RESOLVE_UNDO_CHANGED (1 << 4)#define CACHE_TREE_CHANGED (1 << 5) +struct split_index ;struct index_state {struct cache_entry **cache;unsigned int version;unsigned int cache_nr, cache_alloc, cache_changed;struct string_list *resolve_undo;struct cache_tree *cache_tree; +struct split_index *split_index;struct cache_time timestamp;unsigned name_hash_initialized :1 , initialized :1 ;
read-cache.c +91 -5
173 lines not on this page · raw message
split-index.c new file +90 -0
90 lines not on this page · raw message
split-index.h new file +25 -0
@@ -0,0 +1,25 @@ +#ifndef SPLIT_INDEX_H +#define SPLIT_INDEX_H + +struct index_state ; +struct strbuf ; + +struct split_index { +unsigned char base_sha1[20 ]; +struct index_state *base; +unsigned int saved_cache_nr; +int refcount; +}; + +struct split_index *init_split_index (struct index_state *istate); +int read_link_extension (struct index_state *istate, +const void *data,unsigned long sz); +int write_link_extension (struct strbuf *sb, +struct index_state *istate); +void move_cache_to_base_index (struct index_state *istate); +void merge_base_index (struct index_state *istate); +void prepare_to_write_split_index (struct index_state *istate); +void finish_writing_split_index (struct index_state *istate); +void discard_split_index (struct index_state *istate); + +#endif
unpack-trees.c +4 -0
@@ -8,6 +8,7 @@#include "progress.h" #include "refs.h" #include "attr.h" +#include "split-index.h" /* * Error messages expected by scripts out of plumbing commands such as
@@ -1046,6 +1047,9 @@ int unpack_trees(unsigned len, struct tree_desc *t, struct unpack_trees_options o->result.timestamp.sec = o->src_index->timestamp.sec; o->result.timestamp.nsec = o->src_index->timestamp.nsec; o->result.version = o->src_index->version; + o->result.split_index = o->src_index->split_index; +if (o->result.split_index) + o->result.split_index->refcount++;hashcpy (o->result.sha1, o->src_index->sha1); o->merge_size = len;mark_all_ce_unused (o->src_index);
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 18/32 read-cache: mark new entries for split index
In reply to Nguyễn Thái Ngọc DuyMake sure entry addition does not lead to unifying the index. We don't need to explicitly keep track of new entries. If ce->index is zero, they're new. Otherwise it's unlikely that they are new, but we'll do a through check later at writing time.
--- read-cache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
read-cache.c +2 -1
@@ -38,7 +38,8 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,#define CACHE_EXT_LINK 0x6c696e6b/* "link" */ /* changes that can be kept in $GIT_DIR/index (basically all extensions) */ -#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED) +#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ + CE_ENTRY_ADDED)struct index_state the_index;static const char *alternate_index_output;
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 19/32 read-cache: save deleted entries in split index
In reply to Nguyễn Thái Ngọc DuyEntries that belong to the base index should not be freed. Mark CE_REMOVE to track them.
--- read-cache.c | 14 ++++++++------ split-index.c | 12 ++++++++++++ split-index.h | 1 + 3 files changed, 21 insertions(+), 6 deletions(-)
read-cache.c +8 -6
@@ -39,7 +39,7 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,/* changes that can be kept in $GIT_DIR/index (basically all extensions) */ #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ - CE_ENTRY_ADDED) + CE_ENTRY_ADDED | CE_ENTRY_REMOVED)struct index_state the_index;static const char *alternate_index_output;
@@ -488,7 +488,7 @@ int remove_index_entry_at(struct index_state *istate, int pos)record_resolve_undo (istate, ce);remove_name_hash (istate, ce); -free (ce); +save_or_free_index_entry (istate, ce); istate->cache_changed |=CE_ENTRY_REMOVED ; istate->cache_nr--;if (pos >= istate->cache_nr)
@@ -512,7 +512,7 @@ void remove_marked_cache_entries(struct index_state *istate)for (i = j =0 ; i < istate->cache_nr; i++) {if (ce_array[i]->ce_flags &CE_REMOVE ) {remove_name_hash (istate, ce_array[i]); -free (ce_array[i]); +save_or_free_index_entry (istate, ce_array[i]); }else ce_array[j++] = ce_array[i];
@@ -577,7 +577,9 @@ static int different_name(struct cache_entry *ce, struct cache_entry *alias)* So we use the CE_ADDED flag to verify that the alias was an old * one before we accept it as */ -static struct cache_entry *create_alias_ce (struct cache_entry *ce,struct cache_entry *alias) +static struct cache_entry *create_alias_ce (struct index_state *istate, +struct cache_entry *ce, +struct cache_entry *alias) {int len;struct cache_entry *new;
@@ -590,7 +592,7 @@ static struct cache_entry *create_alias_ce(struct cache_entry *ce, struct cache_ new =xcalloc (1 ,cache_entry_size (len));memcpy (new->name, alias->name, len);copy_cache_entry (new, ce); -free (ce); +save_or_free_index_entry (istate, ce);return new; }
@@ -683,7 +685,7 @@ int add_to_index(struct index_state *istate, const char *path, struct stat *st,set_object_name_for_intent_to_add_entry (ce);if (ignore_case && alias &&different_name (ce, alias)) - ce =create_alias_ce (ce, alias); + ce =create_alias_ce (istate, ce, alias); ce->ce_flags |=CE_ADDED ;/* It was suspected to be racily clean, but it turns out to be Ok */
split-index.c +12 -0
@@ -88,3 +88,15 @@ void discard_split_index(struct index_state *istate) }free (si); } + +void save_or_free_index_entry (struct index_state *istate,struct cache_entry *ce) +{ +if (ce->index && + istate->split_index && + istate->split_index->base && + ce->index <= istate->split_index->base->cache_nr && + ce == istate->split_index->base->cache[ce->index -1 ]) + ce->ce_flags |=CE_REMOVE ; +else +free (ce); +}
split-index.h +1 -0
@@ -12,6 +12,7 @@ struct split_index { };struct split_index *init_split_index (struct index_state *istate); +void save_or_free_index_entry (struct index_state *istate,struct cache_entry *ce);int read_link_extension (struct index_state *istate,const void *data,unsigned long sz); int write_link_extension(struct strbuf *sb,
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 20/32 read-cache: mark updated entries for split index
In reply to Nguyễn Thái Ngọc DuyThe large part of this patch just follows CE_ENTRY_CHANGED marks. replace_index_entry() is updated to update split_index->base->cache[] as well so base->cache[] does not reference to a freed entry.
--- builtin/update-index.c | 2 ++ cache.h | 2 ++ entry.c | 1 + read-cache.c | 5 ++++- split-index.c | 15 +++++++++++++++ split-index.h | 3 +++ unpack-trees.c | 4 +++- 7 files changed, 30 insertions(+), 2 deletions(-)
builtin/update-index.c +2 -0
@@ -55,6 +55,7 @@ static int mark_ce_flags(const char *path, int flag, int mark) active_cache[pos]->ce_flags |= flag; else active_cache[pos]->ce_flags &= ~flag; + active_cache[pos]->ce_flags |=CE_UPDATE_IN_BASE ;cache_tree_invalidate_path (&the_index, path); active_cache_changed |=CE_ENTRY_CHANGED ;return 0 ;
@@ -268,6 +269,7 @@ static void chmod_path(int flip, const char *path) goto fail; }cache_tree_invalidate_path (&the_index, path); + ce->ce_flags |=CE_UPDATE_IN_BASE ; active_cache_changed |=CE_ENTRY_CHANGED ;report ("chmod %cx '%s'" , flip, path);return ;
cache.h +2 -0
@@ -169,6 +169,8 @@ struct cache_entry {/* used to temporarily mark paths matched by pathspecs */ #define CE_MATCHED (1 << 26) +#define CE_UPDATE_IN_BASE (1 << 27) +/* * Extended on-disk flags */
entry.c +1 -0
@@ -214,6 +214,7 @@ finish:if (!fstat_done)lstat (ce->name, &st);fill_stat_cache_info (ce, &st); + ce->ce_flags |=CE_UPDATE_IN_BASE ; state->istate->cache_changed |=CE_ENTRY_CHANGED ; }return 0 ;
read-cache.c +4 -1
@@ -39,7 +39,7 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,/* changes that can be kept in $GIT_DIR/index (basically all extensions) */ #define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ - CE_ENTRY_ADDED | CE_ENTRY_REMOVED) + CE_ENTRY_ADDED | CE_ENTRY_REMOVED | CE_ENTRY_CHANGED)struct index_state the_index;static const char *alternate_index_output;
@@ -54,9 +54,11 @@ static void replace_index_entry(struct index_state *istate, int nr, struct cache {struct cache_entry *old = istate->cache[nr]; +replace_index_entry_in_base (istate, old, ce);remove_name_hash (istate, old);free (old);set_index_entry (istate, nr, ce); + ce->ce_flags |=CE_UPDATE_IN_BASE ; istate->cache_changed |=CE_ENTRY_CHANGED ; }
@@ -1192,6 +1194,7 @@ int refresh_index(struct index_state *istate, unsigned int flags,* means the index is not valid anymore. */ ce->ce_flags &= ~CE_VALID ; + ce->ce_flags |=CE_UPDATE_IN_BASE ; istate->cache_changed |=CE_ENTRY_CHANGED ; }if (quiet)
split-index.c +15 -0
@@ -100,3 +100,18 @@ void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ceelse free (ce); } + +void replace_index_entry_in_base (struct index_state *istate, +struct cache_entry *old, +struct cache_entry *new) +{ +if (old->index && + istate->split_index && + istate->split_index->base && + old->index <= istate->split_index->base->cache_nr) { + new->index = old->index; +if (old != istate->split_index->base->cache[new->index -1 ]) +free (istate->split_index->base->cache[new->index -1 ]); + istate->split_index->base->cache[new->index -1 ] = new; + } +}
split-index.h +3 -0
@@ -13,6 +13,9 @@ struct split_index {struct split_index *init_split_index (struct index_state *istate);void save_or_free_index_entry (struct index_state *istate,struct cache_entry *ce); +void replace_index_entry_in_base (struct index_state *istate, +struct cache_entry *old, +struct cache_entry *new);int read_link_extension (struct index_state *istate,const void *data,unsigned long sz); int write_link_extension(struct strbuf *sb,
unpack-trees.c +3 -1
@@ -257,8 +257,10 @@ static int apply_sparse_checkout(struct index_state *istate, ce->ce_flags |=CE_SKIP_WORKTREE ; else ce->ce_flags &= ~CE_SKIP_WORKTREE; -if (was_skip_worktree !=ce_skip_worktree (ce)) +if (was_skip_worktree !=ce_skip_worktree (ce)) { + ce->ce_flags |=CE_UPDATE_IN_BASE ; istate->cache_changed |=CE_ENTRY_CHANGED ; + }/* * if (!was_skip_worktree && !ce_skip_worktree()) {
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 21/32 split-index: the writing part
In reply to Nguyễn Thái Ngọc Duyprepare_to_write_split_index() does the major work, classifying deleted, updated and added entries. write_link_extension() then just writes it down.
An observation is, deleting an entry, then adding it back is recorded as "entry X is deleted, entry X is added", not "entry X is replaced". This is simpler, with small overhead: a replaced entry is stored without its path, a new entry is store with its path.
A note about unpack_trees() and the deduplication code inside prepare_to_write_split_index(). Usually tracking updated/removed entries via read-cache API is enough. unpack_trees() manipulates the index in a different way: it throws the entire source index out, builds up a new one, copying/duplicating entries (using dup_entry) from the source index over if necessary, then returns the new index.
A naive solution would be marking the entire source index "deleted" and add their duplicates as new. That could bring $GIT_DIR/index back to the original size. So we try harder and memcmp() between the original and the duplicate to see if it needs updating.
We could avoid memcmp() too, by avoiding duplicating the original entry in dup_entry(). The performance gain this way is within noise level and it complicates unpack-trees.c. So memcmp() is the preferred way to deal with deduplication.
--- split-index.c | 101 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- split-index.h | 4 +++ 2 files changed, 103 insertions(+), 2 deletions(-)
split-index.c +99 -2
@@ -1,5 +1,6 @@#include "cache.h" #include "split-index.h" +#include "ewah/ewok.h" struct split_index *init_split_index (struct index_state *istate) {
@@ -26,11 +27,22 @@ int read_link_extension(struct index_state *istate,return 0 ; } +static int write_strbuf (void *user_data,const void *data,size_t len) +{ +struct strbuf *sb = user_data; +strbuf_add (sb, data, len); +return len; +} +int write_link_extension (struct strbuf *sb,struct index_state *istate) {struct split_index *si = istate->split_index;strbuf_add (sb, si->base_sha1,20 ); +if (!si->delete_bitmap && !si->replace_bitmap) +return 0 ; +ewah_serialize_to (si->delete_bitmap, write_strbuf, sb); +ewah_serialize_to (si->replace_bitmap, write_strbuf, sb);return 0 ; }
@@ -62,14 +74,99 @@ void merge_base_index(struct index_state *istate)void prepare_to_write_split_index (struct index_state *istate) {struct split_index *si =init_split_index (istate); -/* take cache[] out temporarily */ +struct cache_entry **entries =NULL , *ce; +int i, nr_entries =0 , nr_alloc =0 ; + + si->delete_bitmap =ewah_new (); + si->replace_bitmap =ewah_new (); + +if (si->base) { +/* Go through istate->cache[] and mark CE_MATCHED to +* entry with positive index. We'll go through +* base->cache[] later to delete all entries in base +* that are not marked eith either CE_MATCHED or +* CE_UPDATE_IN_BASE. If istate->cache[i] is a +* duplicate, deduplicate it. +*/ +for (i =0 ; i < istate->cache_nr; i++) { +struct cache_entry *base; +/* namelen is checked separately */ +const unsigned int ondisk_flags = +CE_STAGEMASK |CE_VALID |CE_EXTENDED_FLAGS ; +unsigned int ce_flags, base_flags, ret; + ce = istate->cache[i]; +if (!ce->index) +continue ; +if (ce->index > si->base->cache_nr) { + ce->index =0 ; +continue ; + } + ce->ce_flags |=CE_MATCHED ;/* or "shared" */ + base = si->base->cache[ce->index -1 ]; +if (ce == base) +continue ; +if (ce->ce_namelen != base->ce_namelen || +strcmp (ce->name, base->name)) { + ce->index =0 ; +continue ; + } + ce_flags = ce->ce_flags; + base_flags = base->ce_flags; +/* only on-disk flags matter */ + ce->ce_flags &= ondisk_flags; + base->ce_flags &= ondisk_flags; + ret =memcmp (&ce->ce_stat_data, &base->ce_stat_data, + offsetof(struct cache_entry , name) - + offsetof(struct cache_entry , ce_stat_data)); + ce->ce_flags = ce_flags; + base->ce_flags = base_flags; +if (ret) + ce->ce_flags |=CE_UPDATE_IN_BASE ; +free (base); + si->base->cache[ce->index -1 ] = ce; + } +for (i =0 ; i < si->base->cache_nr; i++) { + ce = si->base->cache[i]; +if ((ce->ce_flags &CE_REMOVE ) || + !(ce->ce_flags &CE_MATCHED )) +ewah_set (si->delete_bitmap, i); +else if (ce->ce_flags &CE_UPDATE_IN_BASE ) { +ewah_set (si->replace_bitmap, i); +ALLOC_GROW (entries, nr_entries+1 , nr_alloc); + entries[nr_entries++] = ce; + } + } + } + +for (i =0 ; i < istate->cache_nr; i++) { + ce = istate->cache[i]; +if ((!si->base || !ce->index) && !(ce->ce_flags &CE_REMOVE )) { +ALLOC_GROW (entries, nr_entries+1 , nr_alloc); + entries[nr_entries++] = ce; + } + ce->ce_flags &= ~CE_MATCHED ; + } + +/* +* take cache[] out temporarily, put entries[] in its place +* for writing +*/ + si->saved_cache = istate->cache; si->saved_cache_nr = istate->cache_nr; - istate->cache_nr =0 ; + istate->cache = entries; + istate->cache_nr = nr_entries; }void finish_writing_split_index (struct index_state *istate) {struct split_index *si =init_split_index (istate); + +ewah_free (si->delete_bitmap); +ewah_free (si->replace_bitmap); + si->delete_bitmap =NULL ; + si->replace_bitmap =NULL ; +free (istate->cache); + istate->cache = si->saved_cache; istate->cache_nr = si->saved_cache_nr; }
split-index.h +4 -0
@@ -3,10 +3,14 @@struct index_state ;struct strbuf ; +struct ewah_bitmap ;struct split_index {unsigned char base_sha1[20 ];struct index_state *base; +struct ewah_bitmap *delete_bitmap; +struct ewah_bitmap *replace_bitmap; +struct cache_entry **saved_cache;unsigned int saved_cache_nr;int refcount; };
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 22/32 split-index: the reading part
In reply to Nguyễn Thái Ngọc DuyCE_REMOVE'd entries are removed here because only parts of the code base (unpack_trees in fact) test this bit when they look for the presence of an entry. Leaving them may confuse the code ignores this bit and expects to see a real entry.
--- read-cache.c | 2 -- split-index.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- split-index.h | 2 ++ 3 files changed, 84 insertions(+), 4 deletions(-)
read-cache.c +0 -2
@@ -1569,8 +1569,6 @@ int read_index_from(struct index_state *istate, const char *path)if (is_null_sha1 (split_index->base_sha1))return ret; -if (istate->cache_nr) -die ("index in split-index mode must contain no entries" );if (split_index->base)discard_index (split_index->base);
split-index.c +82 -2
110 lines not on this page · raw message
split-index.h +2 -0
@@ -12,6 +12,8 @@ struct split_index {struct ewah_bitmap *replace_bitmap;struct cache_entry **saved_cache;unsigned int saved_cache_nr; +unsigned int nr_deletions; +unsigned int nr_replacements;int refcount; };
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 23/32 split-index: do not invalidate cache-tree at read time
In reply to Nguyễn Thái Ngọc DuyWe are sure that after merge_base_index() is done. cache-tree can still be used with the final index. So don't destroy cache tree.
--- cache.h | 1 + read-cache.c | 3 ++- split-index.c | 1 + 3 files changed, 4 insertions(+), 1 deletion(-)
cache.h +1 -0
@@ -488,6 +488,7 @@ extern int index_name_pos(const struct index_state *, const char *name, int name#define ADD_CACHE_SKIP_DFCHECK 4/* Ok to skip DF conflict checks */ #define ADD_CACHE_JUST_APPEND 8/* Append only; tree.c::read_tree() */ #define ADD_CACHE_NEW_ONLY 16/* Do not replace existing ones */ +#define ADD_CACHE_KEEP_CACHE_TREE 32/* Do not invalidate cache-tree */ extern int add_index_entry (struct index_state *,struct cache_entry *ce,int option);extern void rename_index_entry_at (struct index_state *,int pos,const char *new_name);extern int remove_index_entry_at (struct index_state *,int pos);
read-cache.c +2 -1
@@ -950,7 +950,8 @@ static int add_index_entry_with_check(struct index_state *istate, struct cache_eint skip_df_check = option &ADD_CACHE_SKIP_DFCHECK ;int new_only = option &ADD_CACHE_NEW_ONLY ; -cache_tree_invalidate_path (istate, ce->name); +if (!(option &ADD_CACHE_KEEP_CACHE_TREE )) +cache_tree_invalidate_path (istate, ce->name); pos =index_name_stage_pos (istate, ce->name,ce_namelen (ce),ce_stage (ce));/* existing match? Just replace it. */
split-index.c +1 -0
@@ -133,6 +133,7 @@ void merge_base_index(struct index_state *istate)for (i = si->nr_replacements; i < si->saved_cache_nr; i++) { add_index_entry(istate, si->saved_cache[i], ADD_CACHE_OK_TO_ADD | + ADD_CACHE_KEEP_CACHE_TREE |/* * we may have to replay what * merge-recursive.c:update_stages()
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 24/32 split-index: strip pathname of on-disk replaced entries
In reply to Nguyễn Thái Ngọc DuyWe know the positions of replaced entries via the replace bitmap in "link" extension, so the "name" path does not have to be stored (it's still in the shared index). With this, we also have a way to distinguish additions vs replacements at load time and can catch broken "link" extensions.
--- cache.h | 1 + read-cache.c | 10 ++++++++++ split-index.c | 14 ++++++++++++-- 3 files changed, 23 insertions(+), 2 deletions(-)
cache.h +1 -0
@@ -170,6 +170,7 @@ struct cache_entry {#define CE_MATCHED (1 << 26)#define CE_UPDATE_IN_BASE (1 << 27) +#define CE_STRIP_NAME (1 << 28)/* * Extended on-disk flags
read-cache.c +10 -0
@@ -1787,9 +1787,15 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce, {int size;struct ondisk_cache_entry *ondisk; +int saved_namelen;char *name;int result; +if (ce->ce_flags &CE_STRIP_NAME ) { + saved_namelen =ce_namelen (ce); + ce->ce_namelen =0 ; + } +if (!previous_name) { size =ondisk_ce_size (ce); ondisk =xcalloc (1 , size);
@@ -1821,6 +1827,10 @@ static int ce_write_entry(git_SHA_CTX *c, int fd, struct cache_entry *ce,strbuf_splice (previous_name, common, to_remove, ce->name + common,ce_namelen (ce) - common); } +if (ce->ce_flags &CE_STRIP_NAME ) { + ce->ce_namelen = saved_namelen; + ce->ce_flags &= ~CE_STRIP_NAME ; + } result =ce_write (c, fd, ondisk, size);free (ondisk);
split-index.c +12 -2
@@ -89,6 +89,7 @@ static void replace_entry(size_t pos, void *data)struct index_state *istate = data;struct split_index *si = istate->split_index;struct cache_entry *dst, *src; +if (pos >= istate->cache_nr)die ("position for replacement %d exceeds base index size %d" , (int )pos, istate->cache_nr);
@@ -100,10 +101,14 @@ static void replace_entry(size_t pos, void *data)die ("entry %d is marked as both replaced and deleted" , (int )pos); src = si->saved_cache[si->nr_replacements]; +if (ce_namelen (src)) +die ("corrupt link extension, entry %d should have " +"zero length name" , (int )pos); src->index = pos +1 ; src->ce_flags |=CE_UPDATE_IN_BASE ; -free (dst); - dst = src; + src->ce_namelen = dst->ce_namelen; +copy_cache_entry (dst, src); +free (src); si->nr_replacements++; }
@@ -131,6 +136,9 @@ void merge_base_index(struct index_state *istate)remove_marked_cache_entries (istate);for (i = si->nr_replacements; i < si->saved_cache_nr; i++) { +if (!ce_namelen (si->saved_cache[i])) +die ("corrupt link extension, entry %d should " +"have non-zero length name" , i); add_index_entry(istate, si->saved_cache[i], ADD_CACHE_OK_TO_ADD | ADD_CACHE_KEEP_CACHE_TREE |
@@ -213,6 +221,7 @@ void prepare_to_write_split_index(struct index_state *istate)ewah_set (si->delete_bitmap, i); else if (ce->ce_flags & CE_UPDATE_IN_BASE) {ewah_set (si->replace_bitmap, i); + ce->ce_flags |=CE_STRIP_NAME ;ALLOC_GROW (entries, nr_entries+1 , nr_alloc); entries[nr_entries++] = ce; }
@@ -222,6 +231,7 @@ void prepare_to_write_split_index(struct index_state *istate)for (i =0 ; i < istate->cache_nr; i++) { ce = istate->cache[i];if ((!si->base || !ce->index) && !(ce->ce_flags &CE_REMOVE )) { +assert (!(ce->ce_flags &CE_STRIP_NAME ));ALLOC_GROW (entries, nr_entries+1 , nr_alloc); entries[nr_entries++] = ce; }
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 25/32 update-index: new options to enable/disable split index mode
In reply to Nguyễn Thái Ngọc DuyIf you have a large work tree but only make changes in a subset, then $GIT_DIR/index's size should be stable after a while. If you change branches that touch something else, $GIT_DIR/index's size may grow large that it becomes as slow as the unified index. Do --split-index again occasionally to force all changes back to the shared index and keep $GIT_DIR/index small.
--- Documentation/git-update-index.txt | 11 +++++++ builtin/update-index.c | 18 ++++++++++ cache.h | 1 + read-cache.c | 67 ++++++++++++++++++++++++++++++++++---- split-index.c | 23 +++++++++++++ 5 files changed, 114 insertions(+), 6 deletions(-)
Documentation/git-update-index.txt +11 -0
@@ -161,6 +161,17 @@ may not support it yet. Only meaningful with `--stdin` or `--index-info`; paths are separated with NUL character instead of LF. +--split-index:: +--no-split-index:: + Enable or disable split index mode. If enabled, the index is + split into two files, $GIT_DIR/index and $GIT_DIR/sharedindex.<SHA-1>. + Changes are accumulated in $GIT_DIR/index while the shared + index file contains all index entries stays unchanged. If + split-index mode is already enabled and `--split-index` is + given again, all changes in $GIT_DIR/index are pushed back to + the shared index file. This mode is designed for very large + indexes that take a signficant amount of time to read or write. + \--:: Do not interpret any more arguments as options.
builtin/update-index.c +18 -0
@@ -13,6 +13,7 @@#include "parse-options.h" #include "pathspec.h" #include "dir.h" +#include "split-index.h" /* * Default to not allowing changes to the list of files. The
@@ -742,6 +743,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)char set_executable_bit =0 ;struct refresh_params refresh_args = {0 , &has_errors};int lock_error =0 ; +int split_index =-1 ;struct lock_file *lock_file;struct parse_opt_ctx_t ctx;int parseopt_state =PARSE_OPT_UNKNOWN ;
@@ -824,6 +826,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) resolve_undo_clear_callback}, OPT_INTEGER(0, "index-version", &preferred_index_format, N_("write index in this format")), + OPT_BOOL(0, "split-index", &split_index, + N_("enable or disable split index")),OPT_END () };
@@ -917,6 +921,20 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)strbuf_release (&buf); } +if (split_index >0 ) { +init_split_index (&the_index); + the_index.cache_changed |=SPLIT_INDEX_ORDERED ; + }else if (!split_index && the_index.split_index) { +/* +* can't discard_split_index(&the_index); because that +* will destroy split_index->base->cache[], which may +* be shared with the_index.cache[]. So yeah we're +* leaking a bit here. +*/ + the_index.split_index =NULL ; + the_index.cache_changed |=SOMETHING_CHANGED ; + } +if (active_cache_changed) {if (newfd <0 ) {if (refresh_args.flags &REFRESH_QUIET )
cache.h +1 -0
@@ -278,6 +278,7 @@ static inline unsigned int canon_mode(unsigned int mode)#define SOMETHING_CHANGED (1 << 3)/* unclassified changes go here */ #define RESOLVE_UNDO_CHANGED (1 << 4)#define CACHE_TREE_CHANGED (1 << 5) +#define SPLIT_INDEX_ORDERED (1 << 6)struct split_index ;struct index_state {
read-cache.c +61 -6
121 lines not on this page · raw message
split-index.c +23 -0
@@ -74,6 +74,29 @@ static void mark_base_index_entries(struct index_state *base) base->cache[i]->index = i +1 ; } +void move_cache_to_base_index (struct index_state *istate) +{ +struct split_index *si = istate->split_index; +int i; + +/* +* do not delete old si->base, its index entries may be shared +* with istate->cache[]. Accept a bit of leaking here because +* this code is only used by short-lived update-index. +*/ + si->base =xcalloc (1 ,sizeof (*si->base)); + si->base->version = istate->version; +/* zero timestamp disables racy test in ce_write_index() */ + si->base->timestamp = istate->timestamp; +ALLOC_GROW (si->base->cache, istate->cache_nr, si->base->cache_alloc); + si->base->cache_nr = istate->cache_nr; +memcpy (si->base->cache, istate->cache, +sizeof (*istate->cache) * istate->cache_nr); +mark_base_index_entries (si->base); +for (i =0 ; i < si->base->cache_nr; i++) + si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE ; +} +static void mark_entry_for_delete (size_t pos,void *data) {struct index_state *istate = data;
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 26/32 update-index --split-index: do not split if $GIT_DIR is read only
In reply to Nguyễn Thái Ngọc DuyIf $GIT_DIR is read only, we can't write $GIT_DIR/sharedindex. This could happen when $GIT_INDEX_FILE is set to somehwere outside $GIT_DIR.
--- read-cache.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
read-cache.c +10 -3
@@ -2030,14 +2030,21 @@ static void remove_temporary_sharedindex_on_signal(int signo)raise (signo); } -static int write_shared_index (struct index_state *istate) +static int write_shared_index (struct index_state *istate, +struct lock_file *lock,unsigned flags) {struct split_index *si = istate->split_index;static int installed_handler;int fd, ret; temporary_sharedindex =git_pathdup ("sharedindex_XXXXXX" ); - fd =xmkstemp (temporary_sharedindex); + fd =mkstemp (temporary_sharedindex); +if (fd <0 ) { +free (temporary_sharedindex); + temporary_sharedindex =NULL ; +hashclr (si->base_sha1); +return do_write_locked_index (istate, lock, flags); + }if (!installed_handler) {atexit (remove_temporary_sharedindex);sigchain_push_common (remove_temporary_sharedindex_on_signal);
@@ -2070,7 +2077,7 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock, }if (istate->cache_changed &SPLIT_INDEX_ORDERED ) { -int ret =write_shared_index (istate); +int ret =write_shared_index (istate, lock, flags);if (ret)return ret; }
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 27/32 rev-parse: add --shared-index-path to get shared index path
In reply to Nguyễn Thái Ngọc DuyNormally scripts do not have to be aware about split indexes because all shared indexes are in $GIT_DIR. A simple "mv $tmp_index $GIT_DIR/somewhere" is enough. Scripts that generate temporary indexes and move them across repos must be aware about split index and copy the shared file as well. This option enables that.
--- Documentation/git-rev-parse.txt | 4 ++++ builtin/rev-parse.c | 10 ++++++++++ 2 files changed, 14 insertions(+)
Documentation/git-rev-parse.txt +4 -0
@@ -245,6 +245,10 @@ print a message to stderr and exit with nonzero status. --show-toplevel:: Show the absolute path of the top-level directory. +--shared-index-path:: + Show the path to the shared index file in split index mode, or + empty if not in split-index mode. + Other Options ~~~~~~~~~~~~~
builtin/rev-parse.c +10 -0
@@ -11,6 +11,7 @@#include "parse-options.h" #include "diff.h" #include "revision.h" +#include "split-index.h" #define DO_REVS 1#define DO_NOREV 2
@@ -775,6 +776,15 @@ int cmd_rev_parse(int argc, const char **argv, const char *prefix) : "false");continue ; } +if (!strcmp (arg,"--shared-index-path" )) { +if (read_cache () <0 ) +die (_ ("Could not read the index" )); +if (the_index.split_index) { +const unsigned char *sha1 = the_index.split_index->base_sha1; +puts (git_path ("sharedindex.%s" ,sha1_to_hex (sha1))); + } +continue ; + }if (starts_with (arg,"--since=" )) {show_datestring ("--max-age=" , arg+8 );continue ;
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 28/32 read-tree: force split-index mode off on --index-output
In reply to Nguyễn Thái Ngọc DuyJust a (paranoid?) safety measure..
--- read-cache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
read-cache.c +2 -1
@@ -2070,7 +2070,8 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock, {struct split_index *si = istate->split_index; -if (!si || (istate->cache_changed & ~EXTMASK )) { +if (!si || alternate_index_output || + (istate->cache_changed & ~EXTMASK )) {if (si)hashclr (si->base_sha1);return do_write_locked_index (istate, lock, flags);
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 29/32 read-tree: note about dropping split-index mode or index version
In reply to Nguyễn Thái Ngọc Duy--- builtin/read-tree.c | 9 +++++++++ 1 file changed, 9 insertions(+)
builtin/read-tree.c +9 -0
@@ -155,6 +155,15 @@ int cmd_read_tree(int argc, const char **argv, const char *unused_prefix)if (1 < opts.merge + opts.reset + prefix_set)die ("Which one? -m, --reset, or --prefix?" ); +/* +* NEEDSWORK +* +* The old index should be read anyway even if we're going to +* destroy all index entries because we still need to preserve +* certain information such as index version or split-index +* mode. +*/ +if (opts.reset || opts.merge || opts.prefix) {if (read_cache_unmerged () && (opts.prefix || opts.merge))die ("You need to resolve your current index first" );
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 30/32 read-cache: force split index mode with GIT_TEST_SPLIT_INDEX
In reply to Nguyễn Thái Ngọc DuyThis could be used to run the whole test suite with split indexes. Index splitting is carried out at random. "git read-tree" also resets the index and forces splitting at the next update.
I had a lot of headaches with the test suite, which proves it exercises split index pretty good.
--- read-cache.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
read-cache.c +9 -1
@@ -1885,8 +1885,11 @@ static int do_write_index(struct index_state *istate, int newfd, } } -if (!istate->version) +if (!istate->version) { istate->version =get_index_format_default (); +if (getenv ("GIT_TEST_SPLIT_INDEX" )) +init_split_index (istate); + }/* demote version 3 to version 2 when the latter suffices */ if (istate->version ==3 || istate->version ==2 )
@@ -2077,6 +2080,11 @@ int write_locked_index(struct index_state *istate, struct lock_file *lock,return do_write_locked_index (istate, lock, flags); } +if (getenv ("GIT_TEST_SPLIT_INDEX" )) { +int v = si->base_sha1[0 ]; +if ((v &15 ) <6 ) + istate->cache_changed |=SPLIT_INDEX_ORDERED ; + }if (istate->cache_changed &SPLIT_INDEX_ORDERED ) {int ret =write_shared_index (istate, lock, flags);if (ret)
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 31/32 t2104: make sure split index mode is off for the version test
In reply to Nguyễn Thái Ngọc DuyVersion tests only make sense when all entries are in the same file, so we can see if version is downgraded to 2 if 3 is not required.
--- t/t2104-update-index-skip-worktree.sh | 2 ++ 1 file changed, 2 insertions(+)
t/t2104-update-index-skip-worktree.sh +2 -0
@@ -7,6 +7,8 @@ test_description='skip-worktree bit test' . ./test-lib.sh +sane_unset GIT_TEST_SPLIT_INDEX + test_set_index_version 3 cat >expect.full <<EOF
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 32/32 t1700: new tests for split-index mode
In reply to Nguyễn Thái Ngọc Duy--- .gitignore | 1 + Makefile | 1 + cache.h | 2 + read-cache.c | 3 +- t/t1700-split-index.sh (new +x) | 194 ++++++++++++++++++++++++++++++++++++++++ test-dump-split-index.c (new) | 34 +++++++ 6 files changed, 233 insertions(+), 2 deletions(-) create mode 100755 t/t1700-split-index.sh create mode 100644 test-dump-split-index.c
.gitignore +1 -0
@@ -180,6 +180,7 @@ /test-date /test-delta /test-dump-cache-tree +/test-dump-split-index /test-scrap-cache-tree /test-genrandom /test-hashmap
Makefile +1 -0
@@ -562,6 +562,7 @@ TEST_PROGRAMS_NEED_X += test-ctype TEST_PROGRAMS_NEED_X += test-date TEST_PROGRAMS_NEED_X += test-delta TEST_PROGRAMS_NEED_X += test-dump-cache-tree +TEST_PROGRAMS_NEED_X += test-dump-split-index TEST_PROGRAMS_NEED_X += test-genrandom TEST_PROGRAMS_NEED_X += test-hashmap TEST_PROGRAMS_NEED_X += test-index-version
cache.h +2 -0
@@ -473,6 +473,8 @@ extern int daemonize(void);struct lock_file ;extern int read_index (struct index_state *);extern int read_index_preload (struct index_state *,const struct pathspec *pathspec); +extern int do_read_index (struct index_state *istate,const char *path, +int must_exist);/* for testting only! */ extern int read_index_from (struct index_state *,const char *path);extern int is_index_unborn (struct index_state *);extern int read_index_unmerged (struct index_state *);
read-cache.c +1 -2
@@ -1463,8 +1463,7 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk, }/* remember to discard_cache() before reading a different cache! */ -static int do_read_index (struct index_state *istate,const char *path, -int must_exist) +int do_read_index (struct index_state *istate,const char *path,int must_exist) {int fd, i;struct stat st;
t/t1700-split-index.sh new file +194 -0
194 lines not on this page · raw message
test-dump-split-index.c new file +34 -0
@@ -0,0 +1,34 @@ +#include "cache.h" +#include "split-index.h" +#include "ewah/ewok.h" + +static void show_bit (size_t pos,void *data) +{ +printf (" %d" , (int )pos); +} + +int main (int ac,char **av) +{ +struct split_index *si; +int i; + +do_read_index (&the_index, av[1 ],1 ); +printf ("own %s\n" ,sha1_to_hex (the_index.sha1)); + si = the_index.split_index; +if (!si) { +printf ("not a split index\n" ); +return 0 ; + } +printf ("base %s\n" ,sha1_to_hex (si->base_sha1)); +for (i =0 ; i< the_index.cache_nr; i++) { +struct cache_entry *ce = the_index.cache[i]; +printf ("%06o %s %d\t%s\n" , ce->ce_mode, +sha1_to_hex (ce->sha1),ce_stage (ce), ce->name); + } +printf ("replacements:" ); +ewah_each_bit (si->replace_bitmap, show_bit,NULL ); +printf ("\ndeletions:" ); +ewah_each_bit (si->delete_bitmap, show_bit,NULL ); +printf ("\n" ); +return 0 ; +}
Signature · 1 line
1.9.1.346.ga2b5940
Quoted 10 lines · Nguyễn Thái Ngọc Duy
I hinted about it earlier [1]. It now passes the test suite and with a design that I'm happy with (thanks to Junio for a suggestion about the rename problem).
From the user point of view, this reduces the writable size of index down to the number of updated files. For example my webkit index v4 is 14MB. With a fresh split, I only have to update an index of 200KB. Every file I touch will add about 80 bytes to that. As long as I don't touch every single tracked file in my worktree, I should not pay penalty for writing 14MB index file on every operation.
This is a very welcome type of improvement.
I am however concerned about the complexity of the format employed. Why do we need two EWAH bitmaps in the new index? Why isn't this just a pair of sorted files that are merge-joined at read, with records in $GIT_DIR/index taking priority over same-named records in $GIT_DIR/sharedindex.$SHA1? Deletes could be marked with a bit or an "all zero" metadata record.
Quoted 9 lines
The read penalty is not addressed here, so I still pay 14MB hashing cost. But that's an easy problem. We could cache the validated index in a daemon. Whenever git needs to load an index, it pokes the daemon. The daemon verifies that the on-disk index still has the same signature, then sends the in-mem index to git. When git updates the index, it pokes the daemon again to update in-mem index. Next time git reads the index, it does not have to pay I/O cost any more (actually it does but the cost is hidden away when you do not have to read it yet).
If we are going this far, maybe it is worthwhile building a mmap() region the daemon exports to the git client that holds the "in memory" format of the index. Clients would mmap this PROT_READ, MAP_PRIVATE and can then quickly access the base file information without doing further validation, or copying the large(ish) data over a pipe.
Junio had some other great ideas for improving the index on really large trees. Maybe I should let him comment since they are really his ideas. Something about not even checking out most files, storing most subtrees as just a "tree" entry in the index. E.g. if you are a bad developer and never touch the "t/" subdirectory then that is stored as just "t" and the SHA-1 of the "t" tree, rather than the recursively exploded list of the test directory.
Quoted 2 lines · Nguyễn Thái Ngọc Duy
The read penalty is not addressed here, so I still pay 14MB hashing cost.
Hmm, yeah, the cost for verify_hdr() would still matter, and presumably you would be hashing the additional 200kB to validate the smaller "changes since the base" file to give users the same level of protection against corruption.
Quoted 4 lines
Doing this in other implementations should be easy (at least the reading part) and with small code change. The whole index format is retained. All you need is to read a new extension that contains two ewah-bitmaps and apply the changes to create the final index.
Why bitmaps, though? Naïvely I would have expected you to read from two sorted streams and have the transaction log override the base.
Intrigued to find it out...
Re: [PATCH 17/32] read-cache: split-index mode
In reply to Nguyễn Thái Ngọc Duycache.h struct cache_entry { · line 141
Quoted 12 lines · Nguyễn Thái Ngọc Duy
cache.h +1
-0@@ -135,6 +135,7 @@ struct cache_entry {unsigned int ce_mode;unsigned int ce_flags;unsigned int ce_namelen; +unsigned int index;/* for link extension */ unsigned char sha1[20 ];char name[FLEX_ARRAY ];/* more */ };
I am not sure if we want to keep an otherwise unused 8-byte around per cache entry (especially for a large project where the split index mode should matter) after we read the index.
I expected to see some code where entries in this incremental index are used to override the entries from the base/shared index, but merge_base_index() seems to do just memcpy() to discard the former and replace them with the latter. Is this step meant to work at all, or is it a smaller step meant to be completed in later patches?
I do think it is sensible to keep two arrays of "struct cache_entry" around (one for base and one for incremental changes) inside index_state, and the patch seems to do so via "struct split_index" that does have a copy of saved_cache. If the write-out codepath walks these two sorted arrays in parallel, shouldn't it be able to figure out which entry is added, deleted and modified without fattening this structure?
Maybe it is too early for me to be asking these questions and it may be better if I read the whole series twice and wait it to become clear to me why this field is necessary. I dunno.
Re: [PATCH 17/32] read-cache: split-index mode
Quoted 19 lines · Junio C Hamano
cache.h struct cache_entry { · line 141
Quoted 12 lines · Nguyễn Thái Ngọc Duy
cache.h +1
-0@@ -135,6 +135,7 @@ struct cache_entry {unsigned int ce_mode;unsigned int ce_flags;unsigned int ce_namelen; +unsigned int index;/* for link extension */ unsigned char sha1[20 ];char name[FLEX_ARRAY ];/* more */ };I am not sure if we want to keep an otherwise unused 8-byte around per cache entry (especially for a large project where the split index mode should matter) after we read the index.
Quoted 5 lines
I expected to see some code where entries in this incremental index are used to override the entries from the base/shared index, but
merge_base_index()seems to do justmemcpy()to discard the former and replace them with the latter. Is this step meant to work at all, or is it a smaller step meant to be completed in later patches?
This field only matters at write time, not read time. It's to quickly detect if an entry is shared, see prepare_to_write_split_index().
Quoted 7 lines
I do think it is sensible to keep two arrays of "struct
cache_entry" around (one for base and one for incremental changes) insideindex_state, and the patch seems to do so via "structsplit_index" that does have a copy ofsaved_cache. If the write-out codepath walks these two sorted arrays in parallel, shouldn't it be able to figure out which entry is added, deleted and modified without fattening this structure?
So far without that "index" field I would have to resort to hasing entries in both arrays to find the shared paths. But ideas are welcome.
Signature · 1 line
Duy
Quoted 12 lines · Shawn Pearce
Quoted 10 lines · Nguyễn Thái Ngọc Duy
I hinted about it earlier [1]. It now passes the test suite and with a design that I'm happy with (thanks to Junio for a suggestion about the rename problem).
From the user point of view, this reduces the writable size of index down to the number of updated files. For example my webkit index v4 is 14MB. With a fresh split, I only have to update an index of 200KB. Every file I touch will add about 80 bytes to that. As long as I don't touch every single tracked file in my worktree, I should not pay penalty for writing 14MB index file on every operation.
Quoted 8 lines
This is a very welcome type of improvement.
I am however concerned about the complexity of the format employed. Why do we need two EWAH bitmaps in the new index? Why isn't this just a pair of sorted files that are merge-joined at read, with records in $GIT_DIR/index taking priority over same-named records in $GIT_DIR/sharedindex.$SHA1? Deletes could be marked with a bit or an "all zero" metadata record.
With the bitmaps, I know the exact position to replace or delete an entry. Merge sort works, but I would need to walk through all entries in both indexes to compare entry name and stage, a bit costly in my opinion. And if you look at the format description in patch 0017, I store the replaced entries without their names to save a bit more space. "EWAH" is just an implementation detail. A straightforward bitmap should work fine (25kb for 200k entries seem reasonable).
Signature · 1 line
Duy
Re: [PATCH 17/32] read-cache: split-index mode
In reply to Duy NguyenQuoted 11 lines · Duy Nguyen
Quoted 7 lines
I do think it is sensible to keep two arrays of "struct
cache_entry" around (one for base and one for incremental changes) insideindex_state, and the patch seems to do so via "structsplit_index" that does have a copy ofsaved_cache. If the write-out codepath walks these two sorted arrays in parallel, shouldn't it be able to figure out which entry is added, deleted and modified without fattening this structure?So far without that "index" field I would have to resort to hasing entries in both arrays to find the shared paths. But ideas are welcome.
Hmm, why do you need to hash, when both arrays are sorted? Wouldn't it be just the matter of walking these two arrays in parallel, with one scanning index for each array initialized to the beginning, comparing the elements pointed by these indices, noting the side that comes earlier in the sort order and advancing the index on that side (or if they compare equal then advance both), ...?
Re: [PATCH 24/32] split-index: strip pathname of on-disk replaced entries
In reply to Nguyễn Thái Ngọc DuyThis triggers "saved_namelen may be used uninitialized" for me, even though it looks clear that it is used under CE_STRIP_NAME and it is assigned under that condition. Sigh to a stupid compiler...
Re: [PATCH 17/32] read-cache: split-index mode
In reply to Junio C HamanoQuoted 14 lines · Junio C Hamano
Quoted 11 lines · Duy Nguyen
Quoted 7 lines
I do think it is sensible to keep two arrays of "struct
cache_entry" around (one for base and one for incremental changes) insideindex_state, and the patch seems to do so via "structsplit_index" that does have a copy ofsaved_cache. If the write-out codepath walks these two sorted arrays in parallel, shouldn't it be able to figure out which entry is added, deleted and modified without fattening this structure?So far without that "index" field I would have to resort to hasing entries in both arrays to find the shared paths. But ideas are welcome.
Quoted 6 lines
Hmm, why do you need to hash, when both arrays are sorted? Wouldn't it be just the matter of walking these two arrays in parallel, with one scanning index for each array initialized to the beginning, comparing the elements pointed by these indices, noting the side that comes earlier in the sort order and advancing the index on that side (or if they compare equal then advance both), ...?
And compare all names and stages (especially in the unpack-trees case, when no entry is reused). I kinda hope to avoid that. Speaking about reusing cache_entry, we won't be able to share cache_entry because when it's freed in replace_index_entry, or remove_index_entry_at in the main index, we need to locate the same entry in the shared index as well and remove that stale pointer. Without sharing, we nearly double memory usage from the beginning.
Signature · 1 line
Duy
Re: [PATCH 17/32] read-cache: split-index mode
Quoted 1 line · Duy Nguyen
when no entry is reused). I kinda hope to avoid that.
I see.
Quoted 6 lines
Speaking about
reusingcache_entry, we won't be able to sharecache_entrybecause when it's freed inreplace_index_entry, orremove_index_entry_atin the main index, we need to locate the same entry in the shared index as well and remove that stale pointer. Without sharing, we nearly double memory usage from the beginning.
Yeah, the point being to have most of the entries come from the base one, it is expected the real one and the saved base one will be mostly the same, so sharing is really necessary.
Re: [PATCH 18/32] read-cache: mark new entries for split index
In reply to Nguyễn Thái Ngọc DuyQuoted 4 lines · Nguyễn Thái Ngọc Duy
Make sure entry addition does not lead to unifying the index. We don't need to explicitly keep track of new entries. If
ce->indexis zero, they're new. Otherwise it's unlikely that they are new, but we'll do a through check later at writing time.
s/through/thorough/
Quoted 21 lines
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- read-cache.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)read-cache.c +2
-2@@ -38,7 +38,8 @@ static struct cache_entry *refresh_cache_entry(struct cache_entry *ce,#define CACHE_EXT_LINK 0x6c696e6b/* "link" */ /* changes that can be kept in $GIT_DIR/index (basically all extensions) */ -#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED) +#define EXTMASK (RESOLVE_UNDO_CHANGED | CACHE_TREE_CHANGED | \ + CE_ENTRY_ADDED)struct index_state the_index;static const char *alternate_index_output; --1.9.1.346.ga2b5940
Quoted 6 lines · Nguyễn Thái Ngọc Duy
From the user point of view, this reduces the writable size of index down to the number of updated files. For example my webkit index v4 is 14MB. With a fresh split, I only have to update an index of 200KB. Every file I touch will add about 80 bytes to that. As long as I don't touch every single tracked file in my worktree, I should not pay penalty for writing 14MB index file on every operation.
I played around with these changes a bit and have some questions:
* These changes should only affect performance when the index is updated, right? In other words, if I do "git status; git status" the second "git status" shouldn't update the index and therefore shouldn't have a noticeable performance improvement relative to Git without these patches. Right?
* Do you have any before/after benchmark results you can share?
* Are there any benchmark scripts I can use to test it out in my own repositories?
* Is there a debug utility I can use to examine the contents of the index and sharedindex.* files in a more human-readable way?
I'm asking because in my (very basic) tests I noticed that with the following command:
git status; time git status
the second "git status" had an unexpected ~20% performance improvement in my repo relative to a build without your patches. The second "git status" in the following command also had about a ~20% performance improvement:
git status; touch file-in-index; time git status
So it seems like the patches did improve performance somewhat, but in ways I wasn't expecting. (I'm not entirely certain my benchmark method is sound.)
Thanks,
Richard
Quoted 7 lines · Richard Hansen
I played around with these changes a bit and have some questions:
* These changes should only affect performance when the index is updated, right? In other words, if I do "git status; git status" the second "git status" shouldn't update the index and therefore shouldn't have a noticeable performance improvement relative to Git without these patches. Right?
Yes, provided that other factors in "git status" give stable numbers too.
Quoted 1 line
* Do you have any before/after benchmark results you can share?
I did not pay much attention to benchmarking because the index size is pretty much the deciding factor to write performance (of course too much computation could add up to that..). But here it is with the 14MB webkit index, about 182k files. The lines of interest are the first one (read_cache) and forth (update_index_if_able).
With normal index
pclouds@lanh ~/d/webkit $ time ~/w/git/git status >/dev/null
278.382ms gitmodules_config:199 if (read_cache() < 0) die("index file
0.004ms cmd_status:1294 read_cache_preload(&s.pathspec);
489.275ms cmd_status:1295 refresh_index(&the_index, REFRESH_QUIE
6.191ms cmd_status:1299 update_index_if_able(&the_index, &inde
12.321ms wt_status_collect:616 wt_status_collect_changes_worktree(s)
12.733ms wt_status_collect:621 wt_status_collect_changes_index(s)
98.043ms lazy_init_name_hash:136 { int nr; if (istate->name_hash_initia 915.331ms wt_status_collect:622 wt_status_collect_untracked(s)
real 0m1.727s
user 0m0.809s
sys 0m0.915s
pclouds@lanh ~/d/webkit $ touch wscript
pclouds@lanh ~/d/webkit $ time ~/w/git/git status >/dev/null
276.307ms gitmodules_config:199 if (read_cache() < 0) die("index file
0.004ms cmd_status:1294 read_cache_preload(&s.pathspec);
504.034ms cmd_status:1295 refresh_index(&the_index, REFRESH_QUIE
356.122ms cmd_status:1299 update_index_if_able(&the_index, &inde
11.870ms wt_status_collect:616 wt_status_collect_changes_worktree(s)
10.077ms wt_status_collect:621 wt_status_collect_changes_index(s)
96.205ms lazy_init_name_hash:136 { int nr; if (istate->name_hash_initia 899.425ms wt_status_collect:622 wt_status_collect_untracked(s)
real 0m2.071s
user 0m1.115s
sys 0m0.953s
pclouds@lanh ~/d/webkit $ time ~/w/git/git status >/dev/null
279.424ms gitmodules_config:199 if (read_cache() < 0) die("index file
0.004ms cmd_status:1294 read_cache_preload(&s.pathspec);
484.303ms cmd_status:1295 refresh_index(&the_index, REFRESH_QUIE
5.288ms cmd_status:1299 update_index_if_able(&the_index, &inde
13.927ms wt_status_collect:616 wt_status_collect_changes_worktree(s)
12.507ms wt_status_collect:621 wt_status_collect_changes_index(s)
98.220ms lazy_init_name_hash:136 { int nr; if (istate->name_hash_initia 920.985ms wt_status_collect:622 wt_status_collect_untracked(s)
real 0m1.731s user 0m0.830s sys 0m0.897s
And with split index:
pclouds@lanh ~/d/webkit $ time ~/w/git/git update-index --split-index
real 0m0.660s
user 0m0.601s
sys 0m0.058s
pclouds@lanh ~/d/webkit $ time ~/w/git/git status >/dev/null
281.211ms gitmodules_config:199 if (read_cache() < 0) die("index file
0.003ms cmd_status:1294 read_cache_preload(&s.pathspec);
479.629ms cmd_status:1295 refresh_index(&the_index, REFRESH_QUIE
5.489ms cmd_status:1299 update_index_if_able(&the_index, &inde
12.611ms wt_status_collect:616 wt_status_collect_changes_worktree(s)
11.235ms wt_status_collect:621 wt_status_collect_changes_index(s)
96.086ms lazy_init_name_hash:136 { int nr; if (istate->name_hash_initia 894.489ms wt_status_collect:622 wt_status_collect_untracked(s)
real 0m1.697s
user 0m0.813s
sys 0m0.881s
pclouds@lanh ~/d/webkit $ touch wscript
pclouds@lanh ~/d/webkit $ time ~/w/git/git status >/dev/null
291.411ms gitmodules_config:199 if (read_cache() < 0) die("index file
0.003ms cmd_status:1294 read_cache_preload(&s.pathspec);
475.144ms cmd_status:1295 refresh_index(&the_index, REFRESH_QUIE
24.348ms cmd_status:1299 update_index_if_able(&the_index, &inde
12.440ms wt_status_collect:616 wt_status_collect_changes_worktree(s)
10.400ms wt_status_collect:621 wt_status_collect_changes_index(s)
97.147ms lazy_init_name_hash:136 { int nr; if (istate->name_hash_initia 907.240ms wt_status_collect:622 wt_status_collect_untracked(s)
real 0m1.734s
user 0m0.842s
sys 0m0.888s
pclouds@lanh ~/d/webkit $ time ~/w/git/git status >/dev/null
281.119ms gitmodules_config:199 if (read_cache() < 0) die("index file
0.004ms cmd_status:1294 read_cache_preload(&s.pathspec);
479.702ms cmd_status:1295 refresh_index(&the_index, REFRESH_QUIE
5.061ms cmd_status:1299 update_index_if_able(&the_index, &inde
12.220ms wt_status_collect:616 wt_status_collect_changes_worktree(s)
11.408ms wt_status_collect:621 wt_status_collect_changes_index(s)
95.374ms lazy_init_name_hash:136 { int nr; if (istate->name_hash_initia 896.931ms wt_status_collect:622 wt_status_collect_untracked(s)
real 0m1.700s user 0m0.809s sys 0m0.888s
Quoted 2 lines
* Are there any benchmark scripts I can use to test it out in my own repositories?
You could use the patch I used to generate the timing above. Patch at the bottom of this mail.
Quoted 2 lines
* Is there a debug utility I can use to examine the contents of the index and sharedindex.* files in a more human-readable way?
test-dump-split-index <path-to-$GIT_DIR/index>
will show you the content of "index". Entries without names are replaced entries. Entries with them are added. They are followed by replace/delete bitmaps printed out. So pretty much everything stored in $GIT_DIR/index in human-readable format.
git rev-parse --shared-index-path will give you the path to sharedindex. ls-files --stage can be used to show that.
Quoted 10 lines
I'm asking because in my (very basic) tests I noticed that with the following command:
git status; time git status
the second "git status" had an unexpected ~20% performance improvement in my repo relative to a build without your patches. The second "git status" in the following command also had about a ~20% performance improvement:
Quoted 5 lines
git status; touch file-in-index; time git status
So it seems like the patches did improve performance somewhat, but in ways I wasn't expecting. (I'm not entirely certain my benchmark method is sound.)
git-status is a complex operation. If you want to focus on this writing part only, "git update-index" may be better.
And the timing patch:
-- 8< --
builtin/commit.c +3 -3
@@ -1291,12 +1291,12 @@ int cmd_status(int argc, const char **argv, const char *prefix)PATHSPEC_PREFER_FULL , prefix, argv); -read_cache_preload (&s.pathspec); -refresh_index (&the_index,REFRESH_QUIET |REFRESH_UNMERGED , &s.pathspec,NULL ,NULL ); + TIME(read_cache_preload(&s.pathspec);); + TIME(refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, &s.pathspec, NULL, NULL);); fd =hold_locked_index (&index_lock,0 );if (0 <= fd) -update_index_if_able (&the_index, &index_lock); +TIME (update_index_if_able (&the_index, &index_lock)); s.is_initial =get_sha1 (s.reference, sha1) ?1 :0 ; s.ignore_submodule_arg = ignore_submodule_arg;
cache.h +17 -0
@@ -1480,4 +1480,21 @@ void stat_validity_update(struct stat_validity *sv, int fd);int versioncmp (const char *s1,const char *s2); +#define TIME (x) { \ + extern int time_level__; \ + struct timeval tv1, tv2; \ + int current_level = time_level__; \ + gettimeofday(&tv1, NULL); \ + time_level__++; \ + x; \ + time_level__--; \ + gettimeofday(&tv2, NULL); \ + fprintf(stderr, "% 10.3fms%*s%s:%d %.*s\n", \ + tv2.tv_sec * 1000.0 + tv2.tv_usec / 1000.0 - \ + tv1.tv_sec * 1000.0 - tv1.tv_usec / 1000.0, \ + current_level, " ", \ + __FUNCTION__, __LINE__, 38, #x \ + ); \ + } +#endif /* CACHE_H */
environment.c +1 -0
@@ -63,6 +63,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ struct startup_info *startup_info;unsigned long pack_size_limit_cfg; +int time_level__;/* * The character that begins a commented line in user-editable file
name-hash.c +2 -2
@@ -122,7 +122,7 @@ static int cache_entry_cmp(const struct cache_entry *ce1, }static void lazy_init_name_hash (struct index_state *istate) -{ + TIME({int nr;if (istate->name_hash_initialized)
@@ -133,7 +133,7 @@ static void lazy_init_name_hash(struct index_state *istate)for (nr =0 ; nr < istate->cache_nr; nr++)hash_index_entry (istate, istate->cache[nr]); istate->name_hash_initialized =1 ; -} + })void add_name_hash (struct index_state *istate,struct cache_entry *ce) {
submodule.c +2 -2
@@ -195,8 +195,8 @@ void gitmodules_config(void)int pos;strbuf_addstr (&gitmodules_path, work_tree);strbuf_addstr (&gitmodules_path,"/.gitmodules" ); -if (read_cache () <0 ) -die ("index file corrupt" ); + TIME(if (read_cache() < 0) + die("index file corrupt");); pos =cache_name_pos (".gitmodules" ,11 );if (pos <0 ) {/* .gitmodules not found or isn't merged */ pos =-1 - pos;
wt-status.c +3 -3
@@ -613,13 +613,13 @@ static void wt_status_collect_untracked(struct wt_status *s)void wt_status_collect (struct wt_status *s) { -wt_status_collect_changes_worktree (s); +TIME (wt_status_collect_changes_worktree (s));if (s->is_initial)wt_status_collect_changes_initial (s);else -wt_status_collect_changes_index (s); -wt_status_collect_untracked (s); +TIME (wt_status_collect_changes_index (s)); +TIME (wt_status_collect_untracked (s)); }static void wt_status_print_unmerged (struct wt_status *s)
-- 8< --
Quoted 10 lines · Shawn Pearce
Quoted 9 lines
The read penalty is not addressed here, so I still pay 14MB hashing cost. But that's an easy problem. We could cache the validated index in a daemon. Whenever git needs to load an index, it pokes the daemon. The daemon verifies that the on-disk index still has the same signature, then sends the in-mem index to git. When git updates the index, it pokes the daemon again to update in-mem index. Next time git reads the index, it does not have to pay I/O cost any more (actually it does but the cost is hidden away when you do not have to read it yet).
Quoted 5 lines
If we are going this far, maybe it is worthwhile building a
mmap()region the daemon exports to the git client that holds the "in memory" format of the index. Clients would mmap thisPROT_READ,MAP_PRIVATEand can then quickly access the base file information without doing further validation, or copying the large(ish) data over a pipe.
The below patch implements such a daemon to cache the index. It takes 91ms and 377ms to load a 25MB index with and without the daemon. I use share memory instead of pipe, but the format is still "on disk" not "in memory" for simplicity. I think we're good even without in memory format.
The daemon should work on Windows after shm_open and unix socket are replaced with the equivalents. Then we could cache name-hash in it too.
With all improvements on (index v4, split index, preload index, untracked cache, read-cache daemon), "git status" goes from 1.8s to 0.6s on webkit.git (the CPU is clocked at 800 MHz so this is close to "poor machine" case).
Time to convince Junio it's good and push bit by bit to master :)
-- 8< --
.gitignore +1 -0
@@ -110,6 +110,7 @@ /git-pull /git-push /git-quiltimport +/git-read-cache--daemon /git-read-tree /git-rebase /git-rebase--am
Makefile +6 -0
@@ -1502,6 +1502,12 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifdef HAVE_SHM + BASIC_CFLAGS += -DHAVE_SHM + EXTLIBS += -lrt + PROGRAM_OBJS += read-cache--daemon.o +endif + ifdef DIR_HAS_BSD_GROUP_SEMANTICS COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS endif
cache.h +4 -0
@@ -290,10 +290,14 @@ struct index_state {struct split_index *split_index;struct cache_time timestamp; unsigned name_hash_initialized : 1, + keep_mmap : 1, + poke_daemon : 1, initialized : 1;struct hashmap name_hash;struct hashmap dir_hash;unsigned char sha1[20 ]; +void *mmap; +size_t mmap_size; };extern struct index_state the_index;
config.mak.uname +1 -0
@@ -33,6 +33,7 @@ ifeq ($(uname_S),Linux) HAVE_PATHS_H = YesPlease LIBC_CONTAINS_LIBINTL = YesPlease HAVE_DEV_TTY = YesPlease + HAVE_SHM = YesPlease endif ifeq ($(uname_S),GNU/kFreeBSD) NO_STRLCPY = YesPlease
git-compat-util.h +8 -0
@@ -723,4 +723,12 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);#define gmtime_r git_gmtime_r#endif +#ifndef HAVE_SHM +static inline int shm_open (const char *path,int flags,int mode) +{ + errno =ENOSYS ; +return -1 ; +} +#endif +#endif
read-cache--daemon.c new file +168 -0
168 lines not on this page · raw message
read-cache.c +65 -6
142 lines not on this page · raw message
-- 8< --
Quoted 17 lines · Duy Nguyen
Quoted 10 lines · Shawn Pearce
Quoted 9 lines
The read penalty is not addressed here, so I still pay 14MB hashing cost. But that's an easy problem. We could cache the validated index in a daemon. Whenever git needs to load an index, it pokes the daemon. The daemon verifies that the on-disk index still has the same signature, then sends the in-mem index to git. When git updates the index, it pokes the daemon again to update in-mem index. Next time git reads the index, it does not have to pay I/O cost any more (actually it does but the cost is hidden away when you do not have to read it yet).
Quoted 5 lines
If we are going this far, maybe it is worthwhile building a
mmap()region the daemon exports to the git client that holds the "in memory" format of the index. Clients would mmap thisPROT_READ,MAP_PRIVATEand can then quickly access the base file information without doing further validation, or copying the large(ish) data over a pipe.
Quoted 5 lines
The below patch implements such a daemon to cache the index. It takes 91ms and 377ms to load a 25MB index with and without the daemon. I use share memory instead of pipe, but the format is still "on disk" not "in memory" for simplicity. I think we're good even without in memory format.
Interesting ;-).
cover Speed up cache loading time
In reply to Shawn PearceQuoted 5 lines · Duy Nguyen
The below patch implements such a daemon to cache the index. It takes 91ms and 377ms to load a 25MB index with and without the daemon. I use share memory instead of pipe, but the format is still "on disk" not "in memory" for simplicity. I think we're good even without in memory format.
Here is a better version (on top of split-index). I duplicated webkit index 8 times to get its size to 199MB (version 2), close to what Facebook tried last time [1]. read_cache() on index v2, v4, with the daemon caching v2 and v4 respectively is 2994.861ms (199MB index file), 2245.113ms (118MB) and 663.399ms and 880.935ms. The best number is 4.5 times better the worst.
That is clocked at 800 MHz. A repository at this size deserves a better CPU. At 2.5 GHz we spend 183.228ms on loading the index. A reasonable number to me. If we scale other parts of git-status as well as this, we should be able to make "git status" within 1 or 2 seconds.
The tested index does not have fully populated cache-tree so real world numbers could be a bit higher.
[1] http://thread.gmane.org/gmane.comp.version-control.git/189776/focus=190156
Nguyễn Thái Ngọc Duy (8):
read-cache: allow to keep mmap'd memory after reading
unix-socket: stub impl. for platforms with no unix socket support
daemonize: set a flag before exiting the main process Add read-cache--daemon for caching index and related stuff
read-cache: try index data from shared memory
read-cache--daemon: do not read index from shared memory
read-cache: skip verifying trailing SHA-1 on cached index
read-cache: inform the daemon that the index has been updated
.gitignore | 1 + Documentation/config.txt | 4 + Documentation/git-read-cache--daemon.txt (new) | 27 ++++ Makefile | 8 + builtin/gc.c | 2 +- cache.h | 7 +- config.c | 12 ++ config.mak.uname | 1 + daemon.c | 2 +- environment.c | 1 + read-cache--daemon.c (new) | 208 +++++++++++++++++++++++++ read-cache.c | 116 +++++++++++++- setup.c | 4 +- submodule.c | 1 + unix-socket.h | 18 +++ wrapper.c | 14 ++ 16 files changed, 414 insertions(+), 12 deletions(-) create mode 100644 Documentation/git-read-cache--daemon.txt create mode 100644 read-cache--daemon.c
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 1/8 read-cache: allow to keep mmap'd memory after reading
--- cache.h | 3 +++ read-cache.c | 13 ++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-)
cache.h +3 -0
@@ -290,10 +290,13 @@ struct index_state {struct split_index *split_index;struct cache_time timestamp; unsigned name_hash_initialized : 1, + keep_mmap : 1, initialized : 1;struct hashmap name_hash;struct hashmap dir_hash;unsigned char sha1[20 ]; +void *mmap; +size_t mmap_size; };extern struct index_state the_index;
read-cache.c +12 -1
@@ -1495,6 +1495,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) mmap =xmmap (NULL , mmap_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 );if (mmap ==MAP_FAILED )die_errno ("unable to map index file" ); +if (istate->keep_mmap) { + istate->mmap = mmap; + istate->mmap_size = mmap_size; + }close (fd); hdr = mmap;
@@ -1547,10 +1551,12 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) src_offset +=8 ; src_offset += extsize; } -munmap (mmap, mmap_size); +if (!istate->keep_mmap) +munmap (mmap, mmap_size);return istate->cache_nr; unmap: + istate->mmap =NULL ;munmap (mmap, mmap_size);die ("index file corrupt" ); }
@@ -1576,6 +1582,7 @@ int read_index_from(struct index_state *istate, const char *path)discard_index (split_index->base); else split_index->base = xcalloc(1, sizeof(*split_index->base)); + split_index->base->keep_mmap = istate->keep_mmap; ret =do_read_index (split_index->base,git_path ("sharedindex.%s" ,sha1_to_hex (split_index->base_sha1)),1 );
@@ -1618,6 +1625,10 @@ int discard_index(struct index_state *istate)free (istate->cache); istate->cache =NULL ; istate->cache_alloc =0 ; +if (istate->keep_mmap && istate->mmap) { +munmap (istate->mmap, istate->mmap_size); + istate->mmap =NULL ; + }discard_split_index (istate);return 0 ; }
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 2/8 unix-socket: stub impl. for platforms with no unix socket support
In reply to Nguyễn Thái Ngọc DuyWith this we can make unix_stream_* calls without #ifdef.
--- Makefile | 2 ++ unix-socket.h | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+)
Makefile +2 -0
@@ -1417,6 +1417,8 @@ ifndef NO_UNIX_SOCKETS LIB_H += unix-socket.h PROGRAM_OBJS += credential-cache.o PROGRAM_OBJS += credential-cache--daemon.o +else + BASIC_CFLAGS += -DNO_UNIX_SOCKETS endif ifdef NO_ICONV
unix-socket.h +18 -0
@@ -1,7 +1,25 @@#ifndef UNIX_SOCKET_H #define UNIX_SOCKET_H +#ifndef NO_UNIX_SOCKETS +int unix_stream_connect (const char *path);int unix_stream_listen (const char *path); +#else + +static inline int unix_stream_connect (const char *path) +{ + errno =ENOSYS ; +return -1 ; +} + +static inline int unix_stream_listen (const char *path) +{ + errno =ENOSYS ; +return -1 ; +} + +#endif +#endif /* UNIX_SOCKET_H */
Signature · 1 line
1.9.1.346.ga2b5940
Re: [PATCH 2/8] unix-socket: stub impl. for platforms with no unix socket support
Quoted 41 lines · Nguyễn Thái Ngọc Duy
With this we can make unix_stream_* calls without #ifdef.
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- Makefile | 2 ++ unix-socket.h | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+)Makefile +2
-0@@ -1417,6 +1417,8 @@ ifndef NO_UNIX_SOCKETS LIB_H += unix-socket.h PROGRAM_OBJS += credential-cache.o PROGRAM_OBJS += credential-cache--daemon.o +else + BASIC_CFLAGS += -DNO_UNIX_SOCKETS endif ifdef NO_ICONVunix-socket.h +9
-0@@ -1,7 +1,25 @@#ifndef UNIX_SOCKET_H #define UNIX_SOCKET_H +#ifndef NO_UNIX_SOCKETS +int unix_stream_connect (const char *path);int unix_stream_listen (const char *path); +#else + +static inline int unix_stream_connect (const char *path) +{ + errno =ENOSYS ; +return -1 ; +}
unix-socket.h line 25
Quoted 10 lines
unix-socket.h +9
-0+ +static inline int unix_stream_listen (const char *path) +{ + errno =ENOSYS ; +return -1 ; +} + +#endif +#endif /* UNIX_SOCKET_H */
OK, so I missed this before my other two comments. But still... in what way does errno=ENOSYS make this *work*? Won't we end up compiling lots of non-functional tools on Windows in this case?
Re: [PATCH 2/8] unix-socket: stub impl. for platforms with no unix socket support
Quoted 48 lines · Erik Faye-Lund
Quoted 47 lines · Nguyễn Thái Ngọc Duy
With this we can make unix_stream_* calls without #ifdef.
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- Makefile | 2 ++ unix-socket.h | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+)Makefile +2
-0@@ -1417,6 +1417,8 @@ ifndef NO_UNIX_SOCKETS LIB_H += unix-socket.h PROGRAM_OBJS += credential-cache.o PROGRAM_OBJS += credential-cache--daemon.o +else + BASIC_CFLAGS += -DNO_UNIX_SOCKETS endif ifdef NO_ICONVunix-socket.h +15
-0@@ -1,7 +1,25 @@#ifndef UNIX_SOCKET_H #define UNIX_SOCKET_H +#ifndef NO_UNIX_SOCKETS +int unix_stream_connect (const char *path);int unix_stream_listen (const char *path); +#else + +static inline int unix_stream_connect (const char *path) +{ + errno =ENOSYS ; +return -1 ; +} + +static inline int unix_stream_listen (const char *path) +{ + errno =ENOSYS ; +return -1 ; +}
Quoted 8 lines
unix-socket.h line 25
Quoted 4 lines
unix-socket.h +3
-0+ +#endif +#endif /* UNIX_SOCKET_H */ OK, so I missed this before my other two comments. But still... in what way does errno=ENOSYS make this *work*? Won't we end up compiling lots of non-functional tools on Windows in this case?
ENOSYS makes git-credential-cache.c just die with the message "unable to start cache daemon", and git-credential--daemon.c die with "unable to bind to <socket_path>".
PATCH 3/8 daemonize: set a flag before exiting the main process
In reply to Nguyễn Thái Ngọc DuyThis allows signal handlers and atexit functions to realize this situation and not clean up.
--- builtin/gc.c | 2 +- cache.h | 2 +- daemon.c | 2 +- setup.c | 4 +++- 4 files changed, 6 insertions(+), 4 deletions(-)
builtin/gc.c +1 -1
@@ -325,7 +325,7 @@ int cmd_gc(int argc, const char **argv, const char *prefix)* failure to daemonize is ok, we'll continue * in foreground */ -daemonize (); +daemonize (NULL ); }else add_repack_all_option ();
cache.h +1 -1
@@ -450,7 +450,7 @@ extern int set_git_dir_init(const char *git_dir, const char *real_git_dir, int);extern int init_db (const char *template_dir,unsigned int flags);extern void sanitize_stdfds (void ); -extern int daemonize (void ); +extern int daemonize (int *);#define alloc_nr (x) (((x)+16)*3/2)
daemon.c +1 -1
@@ -1311,7 +1311,7 @@ int main(int argc, char **argv)return execute ();if (detach) { -if (daemonize ()) +if (daemonize (NULL ))die ("--detach not supported on this platform" ); }else sanitize_stdfds ();
setup.c +3 -1
@@ -842,7 +842,7 @@ void sanitize_stdfds(void)close (fd); } -int daemonize (void ) +int daemonize (int *daemonized) {#ifdef NO_POSIX_GOODIES errno =ENOSYS ;
@@ -854,6 +854,8 @@ int daemonize(void)case -1 :die_errno ("fork failed" );default : +if (daemonized) + *daemonized =1 ;exit (0 ); }if (setsid () ==-1 )
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 2/3 Add read-cache--daemon
In reply to Nguyễn Thái Ngọc Duy--- .gitignore | 1 + Makefile | 6 ++ config.mak.uname | 1 + git-compat-util.h | 8 +++ read-cache--daemon.c (new) | 167 +++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+) create mode 100644 read-cache--daemon.c
.gitignore +1 -0
@@ -110,6 +110,7 @@ /git-pull /git-push /git-quiltimport +/git-read-cache--daemon /git-read-tree /git-rebase /git-rebase--am
Makefile +6 -0
@@ -1502,6 +1502,12 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifdef HAVE_SHM + BASIC_CFLAGS += -DHAVE_SHM + EXTLIBS += -lrt + PROGRAM_OBJS += read-cache--daemon.o +endif + ifdef DIR_HAS_BSD_GROUP_SEMANTICS COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS endif
config.mak.uname +1 -0
@@ -33,6 +33,7 @@ ifeq ($(uname_S),Linux) HAVE_PATHS_H = YesPlease LIBC_CONTAINS_LIBINTL = YesPlease HAVE_DEV_TTY = YesPlease + HAVE_SHM = YesPlease endif ifeq ($(uname_S),GNU/kFreeBSD) NO_STRLCPY = YesPlease
git-compat-util.h +8 -0
@@ -723,4 +723,12 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);#define gmtime_r git_gmtime_r#endif +#ifndef HAVE_SHM +static inline int shm_open (const char *path,int flags,int mode) +{ + errno =ENOSYS ; +return -1 ; +} +#endif +#endif
read-cache--daemon.c new file +167 -0
167 lines not on this page · raw message
Signature · 1 line
1.9.1.346.ga2b5940
Re: [PATCH 2/3] Add read-cache--daemon
Quoted 5 lines · Nguyễn Thái Ngọc Duy
Makefile +0
-0
Makefile ifdef HAVE_DEV_TTY · line 1510
Quoted 9 lines
Makefile +6
-0@@ -1502,6 +1502,12 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifdef HAVE_SHM + BASIC_CFLAGS += -DHAVE_SHM + EXTLIBS += -lrt + PROGRAM_OBJS += read-cache--daemon.o +endif +
I think read-cache--daemon will fail in case of NO_UNIX_SOCKETS.
But, read-cache--daemon.c only gets compiled if we have shm_open...
Quoted 5 lines
git-compat-util.h +0
-0
git-compat-util.h struct tm *git_gmtime_r(const time_t *, struct tm *); · line 732
Quoted 10 lines
git-compat-util.h +7
-0@@ -723,4 +723,12 @@ struct tm *git_gmtime_r(const time_t *, struct tm *);#define gmtime_r git_gmtime_r#endif +#ifndef HAVE_SHM +static inline int shm_open (const char *path,int flags,int mode) +{ + errno =ENOSYS ; +return -1 ; +} +#endif
...yet, you introduce a compatibility-shim...
Quoted 37 lines
read-cache--daemon.c new file +31
-0@@ -0,0 +1,167 @@ +#include "cache.h" +#include "sigchain.h" +#include "unix-socket.h" +#include "split-index.h" +#include "pkt-line.h" + +static char *socket_path; +static struct strbuf shm_index =STRBUF_INIT ; +static struct strbuf shm_sharedindex =STRBUF_INIT ; + +static void cleanup_socket (void ) +{ +if (socket_path) +unlink (socket_path); +if (shm_index.len) +shm_unlink (shm_index.buf); +if (shm_sharedindex.len) +shm_unlink (shm_sharedindex.buf); +} + +static void cleanup_socket_on_signal (int sig) +{ +cleanup_socket (); +sigchain_pop (sig); +raise (sig); +} + +static void share_index (struct index_state *istate,struct strbuf *shm_path) +{ +struct strbuf sb =STRBUF_INIT ; +void *map;
read-cache--daemon.c line 41
Quoted 10 lines
read-cache--daemon.c new file +10
-0+int fd; + +strbuf_addf (&sb,"/git-index-%s" ,sha1_to_hex (istate->sha1)); +if (shm_path->len &&strcmp (sb.buf, shm_path->buf)) { +shm_unlink (shm_path->buf); +strbuf_reset (shm_path); + } + fd =shm_open (sb.buf,O_RDWR |O_CREAT |O_TRUNC ,0700 ); +if (fd <0 ) +return ;
...that only gets called from read-cache--daemon.c, which already only gets compiled if we have open?
Re: [PATCH 2/3] Add read-cache--daemon
What do you think is a good replacement for unix socket on Windows? It's only used to refresh the cache in the daemon, no sensitive data sent over, so security is not a problem. I'm thinking maybe just TCP/IP server, but that's going to be a system-wide daemon.. Perhaps the windows daemon could just monitor $GIT_DIR/index and refresh it?
Signature · 1 line
Duy
Re: [PATCH 2/3] Add read-cache--daemon
Quoted 5 lines · Duy Nguyen
What do you think is a good replacement for unix socket on Windows? It's only used to refresh the cache in the daemon, no sensitive data sent over, so security is not a problem. I'm thinking maybe just TCP/IP server, but that's going to be a system-wide daemon.. Perhaps the windows daemon could just monitor $GIT_DIR/index and refresh it?
Windows has support for Named Pipes, which seems like the right kind of communication channel. However, the programming model differs quite a bit from unix-sockets:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365594%28v=vs.85%29.aspx
Re: [PATCH 2/3] Add read-cache--daemon
Quoted 7 lines · Erik Faye-Lund
Quoted 5 lines · Duy Nguyen
What do you think is a good replacement for unix socket on Windows? It's only used to refresh the cache in the daemon, no sensitive data sent over, so security is not a problem. I'm thinking maybe just TCP/IP server, but that's going to be a system-wide daemon.. Perhaps the windows daemon could just monitor $GIT_DIR/index and refresh it?
Quoted 5 lines
Windows has support for Named Pipes, which seems like the right kind of communication channel. However, the programming model differs quite a bit from unix-sockets:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365594%28v=vs.85%29.aspx
Yeah that was my first option, but if code cannot be shared to differences then we probably should go another way. The old FindWindow/PostMessage still works with modern Windows, right? Maybe we could create a window with a name derived from the daemon's pid and save the name in the index, then PostMessage can signal the daemon. On the UNIX front, we store pid and send SIGUSR1 instead..The good thing here is the Git side will be very simple (PostMessage vs kill).
Signature · 1 line
Duy
Re: [PATCH 2/3] Add read-cache--daemon
Quoted 14 lines · Duy Nguyen
Quoted 7 lines · Erik Faye-Lund
Quoted 5 lines · Duy Nguyen
What do you think is a good replacement for unix socket on Windows? It's only used to refresh the cache in the daemon, no sensitive data sent over, so security is not a problem. I'm thinking maybe just TCP/IP server, but that's going to be a system-wide daemon.. Perhaps the windows daemon could just monitor $GIT_DIR/index and refresh it?
Quoted 5 lines
Windows has support for Named Pipes, which seems like the right kind of communication channel. However, the programming model differs quite a bit from unix-sockets:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365594%28v=vs.85%29.aspx
Quoted 7 lines
Yeah that was my first option, but if code cannot be shared to differences then we probably should go another way. The old FindWindow/PostMessage still works with modern Windows, right? Maybe we could create a window with a name derived from the daemon's pid and save the name in the index, then PostMessage can signal the daemon. On the UNIX front, we store pid and send SIGUSR1 instead..The good thing here is the Git side will be very simple (PostMessage vs kill).
Hmmm.... I'm a bit worried about having to load in USER32.DLL just to read the cache that way. But it seems we already do that, thanks to compat/poll/poll.c (it depends on DispatchMessage, MsgWaitForMultipleObjects, PeekMessage and TranslateMessage, all from that DLL).
Preferably, we should delay-load USER32.DLL in compat/poll/poll.c, but if we start needing it for the reading the index, it'll be loaded by the vast majority of processes anyway.
Re: [PATCH 2/3] Add read-cache--daemon
Quoted 29 lines · Erik Faye-Lund
Quoted 14 lines · Duy Nguyen
Quoted 7 lines · Erik Faye-Lund
Quoted 5 lines · Duy Nguyen
What do you think is a good replacement for unix socket on Windows? It's only used to refresh the cache in the daemon, no sensitive data sent over, so security is not a problem. I'm thinking maybe just TCP/IP server, but that's going to be a system-wide daemon.. Perhaps the windows daemon could just monitor $GIT_DIR/index and refresh it?
Quoted 5 lines
Windows has support for Named Pipes, which seems like the right kind of communication channel. However, the programming model differs quite a bit from unix-sockets:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365594%28v=vs.85%29.aspx
Quoted 7 lines
Yeah that was my first option, but if code cannot be shared to differences then we probably should go another way. The old FindWindow/PostMessage still works with modern Windows, right? Maybe we could create a window with a name derived from the daemon's pid and save the name in the index, then PostMessage can signal the daemon. On the UNIX front, we store pid and send SIGUSR1 instead..The good thing here is the Git side will be very simple (PostMessage vs kill).
Hmmm.... I'm a bit worried about having to load in USER32.DLL just to read the cache that way. But it seems we already do that, thanks to
compat/poll/poll.c(it depends on DispatchMessage, MsgWaitForMultipleObjects, PeekMessage and TranslateMessage, all from that DLL).
Quoted 3 lines
Preferably, we should delay-load USER32.DLL in
compat/poll/poll.c, but if we start needing it for the reading the index, it'll be loaded by the vast majority of processes anyway.
Thanks for the info. I'll see if we can still stick to named pipes. If we have to load user32.dll, hopefully the gain will outweigh load time for that dell.
Signature · 1 line
Duy
Re: [PATCH 2/3] Add read-cache--daemon
Quoted 30 lines · Duy Nguyen
Quoted 29 lines · Erik Faye-Lund
Quoted 14 lines · Duy Nguyen
Quoted 7 lines · Erik Faye-Lund
Quoted 5 lines · Duy Nguyen
What do you think is a good replacement for unix socket on Windows? It's only used to refresh the cache in the daemon, no sensitive data sent over, so security is not a problem. I'm thinking maybe just TCP/IP server, but that's going to be a system-wide daemon.. Perhaps the windows daemon could just monitor $GIT_DIR/index and refresh it?
Quoted 5 lines
Windows has support for Named Pipes, which seems like the right kind of communication channel. However, the programming model differs quite a bit from unix-sockets:
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365594%28v=vs.85%29.aspx
Quoted 7 lines
Yeah that was my first option, but if code cannot be shared to differences then we probably should go another way. The old FindWindow/PostMessage still works with modern Windows, right? Maybe we could create a window with a name derived from the daemon's pid and save the name in the index, then PostMessage can signal the daemon. On the UNIX front, we store pid and send SIGUSR1 instead..The good thing here is the Git side will be very simple (PostMessage vs kill).
Hmmm.... I'm a bit worried about having to load in USER32.DLL just to read the cache that way. But it seems we already do that, thanks to
compat/poll/poll.c(it depends on DispatchMessage, MsgWaitForMultipleObjects, PeekMessage and TranslateMessage, all from that DLL).
Quoted 7 lines
Quoted 3 lines
Preferably, we should delay-load USER32.DLL in
compat/poll/poll.c, but if we start needing it for the reading the index, it'll be loaded by the vast majority of processes anyway.Thanks for the info. I'll see if we can still stick to named pipes. If we have to load user32.dll, hopefully the gain will outweigh load time for that dell.
I just timed it here on my system, and omitting USER32.DLL didn't gain anything for "git --version", so I suspect I was worrying too soon.
Re: [PATCH 2/3] Add read-cache--daemon
In reply to Erik Faye-LundQuoted 18 lines · Erik Faye-Lund
Quoted 5 lines · Nguyễn Thái Ngọc Duy
Makefile +0
-0Makefile ifdef HAVE_DEV_TTY · line 1510
Quoted 9 lines
Makefile +6
-0@@ -1502,6 +1502,12 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifdef HAVE_SHM + BASIC_CFLAGS += -DHAVE_SHM + EXTLIBS += -lrt + PROGRAM_OBJS += read-cache--daemon.o +endif +I think read-cache--daemon will fail in case of
NO_UNIX_SOCKETS.
Quoted 1 line
But,
read-cache--daemon.conly gets compiled if we haveshm_open...
Portability is something to be sorted out.Ideally we should not build this unless we have both unix socket and shared memory support. On Windows, I'm not sure how much code can be shared, or it'll be a completely different program. In that case maybe this program should be read-cache--daemon-posix (at least the .c file name, the binary may be still git-read-cache--daemon) or something and the Windows version read-cache--daemon-windows..
Signature · 1 line
Duy
PATCH 4/8 Add read-cache--daemon for caching index and related stuff
In reply to Nguyễn Thái Ngọc DuyThe name of the shared memory folows the template "/git-index-<SHA1>" where <SHA1> is the trailing SHA-1 of the index file. If such a shared memory exists, it contains the same index content as on disk. The content is already validated by the daemon. Note that it does not necessarily use the same format as the on-disk version. The content could be in a format that can be parsed much faster, or even reused without parsing).
While preparing the shm object, the daemon would keep the shm object "/git-index-<SHA1>.lock". After "git-index-<SHA1>" is ready, the ".lock" object is removed. A shared object must not be updated afterwards. So if ".lock" does not exist, it's safe to assume that the associated shm object is ready.
Other info could also by cached if it's tied to the index. For example, name hash could be stored in "/git-namehash-<SHA1>"..
After Git writes a new index down, it may want to ask the daemon to preload the new index so next time Git runs the index is already validated and in memory. It does so by send a command to a UNIX socket in $GIT_DIR/daemon/index.
Windows can use its named shared memory instead of POSIX shared memory and probably named pipe in place of UNIX socket.
--- .gitignore | 1 + Documentation/git-read-cache--daemon.txt (new) | 27 ++++ Makefile | 6 + config.mak.uname | 1 + read-cache--daemon.c (new) | 207 +++++++++++++++++++++++++ wrapper.c | 14 ++ 6 files changed, 256 insertions(+) create mode 100644 Documentation/git-read-cache--daemon.txt create mode 100644 read-cache--daemon.c
.gitignore +1 -0
@@ -110,6 +110,7 @@ /git-pull /git-push /git-quiltimport +/git-read-cache--daemon /git-read-tree /git-rebase /git-rebase--am
Documentation/git-read-cache--daemon.txt new file +27 -0
@@ -0,0 +1,27 @@ +git-read-cache--daemon(1) +============= + +NAME +---- +git-daemon - A simple cache server for speeding up index file access + +SYNOPSIS +-------- +[verse] +'git daemon' [--detach] + +DESCRIPTION +----------- +Keep the index file in memory for faster access. This daemon is per +repository. Note that core.useReadCacheDaemon must be set for Git to +contact the daemon. This daemon is only available on POSIX system with +shared memory support (e.g. Linux) + +OPTIONS +------- +--detach:: + Detach from the shell. + +GIT +--- +Part of the linkgit:git[1] suite
Makefile +6 -0
@@ -1504,6 +1504,12 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifdef HAVE_SHM + BASIC_CFLAGS += -DHAVE_SHM + EXTLIBS += -lrt + PROGRAM_OBJS += read-cache--daemon.o +endif + ifdef DIR_HAS_BSD_GROUP_SEMANTICS COMPAT_CFLAGS += -DDIR_HAS_BSD_GROUP_SEMANTICS endif
config.mak.uname +1 -0
@@ -33,6 +33,7 @@ ifeq ($(uname_S),Linux) HAVE_PATHS_H = YesPlease LIBC_CONTAINS_LIBINTL = YesPlease HAVE_DEV_TTY = YesPlease + HAVE_SHM = YesPlease endif ifeq ($(uname_S),GNU/kFreeBSD) NO_STRLCPY = YesPlease
read-cache--daemon.c new file +207 -0
207 lines not on this page · raw message
wrapper.c +14 -0
@@ -455,3 +455,17 @@ struct passwd *xgetpwuid_self(void) errno ? strerror(errno) : _("no such user"));return pw; } + +#ifndef HAVE_SHM +int shm_open (const char *path,int flags,mode_t mode) +{ + errno =ENOSYS ; +return -1 ; +} + +int shm_unlink (const char *path) +{ + errno =ENOSYS ; +return -1 ; +} +#endif
Signature · 1 line
1.9.1.346.ga2b5940
Re: [PATCH 4/8] Add read-cache--daemon for caching index and related stuff
Quoted 8 lines · Nguyễn Thái Ngọc Duy
Documentation/git-read-cache--daemon.txt new file +2
-0@@ -0,0 +1,27 @@ +git-read-cache--daemon(1) +=============
Documentation/git-read-cache--daemon.txt line 12
Quoted 10 lines
Documentation/git-read-cache--daemon.txt new file +10
-0+ +NAME +---- +git-daemon - A simple cache server for speeding up index file access + +SYNOPSIS +-------- +[verse] +'git daemon' [--detach] +
Huh, "git daemon" can't be right...
Makefile ifdef HAVE_DEV_TTY · line 1511
Quoted 13 lines
Makefile +5
-0@@ -1504,6 +1504,12 @@ ifdef HAVE_DEV_TTY BASIC_CFLAGS += -DHAVE_DEV_TTY endif +ifdef HAVE_SHM + BASIC_CFLAGS += -DHAVE_SHM + EXTLIBS += -lrt + PROGRAM_OBJS += read-cache--daemon.o +endif
I think this also needs to be protected against NO_UNIX_SOCKETS
PATCH 5/8 read-cache: try index data from shared memory
In reply to Nguyễn Thái Ngọc Duy--- Documentation/config.txt | 4 ++++ cache.h | 1 + config.c | 12 ++++++++++++ environment.c | 1 + read-cache.c | 43 +++++++++++++++++++++++++++++++++++++++++++ submodule.c | 1 + 6 files changed, 62 insertions(+)
Documentation/config.txt +4 -0
@@ -617,6 +617,10 @@ relatively high IO latencies. With this set to 'true', Git will do the index comparison to the filesystem data in parallel, allowing overlapping IO's. +core.useReadCacheDaemon:: + Use `git read-cache--daemon` to speed up index reading. See + linkgit:git-read-cache--daemon for more information. + core.createObject:: You can set this to 'link', in which case a hardlink followed by a delete of the source are used to make sure that object creation
cache.h +1 -0
@@ -603,6 +603,7 @@ extern size_t packed_git_limit;extern size_t delta_base_cache_limit;extern unsigned long big_file_threshold;extern unsigned long pack_size_limit_cfg; +extern int use_read_cache_daemon;/* * Do replace refs need to be checked this run? This variable is
config.c +12 -0
@@ -874,6 +874,18 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } +#ifdef HAVE_SHM +/* +* Currently git-read-cache--daemon is only built when +* HAVE_SHM is set. Ignore user settings if HAVE_SHM is not +* defined. +*/ +if (!strcmp (var,"core.usereadcachedaemon" )) { + use_read_cache_daemon =git_config_bool (var, value); +return 0 ; + } +#endif +/* Add other config variables here and to Documentation/config.txt. */ return 0 ; }
environment.c +1 -0
@@ -63,6 +63,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ struct startup_info *startup_info;unsigned long pack_size_limit_cfg; +int use_read_cache_daemon;/* * The character that begins a commented line in user-editable file
read-cache.c +43 -0
55 lines not on this page · raw message
submodule.c +1 -0
@@ -195,6 +195,7 @@ void gitmodules_config(void)int pos;strbuf_addstr (&gitmodules_path, work_tree);strbuf_addstr (&gitmodules_path,"/.gitmodules" ); +git_config (git_default_config,NULL );if (read_cache () <0 )die ("index file corrupt" ); pos =cache_name_pos (".gitmodules" ,11 );
Signature · 1 line
1.9.1.346.ga2b5940
Re: [PATCH 5/8] read-cache: try index data from shared memory
Quoted 84 lines · Nguyễn Thái Ngọc Duy
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- Documentation/config.txt | 4 ++++ cache.h | 1 + config.c | 12 ++++++++++++ environment.c | 1 + read-cache.c | 43 +++++++++++++++++++++++++++++++++++++++++++ submodule.c | 1 + 6 files changed, 62 insertions(+)Documentation/config.txt +4
-0@@ -617,6 +617,10 @@ relatively high IO latencies. With this set to 'true', Git will do the index comparison to the filesystem data in parallel, allowing overlapping IO's. +core.useReadCacheDaemon:: + Use `git read-cache--daemon` to speed up index reading. See + linkgit:git-read-cache--daemon for more information. + core.createObject:: You can set this to 'link', in which case a hardlink followed by a delete of the source are used to make sure that object creationcache.h +1
-0@@ -603,6 +603,7 @@ extern size_t packed_git_limit;extern size_t delta_base_cache_limit;extern unsigned long big_file_threshold;extern unsigned long pack_size_limit_cfg; +extern int use_read_cache_daemon;/* * Do replace refs need to be checked this run? This variable is config.c +12
-0@@ -874,6 +874,18 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } +#ifdef HAVE_SHM +/* +* Currently git-read-cache--daemon is only built when +* HAVE_SHM is set. Ignore user settings if HAVE_SHM is not +* defined. +*/ +if (!strcmp (var,"core.usereadcachedaemon" )) { + use_read_cache_daemon =git_config_bool (var, value); +return 0 ; + } +#endif +/* Add other config variables here and to Documentation/config.txt. */ return 0 ; }environment.c +1
-0@@ -63,6 +63,7 @@ int merge_log_config = -1;int precomposed_unicode =-1 ;/* see probe_utf8_pathname_composition() */ struct startup_info *startup_info;unsigned long pack_size_limit_cfg; +int use_read_cache_daemon;/* * The character that begins a commented line in user-editable file read-cache.c +4
-0@@ -1462,6 +1462,48 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,return ce; } +static void *try_shm (void *mmap,size_t *mmap_size) +{ +struct strbuf sb =STRBUF_INIT ; +size_t old_size = *mmap_size;
read-cache.c static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk, · line 1478
Quoted 10 lines
read-cache.c +10
-0+void *new_mmap; +struct stat st; +int fd; + +if (old_size <=20 || !use_read_cache_daemon) +return mmap; + +strbuf_addf (&sb,"/git-index-%s.lock" , +sha1_to_hex ((unsigned char *)mmap + old_size -20 )); + fd =shm_open (sb.buf,O_RDONLY ,0777 );
OK, so *here* you do an unguarded use of shm_open, but the code never gets executed because of the HAVE_SHM guard in git_default_core_config. Perhaps not introduce the compatibility shim until this patch, then?
PATCH 3/3 read-cache: try index data from shared memory
In reply to Nguyễn Thái Ngọc Duy--- read-cache.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
read-cache.c +30 -0
@@ -1462,6 +1462,35 @@ static struct cache_entry *create_from_disk(struct ondisk_cache_entry *ondisk,return ce; } +static void *try_shm (void *mmap,size_t mmap_size) +{ +struct strbuf sb =STRBUF_INIT ; +void *new_mmap; +struct stat st; +int fd; + +if (mmap_size <=20 ) +return mmap; + +strbuf_addf (&sb,"/git-index-%s" , +sha1_to_hex ((unsigned char *)mmap + mmap_size -20 )); + fd =shm_open (sb.buf,O_RDONLY ,0777 ); +strbuf_release (&sb); +if (fd <0 ) +return mmap; +if (fstat (fd, &st) || st.st_size != mmap_size) { +close (fd); +return mmap; + } + new_mmap =xmmap (NULL , mmap_size,PROT_READ ,MAP_PRIVATE , fd,0 ); +close (fd); +if (new_mmap ==MAP_FAILED ) +return mmap; +munmap (mmap, mmap_size); +return new_mmap; +} + +/* remember to discard_cache() before reading a different cache! */ int do_read_index (struct index_state *istate,const char *path,int must_exist) {
@@ -1501,6 +1530,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) }close (fd); + mmap =try_shm (mmap, mmap_size); hdr = mmap;if (verify_hdr (hdr, mmap_size) <0 ) goto unmap;
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 6/8 read-cache--daemon: do not read index from shared memory
In reply to Nguyễn Thái Ngọc DuyIt does not hurt doing that. But it does not help anybody either.
--- read-cache--daemon.c | 1 + read-cache.c | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-)
read-cache--daemon.c +1 -0
@@ -145,6 +145,7 @@ static void serve_cache(const char *socket_path, int detach)if (fd <0 )die_errno ("unable to bind to '%s'" , socket_path); + use_read_cache_daemon =-1 ;refresh ();if (detach &&daemonize (&daemonized))die_errno ("unable to detach" );
read-cache.c +1 -1
@@ -1470,7 +1470,7 @@ static void *try_shm(void *mmap, size_t *mmap_size)struct stat st;int fd; -if (old_size <=20 || !use_read_cache_daemon) +if (old_size <=20 || use_read_cache_daemon <=0 )return mmap; strbuf_addf(&sb, "/git-index-%s.lock",
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 7/8 read-cache: skip verifying trailing SHA-1 on cached index
In reply to Nguyễn Thái Ngọc DuyThe daemon is responsible for verifying the index before putting it in the shared memory. No need to redo it again.
--- read-cache.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
read-cache.c +5 -2
@@ -1332,6 +1332,8 @@ static int verify_hdr(struct cache_header *hdr, unsigned long size) 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); +if (!size) +return 0 ;git_SHA1_Init (&c);git_SHA1_Update (&c, hdr, size -20 );git_SHA1_Final (sha1, &c);
@@ -1511,7 +1513,7 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist)struct stat st;unsigned long src_offset;struct cache_header *hdr; -void *mmap; +void *mmap, *old_mmap;size_t mmap_size;struct strbuf previous_name_buf =STRBUF_INIT , *previous_name;
@@ -1543,9 +1545,10 @@ int do_read_index(struct index_state *istate, const char *path, int must_exist) }close (fd); + old_mmap = mmap; mmap =try_shm (mmap, &mmap_size); hdr = mmap; -if (verify_hdr (hdr, mmap_size) <0 ) +if (verify_hdr (hdr, old_mmap != mmap ?0 : mmap_size) <0 ) goto unmap;hashcpy (istate->sha1, (const unsigned char *)hdr + mmap_size -20 );
Signature · 1 line
1.9.1.346.ga2b5940
PATCH 8/8 read-cache: inform the daemon that the index has been updated
In reply to Nguyễn Thái Ngọc DuyThe daemon would immediately load the new index in memory in background. Next time Git needs to read the index again, everything is ready.
--- cache.h | 1 + read-cache.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 5 deletions(-)
cache.h +1 -0
@@ -483,6 +483,7 @@ extern int is_index_unborn(struct index_state *);extern int read_index_unmerged (struct index_state *);#define COMMIT_LOCK (1 << 0)#define CLOSE_LOCK (1 << 1) +#define REFRESH_DAEMON (1 << 2)extern int write_locked_index (struct index_state *,struct lock_file *lock,unsigned flags);extern int discard_index (struct index_state *);extern int unmerged_index (const struct index_state *);
read-cache.c +48 -5
83 lines not on this page · raw message
Signature · 1 line
1.9.1.346.ga2b5940
Re: [PATCH 8/8] read-cache: inform the daemon that the index has been updated
Quoted 56 lines · Nguyễn Thái Ngọc Duy
The daemon would immediately load the new index in memory in background. Next time Git needs to read the index again, everything is ready.
Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>--- cache.h | 1 + read-cache.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 5 deletions(-)cache.h +1
-0@@ -483,6 +483,7 @@ extern int is_index_unborn(struct index_state *);extern int read_index_unmerged (struct index_state *);#define COMMIT_LOCK (1 << 0)#define CLOSE_LOCK (1 << 1) +#define REFRESH_DAEMON (1 << 2)extern int write_locked_index (struct index_state *,struct lock_file *lock,unsigned flags);extern int discard_index (struct index_state *);extern int unmerged_index (const struct index_state *);read-cache.c +19
-0@@ -16,6 +16,9 @@#include "varint.h" #include "split-index.h" #include "sigchain.h" +#include "unix-socket.h" +#include "pkt-line.h" +#include "run-command.h" static struct cache_entry *refresh_cache_entry (struct cache_entry *ce,unsigned int options);@@ -2030,6 +2033,32 @@ void set_alternate_index_output(const char *name) alternate_index_output = name; } +static void refresh_daemon (struct index_state *istate) +{ +int fd; + fd =unix_stream_connect (git_path ("daemon/index" )); +if (fd <0 ) { +struct child_process cp; +const char *av[] = {"read-cache--daemon" ,"--detach" ,NULL }; +memset (&cp,0 ,sizeof (cp)); + cp.argv = av; + cp.git_cmd =1 ; + cp.no_stdin =1 ; +if (run_command (&cp)) +warning (_ ("failed to start read-cache--daemon: %s" ), +strerror (errno)); +return ; + }
read-cache.c void set_alternate_index_output(const char *name) · line 2061
Quoted 10 lines
read-cache.c +10
-0+/* +* packet_write() could die() but unless this is from +* update_index_if_able(), we're about to exit anyway, +* probably ok to die (for now). Blocking mode is another +* problem to deal with later. +*/ +packet_write (fd,"refresh" ); +close (fd); +} +
Seems the argument 'istate' isn't used.
Re: [PATCH 0/8] Speed up cache loading time
In reply to Nguyễn Thái Ngọc DuyQuoted 5 lines
That is clocked at 800 MHz. A repository at this size deserves a better CPU. At 2.5 GHz we spend 183.228ms on loading the index. A reasonable number to me. If we scale other parts of git-status as well as this, we should be able to make "git status" within 1 or 2 seconds.
Which harddrive do you use? Traditional or SSDs?
Does have harddrive loading time significant impact here? (just a guess/question)
Re: [PATCH 0/8] Speed up cache loading time
Quoted 7 lines · Stefan Beller
Quoted 5 lines
That is clocked at 800 MHz. A repository at this size deserves a better CPU. At 2.5 GHz we spend 183.228ms on loading the index. A reasonable number to me. If we scale other parts of git-status as well as this, we should be able to make "git status" within 1 or 2 seconds.
Which harddrive do you use? Traditional or SSDs?
Traditional
Quoted 2 lines
Does have harddrive loading time significant impact here? (just a guess/question)
In the hot cache case, I assume the index stays in OS cache anyway so hard drive should not impact much (the other parts of git-status like index refresh or untracked file listing is a different story and some may fall out of cache). My laptop has 4G ram, with my repeated tests, I guess the index (even 200mb) stayed in the cache (but did not really verify it).
Signature · 1 line
Duy
[PATCH 9/8] even faster loading time with index version 254
In reply to Shawn PearceThis dirty (and likely buggy) patch shows a direction of lowering load time even more. Basically the shared memory now contains a clean memory dump that a git process could use with little preparation (which also means it's tied to C Git, other implementations can't use this)
Memory is actually shared, git won't malloc and copy over, so even if the v254 is 235 MB (larger than v2 199MB), we use less memory.
With this patch, we can get as low as 256.442ms (compared to 663ms in 0/8) at 800 MHz, or 91ms at 2.5 GHz. Index load time should be a solved problem.
But I'm not going to polish this patch and try to get it merged. I'd rather see a real world repository of this size first to justify messing up read-cache.c even more.
--- cache.h | 2 + read-cache--daemon.c | 31 +++++------ read-cache.c | 154 ++++++++++++++++++++++++++++++++++++++++++--------- split-index.c | 3 + 4 files changed, 149 insertions(+), 41 deletions(-)
cache.h +2 -0
@@ -297,6 +297,8 @@ struct index_state {unsigned char sha1[20 ];void *mmap;size_t mmap_size; +int mmap_fd; +void *(*allocate_254)(struct index_state *,size_t ); };extern struct index_state the_index;
read-cache--daemon.c +15 -16
@@ -34,10 +34,19 @@ static void cleanup_socket_on_signal(int sig)raise (sig); } +static void *allocate_254 (struct index_state *istate,unsigned long size) +{ +ftruncate (istate->mmap_fd, size); + istate->mmap_size = size; + istate->mmap =xmmap (NULL , istate->mmap_size,PROT_READ |PROT_WRITE , +MAP_SHARED , istate->mmap_fd,0 ); +return istate->mmap !=MAP_FAILED ? istate->mmap :NULL ; +} + +extern int do_write_index (struct index_state *istate,int newfd,int strip_extensions);static int do_share_index (struct index_state *istate,struct strbuf *shm_path) {struct strbuf sb =STRBUF_INIT ; -void *map;int fd;strbuf_addf (&sb,"/git-index-%s" ,sha1_to_hex (istate->sha1));
@@ -48,21 +57,16 @@ static int do_share_index(struct index_state *istate, struct strbuf *shm_path)shm_unlink (shm_path->buf);strbuf_reset (shm_path); } -if (ftruncate (fd, istate->mmap_size)) { -close (fd); -shm_unlink (shm_path->buf); -return -1 ; - } + istate->version =254 ; + istate->allocate_254 = allocate_254; + istate->mmap_fd = fd; +do_write_index (istate,-1 ,0 );strbuf_addbuf (shm_path, &sb); - map =xmmap (NULL , istate->mmap_size,PROT_READ |PROT_WRITE , -MAP_SHARED , fd,0 ); -if (map ==MAP_FAILED ) { +if (istate->mmap ==MAP_FAILED ) {close (fd);shm_unlink (shm_path->buf);return -1 ; } -memcpy (map, istate->mmap, istate->mmap_size); -munmap (map, istate->mmap_size);fchmod (fd,0400 );close (fd);return 0 ;
@@ -88,13 +92,9 @@ static void share_index(struct index_state *istate, struct strbuf *shm_path)static void refresh () { - the_index.keep_mmap =1 ;if (read_cache () <0 )die ("could not read index" );share_index (&the_index, &shm_index); -if (the_index.split_index && - the_index.split_index->base) -share_index (the_index.split_index->base, &shm_sharedindex);discard_index (&the_index); }
@@ -145,7 +145,6 @@ static void serve_cache(const char *socket_path, int detach)if (fd <0 )die_errno ("unable to bind to '%s'" , socket_path); - use_read_cache_daemon =-1 ;refresh ();if (detach &&daemonize (&daemonized))die_errno ("unable to detach" );
read-cache.c +129 -25
266 lines not on this page · raw message
split-index.c +3 -0
@@ -302,6 +302,9 @@ void discard_split_index(struct index_state *istate)void save_or_free_index_entry (struct index_state *istate,struct cache_entry *ce) { +if (ce->index ==0xffffffff ) +return ; + if (ce->index && istate->split_index && istate->split_index->base &&
Signature · 1 line
1.9.1.346.ga2b5940
Re: [PATCH 8/8] read-cache: inform the daemon that the index has been updated
In reply to Nguyễn Thái Ngọc Duyread-cache.c void set_alternate_index_output(const char *name) · line 2049
Quoted 3 lines · Nguyễn Thái Ngọc Duy
read-cache.c +3
-0+if (run_command (&cp)) +warning (_ ("failed to start read-cache--daemon: %s" ), +strerror (errno));
errno is not always (ever?) set, so if read-cache--daemon is missing, you get:
warning: failed to start read-cache--daemon: Success