view mercurial/base85.c @ 20528:1a9ebc83a74c

win32: improve the performance of win32.unlink() over CIFS Emulating POSIX unlink() behavior with os.rename() and os.unlink() is often slow especially over CIFS from Windows clients due to its protocol overhead. This patch changes win32.unlink() to try first an exclusive open with the Win32 delete-on-close flag, and if a sharing violation is detected, to fall back to the original emulation. This patch also removes a test with os.path.isdir() since we expect opening a directory shall fail as os.unlink() would. Example measurements (repeated 3-times after 1-time calibration): (Without this patch: hg update from null to default) 127 files updated, 0 files merged, 0 files removed, 0 files unresolved time: real 19.871 secs (user 0.328+0.000 sys 1.794+0.000) time: real 19.622 secs (user 0.312+0.000 sys 2.044+0.000) time: real 19.138 secs (user 0.250+0.000 sys 1.872+0.000) (Without this patch: hg update from default to null) 0 files updated, 0 files merged, 127 files removed, 0 files unresolved time: real 35.158 secs (user 0.156+0.000 sys 2.512+0.000) time: real 35.272 secs (user 0.250+0.000 sys 2.512+0.000) time: real 36.569 secs (user 0.203+0.000 sys 2.387+0.000) (With this patch: hg update from null to default) 127 files updated, 0 files merged, 0 files removed, 0 files unresolved time: real 17.893 secs (user 0.328+0.000 sys 1.700+0.000) time: real 18.512 secs (user 0.265+0.000 sys 1.529+0.000) time: real 20.238 secs (user 0.312+0.000 sys 1.685+0.000) (With this patch: hg update from default to null) 0 files updated, 0 files merged, 127 files removed, 0 files unresolved time: real 12.312 secs (user 0.250+0.000 sys 0.811+0.000) time: real 12.471 secs (user 0.156+0.000 sys 0.889+0.000) time: real 9.727 secs (user 0.125+0.000 sys 0.858+0.000)
author Kaz Nishimura <kazssym@vx68k.org>
date Thu, 17 Oct 2013 13:27:17 +0900
parents 19a915d43a68
children c1aefe57cf4e
line wrap: on
line source

/*
 base85 codec

 Copyright 2006 Brendan Cully <brendan@kublai.com>

 This software may be used and distributed according to the terms of
 the GNU General Public License, incorporated herein by reference.

 Largely based on git's implementation
*/

#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include "util.h"

static const char b85chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
	"abcdefghijklmnopqrstuvwxyz!#$%&()*+-;<=>?@^_`{|}~";
static char b85dec[256];

static void
b85prep(void)
{
	int i;

	memset(b85dec, 0, sizeof(b85dec));
	for (i = 0; i < sizeof(b85chars); i++)
		b85dec[(int)(b85chars[i])] = i + 1;
}

static PyObject *
b85encode(PyObject *self, PyObject *args)
{
	const unsigned char *text;
	PyObject *out;
	char *dst;
	Py_ssize_t len, olen, i;
	unsigned int acc, val, ch;
	int pad = 0;

	if (!PyArg_ParseTuple(args, "s#|i", &text, &len, &pad))
		return NULL;

	if (pad)
		olen = ((len + 3) / 4 * 5) - 3;
	else {
		olen = len % 4;
		if (olen)
			olen++;
		olen += len / 4 * 5;
	}
	if (!(out = PyBytes_FromStringAndSize(NULL, olen + 3)))
		return NULL;

	dst = PyBytes_AsString(out);

	while (len) {
		acc = 0;
		for (i = 24; i >= 0; i -= 8) {
			ch = *text++;
			acc |= ch << i;
			if (--len == 0)
				break;
		}
		for (i = 4; i >= 0; i--) {
			val = acc % 85;
			acc /= 85;
			dst[i] = b85chars[val];
		}
		dst += 5;
	}

	if (!pad)
		_PyBytes_Resize(&out, olen);

	return out;
}

static PyObject *
b85decode(PyObject *self, PyObject *args)
{
	PyObject *out;
	const char *text;
	char *dst;
	Py_ssize_t len, i, j, olen, cap;
	int c;
	unsigned int acc;

	if (!PyArg_ParseTuple(args, "s#", &text, &len))
		return NULL;

	olen = len / 5 * 4;
	i = len % 5;
	if (i)
		olen += i - 1;
	if (!(out = PyBytes_FromStringAndSize(NULL, olen)))
		return NULL;

	dst = PyBytes_AsString(out);

	i = 0;
	while (i < len)
	{
		acc = 0;
		cap = len - i - 1;
		if (cap > 4)
			cap = 4;
		for (j = 0; j < cap; i++, j++)
		{
			c = b85dec[(int)*text++] - 1;
			if (c < 0)
				return PyErr_Format(
					PyExc_ValueError,
					"bad base85 character at position %d",
					(int)i);
			acc = acc * 85 + c;
		}
		if (i++ < len)
		{
			c = b85dec[(int)*text++] - 1;
			if (c < 0)
				return PyErr_Format(
					PyExc_ValueError,
					"bad base85 character at position %d",
					(int)i);
			/* overflow detection: 0xffffffff == "|NsC0",
			 * "|NsC" == 0x03030303 */
			if (acc > 0x03030303 || (acc *= 85) > 0xffffffff - c)
				return PyErr_Format(
					PyExc_ValueError,
					"bad base85 sequence at position %d",
					(int)i);
			acc += c;
		}

		cap = olen < 4 ? olen : 4;
		olen -= cap;
		for (j = 0; j < 4 - cap; j++)
			acc *= 85;
		if (cap && cap < 4)
			acc += 0xffffff >> (cap - 1) * 8;
		for (j = 0; j < cap; j++)
		{
			acc = (acc << 8) | (acc >> 24);
			*dst++ = acc;
		}
	}

	return out;
}

static char base85_doc[] = "Base85 Data Encoding";

static PyMethodDef methods[] = {
	{"b85encode", b85encode, METH_VARARGS,
	 "Encode text in base85.\n\n"
	 "If the second parameter is true, pad the result to a multiple of "
	 "five characters.\n"},
	{"b85decode", b85decode, METH_VARARGS, "Decode base85 text.\n"},
	{NULL, NULL}
};

#ifdef IS_PY3K
static struct PyModuleDef base85_module = {
	PyModuleDef_HEAD_INIT,
	"base85",
	base85_doc,
	-1,
	methods
};

PyMODINIT_FUNC PyInit_base85(void)
{
	b85prep();

	return PyModule_Create(&base85_module);
}
#else
PyMODINIT_FUNC initbase85(void)
{
	Py_InitModule3("base85", methods, base85_doc);

	b85prep();
}
#endif