[PATCH 2 of 2] dirs.c: speed up by storing number of direct children per dir

Siddharth Agarwal sid at less-broken.com
Tue May 12 13:00:14 CDT 2015


On 05/08/2015 04:12 PM, Martin von Zweigbergk wrote:
> # HG changeset patch
> # User Martin von Zweigbergk <martinvonz at google.com>
> # Date 1431122654 25200
> #      Fri May 08 15:04:14 2015 -0700
> # Node ID b89fb822dcc2bbd542662e1eebe964eb487ee1b9
> # Parent  6e2722576acc7bf9b9eee86179e02c9a80d06f08
> dirs.c: speed up by storing number of direct children per dir

These look fine to me, thanks.

- Siddharth

>
> The Python version of the dirs type stores only the number of direct
> children associated with each directory. That means that while adding
> a directory, it only has to walk backwards until it runs into a
> directory that is already in its map. The C version walks all the way
> to the top-most directory. By copying the Python version's clever
> trick to the C code, we can speed it up quite a bit.
>
> On the Firefox repo, perfdirs now runs in 0.031390, from 0.056518
> before the undoing Sid's optimization in the previous change, and
> 0.061835 before previous his optimization. More practically, it speeds
> up 'hg status nonexistent' on the Firefox repo from 0.176s to 0.155s.
>
> It's unclear why the C version did not have the same cleverness
> implemented from the start, especially given that they were both
> written by the same person (Bryan O'Sullivan) very close in time:
>
>   856960173630 (scmutil: add a dirs class, 2013-04-10)
>   02ee846b246a (scmutil: rewrite dirs in C, use if available, 2013-04-10)
>
> diff -r 6e2722576acc -r b89fb822dcc2 mercurial/dirs.c
> --- a/mercurial/dirs.c	Fri May 08 15:09:28 2015 -0700
> +++ b/mercurial/dirs.c	Fri May 08 15:04:14 2015 -0700
> @@ -69,7 +69,7 @@
>  		val = PyDict_GetItem(dirs, key);
>  		if (val != NULL) {
>  			PyInt_AS_LONG(val) += 1;
> -			continue;
> +			break;
>  		}
>  
>  		/* Force Python to not reuse a small shared int. */
> @@ -114,9 +114,11 @@
>  			goto bail;
>  		}
>  
> -		if (--PyInt_AS_LONG(val) <= 0 &&
> -		    PyDict_DelItem(dirs, key) == -1)
> -			goto bail;
> +		if (--PyInt_AS_LONG(val) <= 0) {
> +			if (PyDict_DelItem(dirs, key) == -1)
> +				goto bail;
> +		} else
> +			break;
>  		Py_CLEAR(key);
>  	}
>  	ret = 0;
> _______________________________________________
> Mercurial-devel mailing list
> Mercurial-devel at selenic.com
> http://selenic.com/mailman/listinfo/mercurial-devel



More information about the Mercurial-devel mailing list