Re: [PATCH 2alt/2] dir: match "attr" pathspec magic with correct paths

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Am 08.07.23 um 23:35 schrieb Junio C Hamano:
> René Scharfe <l.s.r@xxxxxx> writes:
>
>>>  	if (item->attr_match_nr &&
>>> -	    !match_pathspec_attrs(istate, name, namelen, item))
>>> +	    !match_pathspec_attrs(istate, name - prefix, namelen + prefix, item))
>>
>> match_pathspec_item() has only one caller, and it did the opposite, so
>> this is safe.  And a minimal fix like that is less likely to have side
>> effects.  Removing the trick will surely improve the code, though.  If
>> match_pathspec_item() needs the full name then we should pass it on,
>> and if the "prefix" offset needs to be added then it can happen right
>> there in that function.
>
> Yup.  I am inclined to take this version and then update the
> proposed log message to put less blame on the "common prefix"
> optimization in general.
>
> Thanks.
>
> Just for completeness, this is with an updated log message.
>
> ----- >8 --------- >8 --------- >8 --------- >8 --------- >8 -----
> The match_pathspec_item() function takes "prefix" value, allowing a
> caller to chop off the common leading prefix of pathspec pattern
> strings from the path and only use the remainder of the path to
> match the pathspec patterns (after chopping the same leading prefix
> of them, of course).
>
> This "common leading prefix" optimization has two main features:
>
>  * discard the entries in the in-core index that are outside of the
>    common leading prefix; if you are doing "ls-files one/a one/b",
>    we know all matches must be from "one/", so first the code
>    discards all entries outside the "one/" directory from the
>    in-core index.  This allows us to work on a smaller dataset.
>
>  * allow skipping the comparison of the leading bytes when matching
>    pathspec with path.  When "ls-files" finds the path "one/a/1" in
>    the in-core index given "one/a" and "one/b" as the pathspec,
>    knowing that common leading prefix "one/" was found lets the
>    pathspec matchinery not to bother comparing "one/" part, and
>    allows it to feed "a/1" down, as long as the pathspec element
>    "one/a" gets corresponding adjustment to "a".
>
> When the "attr" pathspec magic is in effect, however, the current
> code breaks down.
>
> The attributes, other than the ones that are built-in and the ones
> that come from the $GIT_DIR/info/attributes file and the top-level
> .gitattributes file, are lazily read from the filesystem on-demand,
> as we encounter each path and ask if it matches the pathspec.  For
> example, if you say "git ls-files "(attr:label)sub/" in a repository
> with a file "sub/file" that is given the 'label' attribute in
> "sub/.gitattributes":
>
>  * The common prefix optimization finds that "sub/" is the common
>    prefix and prunes the in-core index so that it has only entries
>    inside that directory.  This is desirable.
>
>  * The code then walks the in-core index, finds "sub/file", and
>    eventually asks do_match_pathspec() if it matches the given
>    pathspec.
>
>  * do_match_pathspec() calls match_pathspec_item() _after_ stripping
>    the common prefix "sub/" from the path, giving it "file", plus
>    the length of the common prefix (4-bytes), so that the pathspec
>    element "(attr:label)sub/" can be treated as if it were "(attr:label)".
>
> The last one is what breaks the match in the current code, as the
> pathspec subsystem ends up asking the attribute subsystem to find
> the attribute attached to the path "file".  We need to ask about the
> attributes on "sub/file" when calling match_pathspec_attrs(); this
> can be done by looking at "prefix" bytes before the beginning of
> "name", which is the same trick already used by another piece of the
> code in the same match_pathspec_item() function.
>
> Unfortunately this was not discovered so far because the code works
> with slightly different arguments, e.g.
>
>  $ git ls-files "(attr:label)sub"
>  $ git ls-files "(attr:label)sub/" "no/such/dir/"
>
> would have reported "sub/file" as a path with the 'label' attribute
> just fine, because neither would trigger the common prefix
> optimization.

Makes me wonder how important this optimization is, when this flaw went
unnoticed for ten years.

Using the latest main against on an old Chromium repository, because
it has lots of files:

   Benchmark 1: ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform
     Time (mean ± σ):      37.8 ms ±   0.2 ms    [User: 28.3 ms, System: 8.4 ms]
     Range (min … max):    37.4 ms …  38.7 ms    74 runs

   Benchmark 2: ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform missing
     Time (mean ± σ):      48.4 ms ±   0.5 ms    [User: 38.5 ms, System: 8.7 ms]
     Range (min … max):    47.8 ms …  51.9 ms    58 runs

     Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

   Summary
     ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform  ran
       1.28 ± 0.02 times faster than ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform missing

We can see that the shared prefix optimization helps noticeably, even
though the measurements are noisy.

With your big patch 2:

   Benchmark 1: ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform
     Time (mean ± σ):      38.0 ms ±   0.4 ms    [User: 28.3 ms, System: 8.5 ms]
     Range (min … max):    37.7 ms …  40.3 ms    72 runs

     Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

   Benchmark 2: ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform missing
     Time (mean ± σ):      48.5 ms ±   0.4 ms    [User: 38.5 ms, System: 8.8 ms]
     Range (min … max):    47.9 ms …  50.6 ms    58 runs

     Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.

   Summary
     ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform  ran
       1.28 ± 0.02 times faster than ./git -C ../chromium/src ls-files third_party/blink/web_tests/external third_party/blink/web_tests/platform missing

The difference to main is small enough to get lost in the noise.

The one-line fix is nice and surgical, but I like the other one more.
Gets rid of crusty underutilized code that doesn't even seem to make
a measurable difference.

René




[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux