Speed up index load through parallelization
Subsystems (2)
cover Speed up index load through parallelization
This patch will help address the CPU cost of loading the index by adding a table of contents extension to the index that will allow us to multi-thread the loading and conversion of cache entries from the on-disk format, to the in-memory format. This is particularly beneficial with large indexes and V4 indexes which have more CPU cost due to the prefix-encoding.
I wanted to get feedback on the concept as the way I'm adding the table of contents information via an extension that can be read before the variable length section of cache entries and other extensions is a bit of a clever hack (see below) as is the resetting of the prefix encoding for V4 indexes. Both, however, are entirely backwards compatible with older versions of git which can still properly read and use the index.
I'm not particularly fond of the names "fastindex" and "IEOT." I've wondered if "indextoc" and "Index Table of Contents (ITOC)" would be better names but I'm open to suggestions.
As there is overhead to spinning up a thread, there is logic to only do the index loading in parallel when there are enough entries for it to help (currently set at 7,500 per thread with a minimum of 2 threads).
The impact of the change can be seen using t/helper/test-read-cache:
fastindex
test count files TRUE FALSE Savings
------------------------------------------------------------------------
test-read-cache 500 100K 6.39 8.33 23.36% test-read-cache 100 1M 12.49 18.68 33.12%
The on-disk format looks like this:
Index header
Cache entry 1
Cache entry 2
.
.
.
Extension 1
Extension 2
.
.
Index Entry Offset Table Extension (must be written last!) IEOT signature bytes
32-bit size
32-bit version
32-bit Cache Entry Offset 1
32-bit Cache Entry count
32-bit Cache Entry Offset 2
32-bit Cache Entry count
.
.
.
32-bit version
32-bit size
IEOT signature bytes
SHA1
Base Ref: master
Web-Diff: https://github.com/benpeart/git/commit/1146d38932
Checkout: git fetch https://github.com/benpeart/git fastindex-v1 && git checkout 1146d38932
Ben Peart (4):
fastindex: speed up index load through parallelization
update-index: add fastindex support to update-index
fastindex: add test tools and a test script
fastindex: add documentation for the fastindex extension
Documentation/config.txt | 8 + Documentation/git-update-index.txt | 11 + Documentation/technical/index-format.txt | 26 +++ Makefile | 2 + builtin/update-index.c | 22 ++ cache.h | 25 +++ config.c | 20 ++ config.h | 1 + environment.c | 3 + read-cache.c | 343 +++++++++++++++++++++++++++++-- t/helper/test-dump-fast-index.c | 68 ++++++ t/helper/test-fast-index.c | 84 ++++++++ t/t1800-fast-index.sh | 55 +++++ 13 files changed, 647 insertions(+), 21 deletions(-) create mode 100644 t/helper/test-dump-fast-index.c create mode 100644 t/helper/test-fast-index.c create mode 100644 t/t1800-fast-index.sh
base-commit: 7668cbc60578f99a4c048f8f8f38787930b8147b
Signature · 1 line
2.15.0.windows.1
PATCH 1/4 fastindex: speed up index load through parallelization
This patch helps address the CPU cost of loading the index by adding additional data to the index that will allow us to multi-thread the loading and conversion of cache entries.
It accomplishes this by adding an (optional) index extension that is a table of offsets to blocks of cache entries in the index file. With version 2, 3 or even 4 indexes, we can utilize the Index Entry Offset Table (IEOT) to parallelize the loading and conversion of the cache entries across all available CPU cores.
To make this work for V4 indexes, when writing the index, it periodically "resets" the prefix-compression by encoding the current entry as if the path name for the previous entry is completely different and saves the offset of that entry in the IEOT. Basically, with V4 indexes, it generates offsets into blocks of prefix-compressed entries.
To enable reading the IEOT extension before reading all the variable length cache entries and other extensions, the IEOT is written last, right before the trailing SHA1.
The format of that extension has the signature bytes and size at the beginning (like a normal extension) as well as at the end in reverse order to enable parsing the extension by seeking back from the end of the file.
During index load, read the index header then seek to the end of the index, back up past the trailing SHA1 and look for the IEOT extension signature bytes. If they exist, read the 32-bit size and seek back to the extension header and verify the leading header and size bits. If they all match, we can be assured we have a valid IEOT extension.
If the IEOT extension is available, create multiple threads to divide the work of loading and converting the cache entries across all available CPU cores. Once the cache entries are loaded, the rest of the extensions can be loaded and processed normally (skipping the IEOT entry as it has already been processed). If the IEOT extension is not available then parsing the index will proceed as usual with a single thread.
--- cache.h | 25 +++++ config.c | 20 ++++ config.h | 1 + environment.c | 3 + read-cache.c | 343 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 5 files changed, 371 insertions(+), 21 deletions(-)
cache.h +25 -0
@@ -794,6 +794,7 @@ extern char *git_replace_ref_base;extern int fsync_object_files;extern int core_preload_index; +extern int core_fast_index;extern int core_apply_sparse_checkout;extern int precomposed_unicode;extern int protect_hfs;
@@ -1942,4 +1943,28 @@ void sleep_millisec(int millisec);*/ void safe_create_dir (const char *dir,int share); + +#ifndef NO_PTHREADS +struct index_entry_offset +{ +/* starting byte offset into index file, count of index entries in this block */ +int offset, nr; +}; + +struct index_entry_offset_table +{ +int nr; +struct index_entry_offset entries[0 ]; +}; + +extern struct index_entry_offset_table *read_ieot_extension (void *mmap,size_t mmap_size); + +struct ondisk_cache_entry ; +extern struct cache_entry *create_from_disk (struct ondisk_cache_entry *ondisk, +unsigned long *ent_size, +struct strbuf *previous_name, +int use_length); + +#endif +#endif /* CACHE_H */
config.c +20 -0
@@ -1203,6 +1203,11 @@ static int git_default_core_config(const char *var, const char *value)return 0 ; } +if (!strcmp (var,"core.fastindex" )) { + core_fast_index =git_config_bool (var, value); +return 0 ; + } +if (!strcmp (var,"core.createobject" )) {if (!strcmp (value,"rename" )) object_creation_mode =OBJECT_CREATION_USES_RENAMES ;
@@ -2156,6 +2161,21 @@ int git_config_get_max_percent_split_change(void)return -1 ;/* default value */ } +int ignore_fast_index_config; +int git_config_get_fast_index (void ) +{ +int val; + +/* Hack for test programs like test-ieot */ +if (ignore_fast_index_config) +return core_fast_index; + +if (!git_config_get_maybe_bool ("core.fastindex" , &val)) +return val; + +return -1 ;/* default value */ +} +NORETURN void git_die_config_linenr(const char *key, const char *filename, int linenr) {
config.h +1 -0
@@ -212,6 +212,7 @@ extern int git_config_get_pathname(const char *key, const char **dest);extern int git_config_get_untracked_cache (void );extern int git_config_get_split_index (void );extern int git_config_get_max_percent_split_change (void ); +extern int git_config_get_fast_index (void );/* This dies if the configured or default date is in the future */ extern int git_config_get_expiry (const char *key,const char **output);
environment.c +3 -0
@@ -87,6 +87,9 @@ int auto_comment_line_char;/* Parallel index stat data preload? */ int core_preload_index =1 ; +/* Parallel index cache entry loading? */ +int core_fast_index; +/* * This is a hack for test programs like test-dump-untracked-cache to * ensure that they do not modify the untracked cache when reading it.
read-cache.c +322 -21
435 lines not on this page · raw message
Signature · 1 line
2.15.0.windows.1
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 5 lines · Ben Peart
To make this work for V4 indexes, when writing the index, it periodically "resets" the prefix-compression by encoding the current entry as if the path name for the previous entry is completely different and saves the offset of that entry in the IEOT. Basically, with V4 indexes, it generates offsets into blocks of prefix-compressed entries.
You specifically mentioned about this change in your earlier correspondence on this series, and it reminds me of Shawn's reftable proposal that also is heavily depended on prefix compression with occasional reset to allow scanning from an entry in the middle. I find it a totally sensible design choice.
Quoted 3 lines
To enable reading the IEOT extension before reading all the variable length cache entries and other extensions, the IEOT is written last, right before the trailing SHA1.
OK, we have already closed the door for further enhancements to place something other than the index extensions after this block by mistakenly made it the rule that the end of the series of extended sections must coincide with the beginning of the trailing checksum, so it does not sound all that bad to insist on this particular one to be the last, I guess. But see below...
Quoted 4 lines
The format of that extension has the signature bytes and size at the beginning (like a normal extension) as well as at the end in reverse order to enable parsing the extension by seeking back from the end of the file.
I think this is a reasonable workaround to allow a single extension that needs to be read before the main index is loaded.
But I'd suggest taking this approach one step further. Instead, what if we introduce a new extension EOIE ("end of index entries") whose sole purpose is to sit at the end of the series of extensions and point at the beginning of the index extension section of the file, to tell you where to seek in order to skip the main index?
That way, you can
- seek to the end of the index file;
- go backward skiping the trailing file checksum;
- now you might be at the end of the EOIE extension. seek back necessary number of bytes and verify EOIE header, pick up the recorded file offset of the beginning of the extensions section.
- The 4-byte sequence you found may happen to match EOIE but that is not enough to be sure that you indeed have such an extension. So the following must be done carefully, allowing the possibility that there wasn't any EOIE extension at the end.
Seek back to that offset, and repeat these three steps to skip over all extensions:
- read the (possible) 4-byte header
- read the (possible) extsize (validate that this is a sane value)
- skip that many bytes
until you come back to the location you assumed that you found your EOIE header, to make sure you did not get fooled by bytes that happened to look like one. Some "extsize" you picked up during that process may lead you beyond the end of the index file, which would be a sure sign that what you found at the end of the index file was *not* the EOIE extension but a part of some other extension who happened to have these four bytes at the right place.
which would be a lot more robust way to allow any extensions to be read before the main body of the index.
And a lot more importantly, we will leave the door open to allow more than one index extensions that we would prefer to read before reading the main body if we do it this way, because we can easily skip things over without spending cycles once we have a robust way to find where the end of the main index is. After all, the reason you placed IEOT at the end is not because you wanted to have it at the very end. You only wanted to be able to find where it is without having to parse the variable-length records of the main index. And there may be other index extensions that wants to be readable without reading the main part just like IEOT, and we do not want to close the door for them.
Hmm?
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 8 lines · Junio C Hamano
Quoted 5 lines · Ben Peart
To make this work for V4 indexes, when writing the index, it periodically "resets" the prefix-compression by encoding the current entry as if the path name for the previous entry is completely different and saves the offset of that entry in the IEOT. Basically, with V4 indexes, it generates offsets into blocks of prefix-compressed entries.
Quoted 6 lines
You specifically mentioned about this change in your earlier correspondence on this series, and it reminds me of Shawn's reftable proposal that also is heavily depended on prefix compression with occasional reset to allow scanning from an entry in the middle. I find it a totally sensible design choice.
Great! I didn't realize there was already precedent for this in other patches. I guess good minds think alike... :)
Quoted 46 lines
Quoted 3 lines
To enable reading the IEOT extension before reading all the variable length cache entries and other extensions, the IEOT is written last, right before the trailing SHA1.
OK, we have already closed the door for further enhancements to place something other than the index extensions after this block by mistakenly made it the rule that the end of the series of extended sections must coincide with the beginning of the trailing checksum, so it does not sound all that bad to insist on this particular one to be the last, I guess. But see below...
Quoted 4 lines
The format of that extension has the signature bytes and size at the beginning (like a normal extension) as well as at the end in reverse order to enable parsing the extension by seeking back from the end of the file.
I think this is a reasonable workaround to allow a single extension that needs to be read before the main index is loaded.
But I'd suggest taking this approach one step further. Instead, what if we introduce a new extension EOIE ("end of index entries") whose sole purpose is to sit at the end of the series of extensions and point at the beginning of the index extension section of the file, to tell you where to seek in order to skip the main index?
That way, you can
- seek to the end of the index file;
- go backward skiping the trailing file checksum;
- now you might be at the end of the EOIE extension. seek back necessary number of bytes and verify EOIE header, pick up the recorded file offset of the beginning of the extensions section.
- The 4-byte sequence you found may happen to match EOIE but that is not enough to be sure that you indeed have such an extension. So the following must be done carefully, allowing the possibility that there wasn't any EOIE extension at the end.
Seek back to that offset, and repeat these three steps to skip over all extensions:- read the (possible) 4-byte header
- read the (possible) extsize (validate that this is a sane value)
- skip that many bytes
Quoted 9 lines
until you come back to the location you assumed that you found your EOIE header, to make sure you did not get fooled by bytes that happened to look like one. Some "extsize" you picked up during that process may lead you beyond the end of the index file, which would be a sure sign that what you found at the end of the index file was *not* the EOIE extension but a part of some other extension who happened to have these four bytes at the right place.
I'm doing this careful examination and verification in the patch already. Please look closely at read_ieot_extension() to make sure there isn't a test I should be doing but missed.
Quoted 15 lines
which would be a lot more robust way to allow any extensions to be read before the main body of the index.
And a lot more importantly, we will leave the door open to allow more than one index extensions that we would prefer to read before reading the main body if we do it this way, because we can easily skip things over without spending cycles once we have a robust way to find where the end of the main index is. After all, the reason you placed IEOT at the end is not because you wanted to have it at the very end. You only wanted to be able to find where it is without having to parse the variable-length records of the main index. And there may be other index extensions that wants to be readable without reading the main part just like IEOT, and we do not want to close the door for them.
The proposed format for extensions (ie having both a header and a footer with name and size) in the current patch already enables having multiple extensions that can be parsed forward or backward. Any extensions that would want to be parse-able in reverse would just all need to be written one after the other after right before the trailing SHA1 (and of course, include the proper footer). The same logic that parses the IEOT extension could be extended to continue looking backwards through the file parsing extensions until it encounters an unknown signature, invalid size, etc.
That said, the IEOT is essentially a table of contents into the index (I hesitate to call it an index of the index as that could get confusing :)). While in the current iteration of this patch it only contains offsets into blocks of cache entries that are independently parse-able, I had initially considered including an offset into the index where the extensions start just as you suggest above.
When I looked at the existing extensions to see if they could be sped up by loading them on parallel threads I found that some of them assume/require they are processed in a specific order so there was no real benefit to loading them in parallel threads.
I ended up removing the offset to the first extension because I've found that trying to speculatively design a feature for a potential future need often doesn't result in what you actually need once you get there. :) Instead, I made sure there was a version number in the IEOT structure so if we actually needed a way to extend the table of contents at some point in the future (like to include an offset to the extensions), we could easily add it and increment the IEOT version.
I could add the offset back in but currently nothing would use it so we're just adding additional complexity to calculate and parse it with with no (current) benefit.
How about I rename this the "Index Table of Contents" (ITOC) to make it's potential more clear and if/when we need to enhance it with an offset to the extensions, we do so at that point in time?
Quoted 2 lines
Hmm?
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 6 lines · Ben Peart
The proposed format for extensions (ie having both a header and a footer with name and size) in the current patch already enables having multiple extensions that can be parsed forward or backward. Any extensions that would want to be parse-able in reverse would just all need to be written one after the other after right before the trailing SHA1 (and of course, include the proper footer).
In other words, anything that wants to be scannable from the tail is welcome to reimplement the same validation structure used by IEOT to check the section specific sanity constraint, and this series is not interested in introducing a more general infrastructure to make it easy for code that want to find where each extension section in the file begins without pasing the body of the index.
I find it somewhat unsatisfactory in that it is a fundamental change to allow jumping to the start of an extension section from the tail that can benefit any future codepath, and have expected a feature neutral extension whose sole purpose is to do so [*1*].
That way, extension sections whose names are all-caps can stay to be optional, even if they allow locating from the tail of the file. If you require them to implement the same validation struture as IEOT to perform section specific sanity constraint and also require them to be placed consecutively at the end, the reader MUST know about all such extensions---otherwise they cannot scan backwards and find ones that appear before an unknown but optional one. If you keep an extension section at the end whose sole purpose is to point at the beginning of extension sections, the reader can then scan forward as usual, skipping over unknown but optional ones, and reading your IEOT can merely be an user (and the first user) of that more generic feature that is futureproof, no?
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 21 lines · Junio C Hamano
Quoted 6 lines · Ben Peart
The proposed format for extensions (ie having both a header and a footer with name and size) in the current patch already enables having multiple extensions that can be parsed forward or backward. Any extensions that would want to be parse-able in reverse would just all need to be written one after the other after right before the trailing SHA1 (and of course, include the proper footer).
In other words, anything that wants to be scannable from the tail is welcome to reimplement the same validation structure used by IEOT to check the section specific sanity constraint, and this series is not interested in introducing a more general infrastructure to make it easy for code that want to find where each extension section in the file begins without pasing the body of the index.
I find it somewhat unsatisfactory in that it is a fundamental change to allow jumping to the start of an extension section from the tail that can benefit any future codepath, and have expected a feature neutral extension whose sole purpose is to do so [*1*].
Quoted 14 lines
That way, extension sections whose names are all-caps can stay to be optional, even if they allow locating from the tail of the file. If you require them to implement the same validation struture as IEOT to perform section specific sanity constraint and also require them to be placed consecutively at the end, the reader MUST know about all such extensions---otherwise they cannot scan backwards and find ones that appear before an unknown but optional one. If you keep an extension section at the end whose sole purpose is to point at the beginning of extension sections, the reader can then scan forward as usual, skipping over unknown but optional ones, and reading your IEOT can merely be an user (and the first user) of that more generic feature that is futureproof, no?
How about I add the logic to write out a special extension right before the SHA1 that contains an offset to the beginning of the extensions section. I will also add the logic in do_read_index() to search for and load this special extension if it exists.
This will provide a common framework for any future extension to take advantage of if it wants to be loaded/processed before or in parallel with the cache entries or other extensions.
For all existing extensions that assume they are loaded _after_ the cache entries, in do_read_index() I'll add the logic to use the offset (if it exists) to adjust the src_offset and then load them normally.
Given the IEOT extension is just another list of offsets into the index to enable out of order processing, I'll add those offsets into the same extension so that it is a more generic "table of contents" for the entire index. This enables us to have common/reusable way to have random access to _all_ sections in the index while maintaining backwards comparability with the existing index formats and code.
These additional offsets will initially only be used to parallelize the loading of cache entries and only if the user explicitly enables that option but I can think of other interesting uses for them in the future.
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 20 lines · Ben Peart
How about I add the logic to write out a special extension right before the SHA1 that contains an offset to the beginning of the extensions section. I will also add the logic in
do_read_index()to search for and load this special extension if it exists.This will provide a common framework for any future extension to take advantage of if it wants to be loaded/processed before or in parallel with the cache entries or other extensions.
For all existing extensions that assume they are loaded _after_ the cache entries, in
do_read_index()I'll add the logic to use the offset (if it exists) to adjust thesrc_offsetand then load them normally.Given the IEOT extension is just another list of offsets into the index to enable out of order processing, I'll add those offsets into the same extension so that it is a more generic "table of contents" for the entire index. This enables us to have common/reusable way to have random access to _all_ sections in the index while maintaining backwards comparability with the existing index formats and code.
Quoted 4 lines
These additional offsets will initially only be used to parallelize the loading of cache entries and only if the user explicitly enables that option but I can think of other interesting uses for them in the future.
If we freeze the format of IEOT extension so that we can guarantee that the very first version of Git that understands IEOT can always find the beginning of extension section in an index file that was written by future versions of Git, then I'm all for that plan, but my impression was that you are planning to make incompatible change in the future to IEOT, judging from the waythat IEOT records its own version number in the section and the reader uses it to reject an unknown one.
With that plan, what I suspect would happen is that a version of Git that understands another optional extension section that wants to be findable without scanning the main table and the then-current version of IEOT would not be able to use an index file written by a new version of Git that enhances the format of the IEOT extension bumps its extension version.
And if that is the case I would have to say that I strongly suspect that you would regret the design decision to mix it into IEOT. That is why I keep suggesting that the back pointer extension should be on its own, minimizing what it does and minimizing the need to be updated across versions of Git.
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 27 lines · Junio C Hamano
Quoted 20 lines · Ben Peart
How about I add the logic to write out a special extension right before the SHA1 that contains an offset to the beginning of the extensions section. I will also add the logic in
do_read_index()to search for and load this special extension if it exists.This will provide a common framework for any future extension to take advantage of if it wants to be loaded/processed before or in parallel with the cache entries or other extensions.
For all existing extensions that assume they are loaded _after_ the cache entries, in
do_read_index()I'll add the logic to use the offset (if it exists) to adjust thesrc_offsetand then load them normally.Given the IEOT extension is just another list of offsets into the index to enable out of order processing, I'll add those offsets into the same extension so that it is a more generic "table of contents" for the entire index. This enables us to have common/reusable way to have random access to _all_ sections in the index while maintaining backwards comparability with the existing index formats and code.
Quoted 4 lines
These additional offsets will initially only be used to parallelize the loading of cache entries and only if the user explicitly enables that option but I can think of other interesting uses for them in the future.
Quoted 8 lines
If we freeze the format of IEOT extension so that we can guarantee that the very first version of Git that understands IEOT can always find the beginning of extension section in an index file that was written by future versions of Git, then I'm all for that plan, but my impression was that you are planning to make incompatible change in the future to IEOT, judging from the way that IEOT records its own version number in the section and the reader uses it to reject an unknown one.
I have no thoughts or plans for changes in the future of IEOT (which I plan to rename ITOC). At this point in time, I can't even imagine what else we'd want as the index only contains cache entries, extensions and the trailing SHA1 and with the TOC we have random access to each of those. If nothing else, it gives another signature to verify to ensure we actually have a valid trailing extension. :)
I only added the version because I've learned my ability to predict the future is pretty bad and it leaves us that option _if_ something ever comes up that we need it _and_ are willing to live with the significant negative trade-offs you correctly point out.
Quoted 8 lines
With that plan, what I suspect would happen is that a version of Git that understands another optional extension section that wants to be findable without scanning the main table and the then-current version of IEOT would not be able to use an index file written by a new version of Git that enhances the format of the IEOT extension bumps its extension version.
Quoted 6 lines
And if that is the case I would have to say that I strongly suspect that you would regret the design decision to mix it into IEOT. That is why I keep suggesting that the back pointer extension should be on its own, minimizing what it does and minimizing the need to be updated across versions of Git.
I understand the risk but the list of offsets into the cache entries is pretty simple as well. I prefer the simplicity of a single TOC extension that gives us random access to the entire index rather than having to piece one together using multiple extensions. That model has its own set of risks and tradeoffs.
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 3 lines · Ben Peart
I have no thoughts or plans for changes in the future of IEOT (which I plan to rename ITOC). At this point in time, I can't even imagine what else we'd want as the index only contains cache entries, ...
Yeah, but the thing is that this is open source, and the imagination of the originator of the initial idea does not limit how the code and data structure evolves.
Back when I added the index extensions to the system, I didn't have perfect foresight, and I didn't have specific plans to add anything beyond "TREE" to optimize "write-tree" and "diff-index --cached".
In hindsight, I got one thing right and one thing wrong.
Even though I didn't have any plan to add a mandatory extension, I made the code to ignore optional ones and error out on mandatory ones if an index extension section is not understood. It turns out that later (in fact much later---the "TREE" extension dates back to April 2006, while "link" came in June 2014) we could add the split-index mode without having to worry about older versions of Git doing random wrong things when they see this new extension, thanks to that design decision. That went well.
On the other hand, I didn't think things through to realize that some operations may want to peek only the extensions without ever using the main table, that some other operations may want to read some extensions first before reading the main table, or more importantly, that these limitations would start mattering once Git becomes popular enough and starts housing a project tree with very many paths in the main table.
I really wish somebody had brought it up as a potential issue during the review---I would have defined the extended index format to have the simple extension at the end that points back to the tail end of the main table back then, and we wouldn't be having this back and forth now. But I was just too happy and excited that I have found a room to squeeze extension sections into the index file format without breaking existing implementations of Git (which checked the trailer checksum matches to the result of hashing the whole thing, and read the recorded number of entries from the main table, without even noticing that there is a gap in between), and that excitement blinded me.
Quoted 5 lines
I understand the risk but the list of offsets into the cache entries is pretty simple as well. I prefer the simplicity of a single TOC extension that gives us random access to the entire index rather than having to piece one together using multiple extensions. That model has its own set of risks and tradeoffs.
I thought that you are not using the "where does the series of extensions begin" information in the first place, no? That piece of information is useful independent of the usefulness of "index into the main table to list entries where the prefix-compression is reset". So if anything, I'd prefer the simplicity of introducing that "where does the series of extensions begin" that does not say anything else, and build on other things like ITOC as mere users of the mechanism.
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 51 lines · Junio C Hamano
Quoted 3 lines · Ben Peart
I have no thoughts or plans for changes in the future of IEOT (which I plan to rename ITOC). At this point in time, I can't even imagine what else we'd want as the index only contains cache entries, ...
Yeah, but the thing is that this is open source, and the imagination of the originator of the initial idea does not limit how the code and data structure evolves.
Back when I added the index extensions to the system, I didn't have perfect foresight, and I didn't have specific plans to add anything beyond "TREE" to optimize "write-tree" and "diff-index --cached".
In hindsight, I got one thing right and one thing wrong.
Even though I didn't have any plan to add a mandatory extension, I made the code to ignore optional ones and error out on mandatory ones if an index extension section is not understood. It turns out that later (in fact much later---the "TREE" extension dates back to April 2006, while "link" came in June 2014) we could add the split-index mode without having to worry about older versions of Git doing random wrong things when they see this new extension, thanks to that design decision. That went well.
On the other hand, I didn't think things through to realize that some operations may want to peek only the extensions without ever using the main table, that some other operations may want to read some extensions first before reading the main table, or more importantly, that these limitations would start mattering once Git becomes popular enough and starts housing a project tree with very many paths in the main table.
I really wish somebody had brought it up as a potential issue during the review---I would have defined the extended index format to have the simple extension at the end that points back to the tail end of the main table back then, and we wouldn't be having this back and forth now. But I was just too happy and excited that I have found a room to squeeze extension sections into the index file format without breaking existing implementations of Git (which checked the trailer checksum matches to the result of hashing the whole thing, and read the recorded number of entries from the main table, without even noticing that there is a gap in between), and that excitement blinded me.
Quoted 5 lines
I understand the risk but the list of offsets into the cache entries is pretty simple as well. I prefer the simplicity of a single TOC extension that gives us random access to the entire index rather than having to piece one together using multiple extensions. That model has its own set of risks and tradeoffs.
Quoted 9 lines
I thought that you are not using the "where does the series of extensions begin" information in the first place, no? That piece of information is useful independent of the usefulness of "index into the main table to list entries where the prefix-compression is reset". So if anything, I'd prefer the simplicity of introducing that "where does the series of extensions begin" that does not say anything else, and build on other things like ITOC as mere users of the mechanism.
OK. I'll call this new extension "EOIE" ("end of index entries"). Other than the standard header/footer, it will only contain a 32 bit offset to the beginning of the extension entries. I'll always write out that extension unless you would prefer it be behind a setting (if so, what do you want it called and what should the default be)? I won't add support in update-index for this extension.
Since the goal was to find a way to load the IEOT extension before the cache entries, I'll also refactor the extension reading loop into a function that takes a function pointer and add a preread_index_extension() function that can be passed in when that loop is run before the cache entries are loaded. When the loop is run again after the cache entries are loaded, it will pass in the existing read_index_extension() function. Extensions can then choose which function they want to be loaded in.
The code to read/write/use the IEOT to parallelize the cache entry loading will stay behind a config setting that defaults to false (at least for now). I'll stick with "core.fastindex" until someone can (please) propose a better name.
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 6 lines · Ben Peart
OK. I'll call this new extension "EOIE" ("end of index entries"). Other than the standard header/footer, it will only contain a 32 bit offset to the beginning of the extension entries. I'll always write out that extension unless you would prefer it be behind a setting (if so, what do you want it called and what should the default be)? I won't add support in update-index for this extension.
To make it robust, if I were doing this, I would at least add a checksum of some sort.
As each extension section consists of 4-byte extension type, 4-byte size, followed by that many bytes of the "meat" of the section, what I had in mind when I suggested this backpointer was something like:
"EOIE" <32-bit size> <32-bit offset> <20-byte hash>
where the size of the extension section is obviously 24-byte to cover the offset plus hash, and the hash is computed over extension types and their sizes (but not their contents---this is not about protecting against file corruption and not worth wasting the cycles for hashing) for all the extension sections this index file has (except for "EOIE" at the end, for obvious reasons). E.g. if we have "TREE" extension that is N-bytes long, "REUC" extension that is M-bytes long, followed by "EOIE", then the hash would be
SHA-1("TREE" + <binary representation of N> +
"REUC" + <binary representation of M>)
Then the reader would
- Seek back 32-byte from the trailer to ensure it sees "EOIE" followed by a correct size (24?)
- Jump to the offset and find 4-bytes that presumably is the type of the first extension, followed by its size.
- Feed these 8-bytes to the hash, skip that section based on its size (while making sure we won't run off the end of the file, which is a sign that we thought EOIE exists when there wasn't). Repeat this until we hit where we found "EOIE" (or we notice our mistake by overrunning it).
- Check the hash to make sure we got it right.
Quoted 9 lines
Since the goal was to find a way to load the IEOT extension before the cache entries, I'll also refactor the extension reading loop into a function that takes a function pointer and add a
preread_index_extension()function that can be passed in when that loop is run before the cache entries are loaded. When the loop is run again after the cache entries are loaded, it will pass in the existingread_index_extension()function. Extensions can then choose which function they want to be loaded in.
Quoted 4 lines
The code to read/write/use the IEOT to parallelize the cache entry loading will stay behind a config setting that defaults to false (at least for now). I'll stick with "core.fastindex" until someone can (please) propose a better name.
Sounds good.
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Further testing has revealed that switching from the regular heap to a refactored version of the mem_pool in fast-import.c produces similar gains as parallelizing do_index_load(). This appears to be a much simpler patch for similar gains so we will be pursuing that path.
Combining the two patches resulted in a further 25% reduction of do_index_load() but with index load times getting smaller, that only resulted in a 3% improvement in total git command time. Given the greater complexity of this patch, we've decided to put it on hold for now. We'll keep it in our back pocket in case we need it in the future.
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 4 lines · Ben Peart
Further testing has revealed that switching from the regular heap to a refactored version of the
mem_poolinfast-import.cproduces similar gains as parallelizingdo_index_load(). This appears to be a much simpler patch for similar gains so we will be pursuing that path.
That sounds like a pretty easy win for index entries, which tend to stick around in big clumps.
Out of curiosity, have you tried experimenting with any high-performance 3rd-party allocator libraries? I've often wondered if we could get a performance improvement from dropping in a new allocator, but was never able to measure any real benefit over glibc's ptmalloc2. The situation might be different on Windows, though (i.e., if the libc allocator isn't that great).
Most of the high-performance allocators are focused on concurrency, which usually isn't a big deal for git. But tcmalloc, at least, claims to be about 6x faster than glibc.
The reason I ask is that we could possibly get the same wins without writing a single line of code. And it could apply across the whole code-base, not just the index code. I don't know how close a general purpose allocator could come to a pooled implementation, though. You're inherently making a tradeoff with a pool in not being able to free individual entries.
-Peff
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 6 lines · Jeff King
Out of curiosity, have you tried experimenting with any high-performance 3rd-party allocator libraries? I've often wondered if we could get a performance improvement from dropping in a new allocator, but was never able to measure any real benefit over glibc's ptmalloc2. The situation might be different on Windows, though (i.e., if the libc allocator isn't that great).
Just linking with tcmalloc, like:
Makefile +1 -1
@@ -1018,7 +1018,7 @@ BUILTIN_OBJS += builtin/worktree.o BUILTIN_OBJS += builtin/write-tree.o GITLIBS = common-main.o $(LIB_FILE) $(XDIFF_LIB) -EXTLIBS = +EXTLIBS = -ltcmalloc GIT_USER_AGENT = git/$(GIT_VERSION)
seems to consistently show about 30% speedup on p0002:
Test HEAD^ HEAD
--------------------------------------------------------------------------------------
0002.1: read_cache/discard_cache 1000 times 0.19(0.18+0.01) 0.13(0.12+0.01) -31.6%
I don't think we really even need a patch for it. You should be able to just build with:
make EXT_LIBS=-ltcmalloc
I can't think of any real advantage to a Makefile knob except discoverability.
-Peff
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
In reply to Ben PeartQuoted 3 lines · Ben Peart
Further testing has revealed that switching from the regular heap to a refactored version of the
mem_poolinfast-import.cproduces similar gains as parallelizing do_index_load(). This appears to be a much simpler patch for similar gains so we will be pursuing that path.Combining the two patches resulted in a further 25% reduction of
do_index_load()but with index load times getting smaller, that only resulted in a 3% improvement in total git command time. Given the greater complexity of this patch, we've decided to put it on hold for now. We'll keep it in our back pocket in case we need it in the future.
Since you are withdrawing the 'bp/fastindex' branch, I won't send the patch to fix up the sparse warnings (unless you would like to see it before you put it in your back pocket). ;-)
ATB,
Ramsay Jones
Re: [PATCH v1 1/4] fastindex: speed up index load through parallelization
Quoted 5 lines · Ramsay Jones
Quoted 2 lines · Ben Peart
Further testing has revealed that switching from the regular heap to a refactored version of the
mem_poolinfast-import.cproduces similar gains as parallelizing do_index_load(). This appears to be a much simpler patch for similar gains so we will be pursuing that path.
Quoted 10 lines
Quoted 1 line
Combining the two patches resulted in a further 25% reduction of
do_index_load()but with index load times getting smaller, that only resulted in a 3% improvement in total git command time. Given the greater complexity of this patch, we've decided to put it on hold for now. We'll keep it in our back pocket in case we need it in the future.Since you are withdrawing the 'bp/fastindex' branch, I won't send the patch to fix up the sparse warnings (unless you would like to see it before you put it in your back pocket). ;-)
ATB,
Ramsay Jones
I'm happy to take your patches as I am keeping a copy just in case we need to revisit it.
I'm actually interested to see how Peff's suggestion to use tcmalloc helps because nearly 90% of the remaining execution time was spent waiting for a lock in malloc due to all contention from the multi-threading.
PATCH 2/4 update-index: add fastindex support to update-index
In reply to Ben PeartAdd support in update-index to manually add/remove the fastindex extension via --[no-]fastindex flags.
--- builtin/update-index.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
builtin/update-index.c +22 -0
@@ -917,6 +917,7 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)struct refresh_params refresh_args = {0 , &has_errors};int lock_error =0 ;int split_index =-1 ; +int fast_index =-1 ;struct lock_file lock_file =LOCK_INIT ;struct parse_opt_ctx_t ctx;strbuf_getline_fn getline_fn;
@@ -1008,6 +1009,8 @@ int cmd_update_index(int argc, const char **argv, const char *prefix) N_("test if the filesystem supports untracked cache"), UC_TEST),OPT_SET_INT (0 ,"force-untracked-cache" , &untracked_cache,N_ ("enable untracked cache without testing the filesystem" ),UC_FORCE ), +OPT_BOOL (0 ,"fastindex" , &fast_index, +N_ ("enable or disable fast index parsing" )), OPT_END() };
@@ -1146,6 +1149,25 @@ int cmd_update_index(int argc, const char **argv, const char *prefix)die ("BUG: bad untracked_cache value: %d" , untracked_cache); } +if (fast_index >0 ) { +if (git_config_get_fast_index () ==0 ) +warning (_ ("core.fastindex is unset; " +"set it if you really want to " +"enable fastindex" )); + core_fast_index =1 ; + active_cache_changed |=SOMETHING_CHANGED ; +report (_ ("fastindex enabled" )); + } +else if (!fast_index) { +if (git_config_get_fast_index () ==1 ) +warning (_ ("core.fastindex is set; " +"remove it if you really want to " +"disable fastindex" )); + core_fast_index =0 ; + active_cache_changed |=SOMETHING_CHANGED ; +report (_ ("fastindex disabled" )); + } +if (active_cache_changed) {if (newfd <0 ) {if (refresh_args.flags &REFRESH_QUIET )
Signature · 1 line
2.15.0.windows.1
PATCH 3/4 fastindex: add test tools and a test script
In reply to Ben PeartAdd a test utility (test-dump-fast-index) that dumps the contents of the IEOT to stdout. This enables checking the existance of the extension and the ability to parse and output its contents.
Add a test utility (test-fast-index) that loads the index using the fastindex logic then loads it using the regular logic and compares the results of the two. Any differences are reported and an error is returned.
Add a test script (t1800-fast-index.sh) to:
Test the ability to add/remove the fastindex index extension via update-index.
Verify that loading the index with and without the fastindex extension return the same results with both V2 and V4 indexes.
--- Makefile | 2 + t/helper/test-dump-fast-index.c | 68 +++++++++++++++++++++++++++++++++ t/helper/test-fast-index.c | 84 +++++++++++++++++++++++++++++++++++++++++ t/t1800-fast-index.sh | 55 +++++++++++++++++++++++++++ 4 files changed, 209 insertions(+) create mode 100644 t/helper/test-dump-fast-index.c create mode 100644 t/helper/test-fast-index.c create mode 100644 t/t1800-fast-index.sh
Makefile +2 -0
@@ -647,9 +647,11 @@ TEST_PROGRAMS_NEED_X += test-config 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-fast-index TEST_PROGRAMS_NEED_X += test-dump-split-index TEST_PROGRAMS_NEED_X += test-dump-untracked-cache TEST_PROGRAMS_NEED_X += test-fake-ssh +TEST_PROGRAMS_NEED_X += test-fast-index TEST_PROGRAMS_NEED_X += test-genrandom TEST_PROGRAMS_NEED_X += test-hashmap TEST_PROGRAMS_NEED_X += test-index-version
t/helper/test-dump-fast-index.c new file +68 -0
68 lines not on this page · raw message
t/helper/test-fast-index.c new file +84 -0
84 lines not on this page · raw message
t/t1800-fast-index.sh new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.15.0.windows.1
PATCH 4/4 fastindex: add documentation for the fastindex extension
In reply to Ben PeartThis includes the core.fastindex setting, the update-index additions, and the fastindex index extension.
--- Documentation/config.txt | 8 ++++++++ Documentation/git-update-index.txt | 11 +++++++++++ Documentation/technical/index-format.txt | 26 ++++++++++++++++++++++++++ 3 files changed, 45 insertions(+)
Documentation/config.txt +8 -0
@@ -868,6 +868,14 @@ relatively high IO latencies. When enabled, Git will do the index comparison to the filesystem data in parallel, allowing overlapping IO's. Defaults to true. +core.fastIndex:: + Enable parallel index loading ++ +This can speed up operations like 'git diff' and 'git status' especially +when the index is very large. When enabled, Git will do the index +loading from the on disk format to the in-memory format in parallel. +Defaults to false. + 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
Documentation/git-update-index.txt +11 -0
@@ -19,6 +19,7 @@ SYNOPSIS [--ignore-submodules] [--[no-]split-index] [--[no-|test-|force-]untracked-cache] + [--[no-]fastindex] [--really-refresh] [--unresolve] [--again | -g] [--info-only] [--index-info] [-z] [--stdin] [--index-version <n>]
@@ -201,6 +202,16 @@ will remove the intended effect of the option. `--untracked-cache` used to imply `--test-untracked-cache` but this option would enable the extension unconditionally. +--fastindex:: +--no-fastindex:: + Enable or disable the fast index feature. These options + take effect whatever the value of the `core.fastindex` + configuration variable (see linkgit:git-config[1]). But a warning + is emitted when the change goes against the configured value, as + the configured value will take effect next time the index is + read and this will remove the intended effect of the option. + + \--:: Do not interpret any more arguments as options.
Documentation/technical/index-format.txt +26 -0
@@ -295,3 +295,29 @@ The remaining data of each directory block is grouped by type: in the previous ewah bitmap. - One NUL. + +== Index Entry Offset Table + + The Index Entry Offset Table (IEOT) is used to help address the CPU + cost of loading the index by enabling multi-threading the process of + converting cache entries from the on-disk format to the in-memory format. + Because it must be able to be loaded before the variable length cache + entries and other index extensions, this extension must be written last. + The signature for this extension is { 'I', 'E', 'O', 'T' }. + + The extension consists of: + + - 32-bit version (currently 1) + + - A number of index offset entries each consisting of: + + - 32-bit offset from the begining of the file to the first cache entry + in this block of entries. + + - 32-bit count of cache entries in this block + + - 32-bit version (currently 1) + + - 32-bit size of the extension + + - 4-byte extension signature
Signature · 1 line
2.15.0.windows.1