git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] diff compaction heuristic: favor shortest neighboring blank lines

From
Stefan Beller <sbeller@google.com>
Date
Jun 16, 2016, 21:06 UTC
Message-ID
<CAGZ79kYHO8q_CmePBxFUYxmhY6V_dS4M3djxCOrz5iJx_vFC-Q@mail.gmail.com>
In-Reply-To
<xmqqlh24516i.fsf@gitster.mtv.corp.google.com>
On Thu, Jun 16, 2016 at 1:27 PM, Junio C Hamano <gitster@pobox.com> wrote:
Show 9 quoted lines
>> ...
>> because there is less space between line start and {end, def bal}
>> than for {do_bal_stuff, common_ending}.
>
> I haven't thought this carefully yet, but would this equally work
> well for Python, where it does not have the "end" or does the lack
> of "end" pose a problem?  You'll still find "def bal" is a good
> boundary (but you cannot tell if it is the beginning or the end of a
> block, unless you understand the language), though.

Good point. I found a flaw in my implementation (as it doesn't match my mental model, not necessarily a bad thing)

We take the minimum of the two neighbors, i.e.
+                do_bal_stuff()
+
+        common_ending()
is preferrable to
+                do_bal_stuff()
+
+                common_ending()
and in python the example would look like:
    def foo():
        do_foo()
        common_thing()
+    def baz():
+        do_baz()
+
+        common_thing()
+
    def bar():
        do_bar()
        common_thing()
and breaking between
        common_thing()
    def bar():
is more favorable than between
        do_baz()
        common_thing()

because in the first former the count of white space in front of "def bar():" is smaller than for any of "do_baz()" and "common_thing()"

Show 10 quoted lines
>
>> +static unsigned int leading_blank(const char *line)
>> +{
>> +     unsigned int ret = 0;
>> +     while (*line) {
>> +             if (*line == '\t')
>> +                     ret += 8;
>
> This will be broken with a line with space-before-tab whitespace
> breakage, I suspect...

How so? We inspect each character on its own and then move on later by line++. (I am not seeing how this could cause trouble, so please help me?)

Going back to python, this may become a problem when you have a code like:
 def baz():
        do_baz()
        common_thing()
 def bar():
+       do_bal()
+
+       common_thing()
+
+def bar():
+
        do_bar()
        common_thing()

but this was fabricated with a typo (the first definition of bar should have been bal), (Also it doesn't worsen the diff, as it is same without the heuristic)

once that typo is fixed we get: (both with and without the heuristic)

        do_foo()
        common_thing()
 def baz():
        do_baz()
        common_thing()
+def bal():
+
+       do_bal()
+
+       common_thing()
+
 def bar():
        do_bar()
        common_thing()

Clearly it can also be intentional to have 2 methods with the same code for historical reasons, (even without the blank line after the function definition this produces the same result)

When playing around with various diffs I could not find a thing that this patch makes worse, it only fixes the actual issue. (I realized Peff actually attached a script to produce a bad diff, which is gone with this patch)

Thanks, Stefan

Previous: Junio C HamanoNext: Michael Haggerty
Message 3 of 15 in “diff compaction heuristic: favor shortest neighboring blank lines”
  1. diff compaction heuristic: favor shortest neighboring blank linesStefan Beller, Jun 16, 2016
  2. Junio C HamanoJun 16, 2016
  3. Stefan BellerJun 16, 2016
  4. Michael HaggertyJun 16, 2016
  5. Stefan BellerJun 16, 2016
  6. Jeff KingJun 17, 2016
  7. Stefan BellerJun 17, 2016
  8. Michael HaggertyJun 23, 2016
  9. Stefan BellerJun 23, 2016
  10. Junio C HamanoJun 23, 2016
  11. Michael HaggertyJun 23, 2016
  12. Michael HaggertyJun 30, 2016
  13. diff heuristics dramatically improved by considering line indentation and blank linesMichael Haggerty, Jul 1, 2016
  14. Junio C HamanoJul 1, 2016
  15. Jakub NarębskiJul 4, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.