[PATCH] V4.4 of experiment for a simpler path encoding for hashed paths (for "fncache2")

Adrian Buehlmann adrian at cadifra.com
Thu Sep 27 10:21:32 CDT 2012


# HG changeset patch
# User Adrian Buehlmann <adrian at cadifra.com>
# Date 1348737919 -7200
# Node ID 01cac07595820e30f314d056a0d698dcd35cdc07
# Parent  68a3d36c2c29172e279fe6ccadc360fa7e7bad42
V4.4 of experiment for a simpler path encoding for hashed paths (for "fncache2")

Changes since V4.3:

- using integer compares for checking for 'aux' etc.
- reorganized using a switch on dirlen

TODO: Python implementation

diff --git a/mercurial/parsers.c b/mercurial/parsers.c
--- a/mercurial/parsers.c
+++ b/mercurial/parsers.c
@@ -1508,6 +1508,7 @@
 
 PyObject *encodedir(PyObject *self, PyObject *args);
 PyObject *pathencode(PyObject *self, PyObject *args);
+PyObject *cutdirs(PyObject *self, PyObject *args);
 
 static PyMethodDef methods[] = {
 	{"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"},
@@ -1516,6 +1517,7 @@
 	{"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
 	{"encodedir", encodedir, METH_VARARGS, "encodedir a path\n"},
 	{"pathencode", pathencode, METH_VARARGS, "fncache-encode a path\n"},
+	{"cutdirs", cutdirs, METH_VARARGS, "fncache-encode a path\n"},
 	{NULL, NULL}
 };
 
diff --git a/mercurial/pathencode.c b/mercurial/pathencode.c
--- a/mercurial/pathencode.c
+++ b/mercurial/pathencode.c
@@ -481,6 +481,86 @@
 
 static const Py_ssize_t maxstorepathlen = 120;
 
+static const char encchar[128] =
+	"~abcdefghijklmnopqrstuvwxyz{~}~~"
+	"~!~#$%&'()~+,-~~0123456789~;~=~~"
+	"@abcdefghijklmnopqrstuvwxyz[~]^_"
+	"`abcdefghijklmnopqrstuvwxyz{~}~~";
+
+/* this encoding folds */
+static inline char encodechar(char c)
+{
+	return encchar[0x7f & c];
+}
+
+static Py_ssize_t _cutdirs(char *dest, Py_ssize_t destlen, size_t destsize,
+			   const char *src, Py_ssize_t len)
+{
+	Py_ssize_t i = 0, spaceleft = maxstorepathlen - 40 + 1;
+	char c0, c1, c;
+	int cmp, dirlen = 0;
+
+	while (i < len && spaceleft > 0) {
+		if (src[i] == '/' || src[i] == '\0') {
+			charcopy(dest, &destlen, destsize, src[i++]);
+			spaceleft--;
+			dirlen = 0;
+		}
+		else {
+			switch (dirlen) {
+			case 8:
+				i++;
+				continue;
+			case 2:
+				c = encodechar(src[i++]);
+				cmp = c0 << 16 | c1 << 8 | c;
+				if (   cmp == 0x617578 /* aux */
+				    || cmp == 0x636f6e /* con */
+				    || cmp == 0x70726e /* prn */
+				    || cmp == 0x6e756c /* nul */
+				    || cmp == 0x636f6d /* com */
+				    || cmp == 0x6c7074 /* lpt */)
+					c = '~';
+				break;
+			default:
+				c = encodechar(src[i++]);
+				c0 = c1;
+				c1 = c;
+			}
+			charcopy(dest, &destlen, destsize, c);
+			spaceleft--; dirlen++;
+		}
+	}
+
+	return destlen;
+}
+
+PyObject *cutdirs(PyObject *self, PyObject *args)
+{
+	Py_ssize_t len, newlen;
+	PyObject *pathobj, *newobj;
+	char *path;
+
+	if (!PyArg_ParseTuple(args, "O:cutdirs", &pathobj))
+		return NULL;
+
+	if (PyString_AsStringAndSize(pathobj, &path, &len) == -1) {
+		PyErr_SetString(PyExc_TypeError, "expected a string");
+		return NULL;
+	}
+
+	newlen = len ? _cutdirs(NULL, 0, 0, path, len + 1) : 1;
+
+	newobj = PyString_FromStringAndSize(NULL, newlen);
+
+	if (newobj) {
+		PyString_GET_SIZE(newobj)--;
+		_cutdirs(PyString_AS_STRING(newobj), 0, newlen, path, len + 1);
+	}
+
+	return newobj;
+}
+
 /*
  * We currently implement only basic encoding.
  *
diff --git a/tests/test-hybridencode.py b/tests/test-hybridencode.py
--- a/tests/test-hybridencode.py
+++ b/tests/test-hybridencode.py
@@ -455,3 +455,54 @@
           'VWXYZ-1234567890-xxxxxxxxx-xxxxxxxxx-xxxxxxxx-xxxx'
           'xxxxx-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwww'
           'wwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww')
+
+from mercurial.parsers import cutdirs
+
+def cd(s):
+    print "cutdirs('%s')" % s.encode("string_escape")
+    print "        '%s'" % cutdirs(s).encode("string_escape")
+
+cd('data/a/c/p/n/c/l/foo.i')
+cd('data/au/co/pr/nu/co/lp/foo.i')
+cd('data/aux/con/prn/nul/com/lpt/foo.i')
+cd('data/xaux/xcon/xprn/xnul/xcom/xlpt/foo.i')
+cd('data/auxx/conx/prnx/nulx/comx/lptx/foo.i')
+cd('data/com0/com1/com9/lpt0/lpt1/lpt9/foo.i')
+print
+cd('data/.hello/ hello/hello./hello /foo.i')
+cd('data/..hello/  hello/hello../hello  /foo.i')
+cd('data/x.hello/x hello/hello.x/hello x/foo.i')
+cd('data/.hel..lo/ hel  lo/hel..lo./hel  lo /foo.i')
+print
+cd('data/abcdefgh/ijklmnop/qrstuvwx/yz012345/789/foo.i')
+cd('data/xabcdefgh/xijklmnop/xqrstuvwx/xyz012345/789/foo.i')
+cd('data/abcdefghx/ijklmnopx/qrstuvwxx/yz012345x/789/foo.i')
+cd('data/abcdefg.x/ijklmno x/qrstuvwxx/yz012345x/789/foo.i')
+print
+cd('data/ABCDEFGH/IJKLMNOP/QRSTUVWX/YZ012345/789/foo.i')
+print
+cd("data/01234567/89 !\"#%&/'()+,-.;/=[]^`{}/foo.i")
+print
+print "Windows reserved characters"
+cd('data/\\sl\\ash\\/:co:lon:/*st*ar*/?que?st?/foo.i')
+cd('data/"dqu"ot"/<le<ft</>rig>ht>/|pi|pe|/foo.i')
+print
+cd('data/01234567/01234567/01234567/01234567/01234567'
+   '/01234567/01234567/012345/fo/aux/bla.i')
+cd('data/01234567/01234567/01234567/01234567/01234567'
+   '/01234567/01234567/01234/fo/aux/bla.i')
+cd('data/01234567x/01234567x/01234567x/01234567x/01234567x'
+  '/01234567x/01234567x/01234567x/foo.i')
+print
+cd('data/the quick brown fox jumps over the lazy dog.i')
+print
+print "characters in ASCII code range 1..31"
+cd('data/\x01\x02\x03\x04\x05\x06\x07\x08'
+   '/\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10/foo.i')
+cd('data/\x11\x12\x13\x14\x15\x16\x17\x18'
+   '/\x19\x1a\x1b\x1c\x1d\x1e\x1f/foo.i')
+print
+cd('data/\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f/\x80\x81\x82/'
+    '\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff/a.i')
+print
+cd('data/\xae_\xae_\xae/bla.i')
diff --git a/tests/test-hybridencode.py.out b/tests/test-hybridencode.py.out
--- a/tests/test-hybridencode.py.out
+++ b/tests/test-hybridencode.py.out
@@ -486,3 +486,67 @@
 A = 'data/12345678/12345678/12345678/12345678/12345678/12345678/12345678/12345/-xxxxxxxxx-xxxxxxxxx-xxxxxxxxx-123456789-12.3456789-12345-ABCDEFGHIJKLMNOPRSTUVWXYZ-abcdefghjiklmnopqrstuvwxyz-ABCDEFGHIJKLMNOPRSTUVWXYZ-1234567890-xxxxxxxxx-xxxxxxxxx-xxxxxxxx-xxxxxxxxx-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww'
 B = 'dh/12345678/12345678/12345678/12345678/12345678/12345678/12345678/12345/28de8651e30eeb95f4b97edb7d12b281d3fb3ce0.3456789-12345-abcdefghijklmnoprstuvwxyz-abcdefghjiklmnopqrstuvwxyz-abcdefghijklmnoprstuvwxyz-1234567890-xxxxxxxxx-xxxxxxxxx-xxxxxxxx-xxxxxxxxx-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww-wwwwwwwww'
 
+cutdirs('data/a/c/p/n/c/l/foo.i')
+        'data/a/c/p/n/c/l/foo~i'
+cutdirs('data/au/co/pr/nu/co/lp/foo.i')
+        'data/au/co/pr/nu/co/lp/foo~i'
+cutdirs('data/aux/con/prn/nul/com/lpt/foo.i')
+        'data/au~/co~/pr~/nu~/co~/lp~/foo~i'
+cutdirs('data/xaux/xcon/xprn/xnul/xcom/xlpt/foo.i')
+        'data/xaux/xcon/xprn/xnul/xcom/xlpt/foo~i'
+cutdirs('data/auxx/conx/prnx/nulx/comx/lptx/foo.i')
+        'data/au~x/co~x/pr~x/nu~x/co~x/lp~x/foo~i'
+cutdirs('data/com0/com1/com9/lpt0/lpt1/lpt9/foo.i')
+        'data/co~0/co~1/co~9/lp~0/lp~1/lp~9/foo~i'
+
+cutdirs('data/.hello/ hello/hello./hello /foo.i')
+        'data/~hello/~hello/hello~/hello~/foo~i'
+cutdirs('data/..hello/  hello/hello../hello  /foo.i')
+        'data/~~hello/~~hello/hello~~/hello~~/foo~i'
+cutdirs('data/x.hello/x hello/hello.x/hello x/foo.i')
+        'data/x~hello/x~hello/hello~x/hello~x/foo~i'
+cutdirs('data/.hel..lo/ hel  lo/hel..lo./hel  lo /foo.i')
+        'data/~hel~~lo/~hel~~lo/hel~~lo~/hel~~lo~/foo~i'
+
+cutdirs('data/abcdefgh/ijklmnop/qrstuvwx/yz012345/789/foo.i')
+        'data/abcdefgh/ijklmnop/qrstuvwx/yz012345/789/foo~i'
+cutdirs('data/xabcdefgh/xijklmnop/xqrstuvwx/xyz012345/789/foo.i')
+        'data/xabcdefg/xijklmno/xqrstuvw/xyz01234/789/foo~i'
+cutdirs('data/abcdefghx/ijklmnopx/qrstuvwxx/yz012345x/789/foo.i')
+        'data/abcdefgh/ijklmnop/qrstuvwx/yz012345/789/foo~i'
+cutdirs('data/abcdefg.x/ijklmno x/qrstuvwxx/yz012345x/789/foo.i')
+        'data/abcdefg~/ijklmno~/qrstuvwx/yz012345/789/foo~i'
+
+cutdirs('data/ABCDEFGH/IJKLMNOP/QRSTUVWX/YZ012345/789/foo.i')
+        'data/abcdefgh/ijklmnop/qrstuvwx/yz012345/789/foo~i'
+
+cutdirs('data/01234567/89 !"#%&/\'()+,-.;/=[]^`{}/foo.i')
+        'data/01234567/89~!~#%&/\'()+,-~;/=[]^`{}/foo~i'
+
+Windows reserved characters
+cutdirs('data/\\sl\\ash\\/:co:lon:/*st*ar*/?que?st?/foo.i')
+        'data/~sl~ash~/~co~lon~/~st~ar~/~que~st~/foo~i'
+cutdirs('data/"dqu"ot"/<le<ft</>rig>ht>/|pi|pe|/foo.i')
+        'data/~dqu~ot~/~le~ft~/~rig~ht~/~pi~pe~/foo~i'
+
+cutdirs('data/01234567/01234567/01234567/01234567/01234567/01234567/01234567/012345/fo/aux/bla.i')
+        'data/01234567/01234567/01234567/01234567/01234567/01234567/01234567/012345/fo/au'
+cutdirs('data/01234567/01234567/01234567/01234567/01234567/01234567/01234567/01234/fo/aux/bla.i')
+        'data/01234567/01234567/01234567/01234567/01234567/01234567/01234567/01234/fo/au~'
+cutdirs('data/01234567x/01234567x/01234567x/01234567x/01234567x/01234567x/01234567x/01234567x/foo.i')
+        'data/01234567/01234567/01234567/01234567/01234567/01234567/01234567/01234567/foo'
+
+cutdirs('data/the quick brown fox jumps over the lazy dog.i')
+        'data/the~quic'
+
+characters in ASCII code range 1..31
+cutdirs('data/\x01\x02\x03\x04\x05\x06\x07\x08/\t\n\x0b\x0c\r\x0e\x0f\x10/foo.i')
+        'data/abcdefgh/ijklmnop/foo~i'
+cutdirs('data/\x11\x12\x13\x14\x15\x16\x17\x18/\x19\x1a\x1b\x1c\x1d\x1e\x1f/foo.i')
+        'data/qrstuvwx/yz{~}~~/foo~i'
+
+cutdirs('data/xyz{|}~\x7f/\x80\x81\x82/\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff/a.i')
+        'data/xyz{~}~~/~ab/xyz{~}~~/a~i'
+
+cutdirs('data/\xae_\xae_\xae/bla.i')
+        'data/~_~_~/bla~i'


More information about the Mercurial-devel mailing list