[PATCH 2 of 2] bdiff: don't check border condition in loop

Yuya Nishihara yuya at tcha.org
Sun Nov 20 05:39:35 EST 2016


On Sat, 12 Nov 2016 19:44:49 -0800, Gregory Szorc wrote:
> # HG changeset patch
> # User Gregory Szorc <gregory.szorc at gmail.com>
> # Date 1479000803 28800
> #      Sat Nov 12 17:33:23 2016 -0800
> # Node ID 812aa0657b1a5dc275be6bfd26c684cf9f937bfe
> # Parent  99388e747e8f99c7bdcfd2592ed8fc45e885ee55
> bdiff: don't check border condition in loop
> 
> This is pretty much a copy of d500ddae7494, just to a different loop.
> 
> The condition `p == plast` (`plast == a + len - 1`) was only true on
> the final iteration of the loop. So it was wasteful to check for it
> on every iteration. We decrease the iteration count by 1 and add an
> explicit check for `p == plast` after the loop.
> 
> Again, we see modest wins.
> 
> From the mozilla-unified repository:
> 
> $ perfbdiff -m 3041e4d59df2
> ! wall 0.035502 comb 0.040000 user 0.040000 sys 0.000000 (best of 100)
> ! wall 0.030480 comb 0.030000 user 0.030000 sys 0.000000 (best of 100)
> 
> $ perfbdiff 0e9928989e9c --alldata --count 100
> ! wall 4.097394 comb 4.100000 user 4.100000 sys 0.000000 (best of 3)
> ! wall 3.597798 comb 3.600000 user 3.600000 sys 0.000000 (best of 3)
> 
> This 2nd example throws a total of ~3.3GB of data at bdiff. This
> change increases the throughput from ~811 MB/s to ~924 MB/s.
> 
> diff --git a/mercurial/bdiff.c b/mercurial/bdiff.c
> --- a/mercurial/bdiff.c
> +++ b/mercurial/bdiff.c
> @@ -57,14 +57,19 @@ int bdiff_splitlines(const char *a, ssiz
>  
>  	/* build the line array and calculate hashes */
>  	hash = 0;
> -	for (p = a; p < a + len; p++) {
> +	for (p = a; p < plast; p++) {
>  		hash = HASH(hash, *p);
>  
> -		if (*p == '\n' || p == plast) {
> +		if (*p == '\n') {
>  			FINALIZELINE(l, hash, p, b);
>  		}
>  	}
>  
> +	if (p == plast) {
> +		hash = HASH(hash, *p);
> +		FINALIZELINE(l, hash, p, b);

Here we won't need to reset hash, l and b, so maybe we can do better without
the FINALIZELINE() macro.


More information about the Mercurial-devel mailing list