# [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.

7 messages from 2010-07-05 to 2010-07-06. Participants: Dylan Reid, Michael J Gruber, Junio C Hamano.
Thread: https://gitlist.dev/t/24288

## Dylan Reid, 2010-07-05 06:06

Subject: [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.
Message-ID: <1278310017-24299-1-git-send-email-dgreid@gmail.com>
URL: https://gitlist.dev/e/1278310017-24299-1-git-send-email-dgreid%40gmail.com

```
Invert the order of the memcmp and flag check are done in xdl_recmatch,
as it makes the common case (there is no whitespace difference) faster.
It costs the case where lines are the same length and contain
whitespace differences, but the common case is more than 20% faster.

Signed-off-by: Dylan Reid <dgreid@gmail.com>
---
 xdiff/xutils.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index bc12f29..dc97a21 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -190,8 +190,10 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
 {
 	int i1, i2;
 
-	if (!(flags & XDF_WHITESPACE_FLAGS))
-		return s1 == s2 && !memcmp(l1, l2, s1);
+	if (s1 == s2 && !memcmp(l1, l2, s1))
+		return 1;
+	else if (!(flags & XDF_WHITESPACE_FLAGS))
+		return 0;
 
 	i1 = 0;
 	i2 = 0;
-- 
1.7.1

```

## Michael J Gruber, 2010-07-05 08:34

Subject: Re: [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.
Message-ID: <4C319931.4020603@drmicha.warpmail.net>
URL: https://gitlist.dev/e/4C319931.4020603%40drmicha.warpmail.net
In-Reply-To: <1278310017-24299-1-git-send-email-dgreid@gmail.com>

```
Dylan Reid venit, vidit, dixit 05.07.2010 08:06:
> Invert the order of the memcmp and flag check are done in xdl_recmatch,

I was wondering whether you describe old (before patch) or new (after
patch) here. Maybe one of the following is clearer:

"Invert the order of the memcmp and flag check which are done in
xdl_recmatch,"

"Invert the order of the memcmp and flag check in xdl_recmatch,"

> as it makes the common case (there is no whitespace difference) faster.
> It costs the case where lines are the same length and contain
> whitespace differences, but the common case is more than 20% faster.
> 
> Signed-off-by: Dylan Reid <dgreid@gmail.com>
> ---
>  xdiff/xutils.c |    6 ++++--
>  1 files changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/xdiff/xutils.c b/xdiff/xutils.c
> index bc12f29..dc97a21 100644
> --- a/xdiff/xutils.c
> +++ b/xdiff/xutils.c
> @@ -190,8 +190,10 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
>  {
>  	int i1, i2;
>  
> -	if (!(flags & XDF_WHITESPACE_FLAGS))
> -		return s1 == s2 && !memcmp(l1, l2, s1);
> +	if (s1 == s2 && !memcmp(l1, l2, s1))
> +		return 1;
> +	else if (!(flags & XDF_WHITESPACE_FLAGS))

You can do without the "else" here.

> +		return 0;
>  
>  	i1 = 0;
>  	i2 = 0;

BTW: How did you find this? Are you profiling parts of git?

Michael

```

## Dylan Reid, 2010-07-05 12:34

Subject: Re: [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.
Message-ID: <AANLkTinmdeTSRVCTblnGrG5lGpNKCiYIb8BO8jMhajXW@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTinmdeTSRVCTblnGrG5lGpNKCiYIb8BO8jMhajXW%40mail.gmail.com
In-Reply-To: <4C319931.4020603@drmicha.warpmail.net>

```
On Mon, Jul 5, 2010 at 4:34 AM, Michael J Gruber
<git@drmicha.warpmail.net> wrote:
> Dylan Reid venit, vidit, dixit 05.07.2010 08:06:
>> Invert the order of the memcmp and flag check are done in xdl_recmatch,
>
> I was wondering whether you describe old (before patch) or new (after
> patch) here. Maybe one of the following is clearer:
>
> "Invert the order of the memcmp and flag check which are done in
> xdl_recmatch,"
>
> "Invert the order of the memcmp and flag check in xdl_recmatch,"
>

I'll make that more clear.

>> +     if (s1 == s2 && !memcmp(l1, l2, s1))
>> +             return 1;
>> +     else if (!(flags & XDF_WHITESPACE_FLAGS))
>
> You can do without the "else" here.

sure can, will do.

>> +             return 0;
>>
>>       i1 = 0;
>>       i2 = 0;
>
> BTW: How did you find this? Are you profiling parts of git?
>
> Michael
>

I was looking to add an unrelated feature and just happened to notice
this.  It was a really cheap change for a good speedup.

Thanks,

Dylan

```

## Dylan Reid, 2010-07-05 13:00

Subject: [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.
Message-ID: <1278334842-9701-1-git-send-email-dgreid@gmail.com>
URL: https://gitlist.dev/e/1278334842-9701-1-git-send-email-dgreid%40gmail.com
In-Reply-To: <1278310017-24299-1-git-send-email-dgreid@gmail.com>

```
In xdl_recmatch, do the memcmp to check if the two lines are equal before
checking if whitespace flags are set.  If the lines are identicle, then
there is no need to check if they differ only in whitespace.
This makes the common case (there is no whitespace difference) faster.
It costs the case where lines are the same length and contain
whitespace differences, but the common case is more than 20% faster.

Signed-off-by: Dylan Reid <dgreid@gmail.com>
---
 xdiff/xutils.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index bc12f29..22f9bd6 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -190,8 +190,10 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
 {
 	int i1, i2;
 
+	if (s1 == s2 && !memcmp(l1, l2, s1))
+		return 1;
 	if (!(flags & XDF_WHITESPACE_FLAGS))
-		return s1 == s2 && !memcmp(l1, l2, s1);
+		return 0;
 
 	i1 = 0;
 	i2 = 0;
-- 
1.7.1

```

## Junio C Hamano, 2010-07-06 02:36

Subject: Re: [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.
Message-ID: <7vsk3x72wb.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vsk3x72wb.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1278334842-9701-1-git-send-email-dgreid@gmail.com>

```
Dylan Reid <dgreid@gmail.com> writes:

> In xdl_recmatch, do the memcmp to check if the two lines are equal before
> checking if whitespace flags are set.  If the lines are identicle, then

"identical"?

> there is no need to check if they differ only in whitespace.
> This makes the common case (there is no whitespace difference) faster.
> It costs the case where lines are the same length and contain
> whitespace differences, but the common case is more than 20% faster.

"more than 20% faster" based on what dataset and benchmark?

>
> Signed-off-by: Dylan Reid <dgreid@gmail.com>
> ---
>  xdiff/xutils.c |    4 +++-
>  1 files changed, 3 insertions(+), 1 deletions(-)
>
> diff --git a/xdiff/xutils.c b/xdiff/xutils.c
> index bc12f29..22f9bd6 100644
> --- a/xdiff/xutils.c
> +++ b/xdiff/xutils.c
> @@ -190,8 +190,10 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
>  {
>  	int i1, i2;
>  
> +	if (s1 == s2 && !memcmp(l1, l2, s1))
> +		return 1;
>  	if (!(flags & XDF_WHITESPACE_FLAGS))
> -		return s1 == s2 && !memcmp(l1, l2, s1);
> +		return 0;
>  
>  	i1 = 0;
>  	i2 = 0;
> -- 
> 1.7.1
>
> --
> To unsubscribe from this list: send the line "unsubscribe git" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

```

## Dylan Reid, 2010-07-06 03:04

Subject: Re: [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.
Message-ID: <AANLkTilkML1edHz6JSj7w3u60oFt3Va-5Z2ZBWVphxVI@mail.gmail.com>
URL: https://gitlist.dev/e/AANLkTilkML1edHz6JSj7w3u60oFt3Va-5Z2ZBWVphxVI%40mail.gmail.com
In-Reply-To: <7vsk3x72wb.fsf@alter.siamese.dyndns.org>

```
On Mon, Jul 5, 2010 at 10:36 PM, Junio C Hamano <gitster@pobox.com> wrote:
> Dylan Reid <dgreid@gmail.com> writes:
>
>> In xdl_recmatch, do the memcmp to check if the two lines are equal before
>> checking if whitespace flags are set.  If the lines are identicle, then
>
> "identical"?

That's exaclty what I meant.  Correct patch sent.

>
>> there is no need to check if they differ only in whitespace.
>> This makes the common case (there is no whitespace difference) faster.
>> It costs the case where lines are the same length and contain
>> whitespace differences, but the common case is more than 20% faster.
>
> "more than 20% faster" based on what dataset and benchmark?
>

I benchmarked it with some of the bigger files from Linux.  The
results were consistenly > 20% faster
across different file sizes.  I tested it by simply running the
command a few times then running it with
my local build a few times to see if I had achieved any speedup.

~/linux-2.6(119)$ time git blame --incremental -w
./sound/pci/hda/patch_realtek.c > /dev/null

real    0m8.166s
user    0m8.063s
sys     0m0.090s
~/linux-2.6(120)$ time git blame --incremental -w
./sound/pci/hda/patch_realtek.c > /dev/null

real    0m8.010s
user    0m7.866s
sys     0m0.137s
~/linux-2.6(121)$ time ~/work/git/git blame --incremental -w
./sound/pci/hda/patch_realtek.c > /dev/null

real    0m6.115s
user    0m5.986s
sys     0m0.123s
~/linux-2.6(122)$ time ~/work/git/git blame --incremental -w
./sound/pci/hda/patch_realtek.c > /dev/null

real    0m6.119s
user    0m5.986s
sys     0m0.127s


Thanks for taking the time to read the patch.

Dylan

```

## Dylan Reid, 2010-07-06 03:11

Subject: [PATCH] xdiff: optimise for no whitespace difference when ignoring whitespace.
Message-ID: <1278385877-13902-1-git-send-email-dgreid@gmail.com>
URL: https://gitlist.dev/e/1278385877-13902-1-git-send-email-dgreid%40gmail.com
In-Reply-To: <1278310017-24299-1-git-send-email-dgreid@gmail.com>

```
In xdl_recmatch, do the memcmp to check if the two lines are equal before
checking if whitespace flags are set.  If the lines are identical, then
there is no need to check if they differ only in whitespace.
This makes the common case (there is no whitespace difference) faster.
It costs the case where lines are the same length and contain
whitespace differences, but the common case is more than 20% faster.

Signed-off-by: Dylan Reid <dgreid@gmail.com>
---
 xdiff/xutils.c |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)

diff --git a/xdiff/xutils.c b/xdiff/xutils.c
index bc12f29..22f9bd6 100644
--- a/xdiff/xutils.c
+++ b/xdiff/xutils.c
@@ -190,8 +190,10 @@ int xdl_recmatch(const char *l1, long s1, const char *l2, long s2, long flags)
 {
 	int i1, i2;
 
+	if (s1 == s2 && !memcmp(l1, l2, s1))
+		return 1;
 	if (!(flags & XDF_WHITESPACE_FLAGS))
-		return s1 == s2 && !memcmp(l1, l2, s1);
+		return 0;
 
 	i1 = 0;
 	i2 = 0;
-- 
1.7.1

```
