# HG changeset patch # User Paul Boddie # Date 1253216130 -7200 # Node ID b97809e3386721adb355ae52f04561fb7ec88858 # Parent 67384f696d3c123ecb45d4ec01439a24a21c7ddb Replaced the partial Pyrex vint implementation with a cache. diff -r 67384f696d3c -r b97809e33867 iixr/data.py --- a/iixr/data.py Thu Sep 17 20:10:31 2009 +0200 +++ b/iixr/data.py Thu Sep 17 21:35:30 2009 +0200 @@ -18,24 +18,15 @@ with this program. If not, see . """ -try: - from vint import vint as _vint +vint_cache = {} - def vint(number): - - "Write 'number' as a variable-length integer." +def vint(number): - if number >= 0: - return _vint(number) - else: - raise ValueError, "Number %r is negative." % number + "Write 'number' as a variable-length integer." -except ImportError: - - def vint(number): - - "Write 'number' as a variable-length integer." - + try: + return vint_cache[number] + except KeyError: if number >= 0: # Special case: one byte containing a 7-bit number. @@ -61,4 +52,7 @@ else: raise ValueError, "Number %r is negative." % number +for i in xrange(0, 1024): + vint_cache[i] = vint(i) + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 67384f696d3c -r b97809e33867 setup.py --- a/setup.py Thu Sep 17 20:10:31 2009 +0200 +++ b/setup.py Thu Sep 17 21:35:30 2009 +0200 @@ -1,8 +1,6 @@ #! /usr/bin/env python -from distutils.core import setup, Extension - -vint = Extension("vint", ["vint.c"]) +from distutils.core import setup setup( name = "iixr", @@ -13,5 +11,4 @@ version = "0.1", py_modules = ["itermerge"], packages = ["iixr"], - ext_modules = [vint], ) diff -r 67384f696d3c -r b97809e33867 vint.pyx --- a/vint.pyx Thu Sep 17 20:10:31 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,46 +0,0 @@ -#!/usr/bin/env python - -""" -A variable-length integer encoding implementation in Pyrex. - -Copyright (C) 2009 Paul Boddie - -This program is free software; you can redistribute it and/or modify it under -the terms of the GNU General Public License as published by the Free Software -Foundation; either version 3 of the License, or (at your option) any later -version. - -This program is distributed in the hope that it will be useful, but WITHOUT ANY -WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -PARTICULAR PURPOSE. See the GNU General Public License for more details. - -You should have received a copy of the GNU General Public License along -with this program. If not, see . -""" - -cdef extern from "Python.h": - object PyString_FromStringAndSize(char *, int) - -def vint(int number): - - cdef char bytes[40] # NOTE: Arbitrary limit. - cdef int lsd, i - - if number < 128: - bytes[0] = number - s = PyString_FromStringAndSize(bytes, 1) - return s - - i = 0 - while number != 0: - lsd = number & 127 - number = number >> 7 - if number != 0: - lsd = lsd | 128 - bytes[i] = lsd - i = i + 1 - - s = PyString_FromStringAndSize(bytes, i) - return s - -# vim: tabstop=4 expandtab shiftwidth=4