From f8c1c78df1a3118c604804f84556634bb6124b68 Mon Sep 17 00:00:00 2001 From: Siu Kwan Lam Date: Wed, 18 Dec 2013 12:49:24 -0600 Subject: [PATCH] Leaner --- llvm/__init__.py | 2 ++ llvm/core.py | 48 ++++++++++++++++++++++++++++++++++-------- llvm/tests/__init__.py | 12 ++++++++++- llvmpy/capsule.py | 5 +++-- llvmpy/gen/binding.py | 16 +++++++++++++- 5 files changed, 70 insertions(+), 13 deletions(-) diff --git a/llvm/__init__.py b/llvm/__init__.py index dc9852f..fe57700 100644 --- a/llvm/__init__.py +++ b/llvm/__init__.py @@ -9,6 +9,8 @@ version = extra.get_llvm_version() del extra class Wrapper(object): + __slots__ = '__ptr' + def __init__(self, ptr): assert ptr self.__ptr = ptr diff --git a/llvm/core.py b/llvm/core.py index 9fd5282..73e33fd 100644 --- a/llvm/core.py +++ b/llvm/core.py @@ -379,6 +379,7 @@ class Module(llvm.Wrapper): module_obj = Module.new('my_module') """ + __slots__ = '__weakref__' __cache = weakref.WeakValueDictionary() def __new__(cls, ptr): @@ -688,6 +689,7 @@ class Type(llvm.Wrapper): Use one of the static methods to create an instance. Example: ty = Type.double() """ + __slots__ = '__name__' _type_ = api.llvm.Type def __init__(self, ptr): @@ -880,6 +882,7 @@ class Type(llvm.Wrapper): class IntegerType(Type): """Represents an integer type.""" + __slots__ = () _type_ = api.llvm.IntegerType @property @@ -889,6 +892,7 @@ class IntegerType(Type): class FunctionType(Type): """Represents a function type.""" + __slots__ = () _type_ = api.llvm.FunctionType @property @@ -918,6 +922,7 @@ class FunctionType(Type): class StructType(Type): """Represents a structure type.""" _type_ = api.llvm.StructType + __slots__ = () @property def element_count(self): @@ -976,6 +981,7 @@ class StructType(Type): class ArrayType(Type): """Represents an array type.""" _type_ = api.llvm.ArrayType + __slots__ = () @property def element(self): @@ -987,6 +993,7 @@ class ArrayType(Type): class PointerType(Type): _type_ = api.llvm.PointerType + __slots__ = () @property def pointee(self): @@ -998,6 +1005,7 @@ class PointerType(Type): class VectorType(Type): _type_ = api.llvm.VectorType + __slots__ = () @property def element(self): @@ -1009,6 +1017,7 @@ class VectorType(Type): class Value(llvm.Wrapper): _type_ = api.llvm.Value + __slots__ = '__weakref__' def __init__(self, builder, ptr): assert builder is _ValueFactory @@ -1077,6 +1086,7 @@ class Value(llvm.Wrapper): class User(Value): _type_ = api.llvm.User + __slots__ = () @property def operand_count(self): @@ -1091,6 +1101,7 @@ class User(Value): class Constant(User): _type_ = api.llvm.Constant + __slots__ = () @staticmethod def null(ty): @@ -1276,6 +1287,7 @@ class Constant(User): class ConstantExpr(Constant): _type_ = api.llvm.ConstantExpr + __slots__ = () @property def opcode(self): @@ -1286,19 +1298,20 @@ class ConstantExpr(Constant): return self._ptr.getOpcodeName() class ConstantAggregateZero(Constant): - pass + __slots__ = () class ConstantDataArray(Constant): - pass + __slots__ = () class ConstantDataVector(Constant): - pass + __slots__ = () class ConstantInt(Constant): _type_ = api.llvm.ConstantInt + __slots__ = () @property def z_ext_value(self): @@ -1314,30 +1327,32 @@ class ConstantInt(Constant): class ConstantFP(Constant): - pass + __slots__ = () class ConstantArray(Constant): - pass + __slots__ = () class ConstantStruct(Constant): - pass + __slots__ = () class ConstantVector(Constant): - pass + __slots__ = () class ConstantPointerNull(Constant): - pass + __slots__ = () class UndefValue(Constant): - pass + __slots__ = () + class GlobalValue(Constant): _type_ = api.llvm.GlobalValue + __slots__ = () def _get_linkage(self): return self._ptr.getLinkage() @@ -1383,6 +1398,7 @@ class GlobalValue(Constant): class GlobalVariable(GlobalValue): _type_ = api.llvm.GlobalVariable + __slots__ = () @staticmethod def new(module, ty, name, addrspace=0): @@ -1443,6 +1459,7 @@ class GlobalVariable(GlobalValue): thread_local = property(_get_thread_local, _set_thread_local) class Argument(Value): + __slots__ = () _type_ = api.llvm.Argument _valid_attrs = frozenset([ATTR_BY_VAL, ATTR_NEST, ATTR_NO_ALIAS, ATTR_NO_CAPTURE, ATTR_STRUCT_RET]) @@ -1543,6 +1560,7 @@ class Argument(Value): return self._ptr.hasStructRetAttr() class Function(GlobalValue): + __slots__ = () _type_ = api.llvm.Function @staticmethod @@ -1681,6 +1699,7 @@ class Function(GlobalValue): #===----------------------------------------------------------------------=== class InlineAsm(Value): + __slots__ = () _type_ = api.llvm.InlineAsm @staticmethod @@ -1695,6 +1714,7 @@ class InlineAsm(Value): #===----------------------------------------------------------------------=== class MetaData(Value): + __slots__ = () _type_ = api.llvm.MDNode @staticmethod @@ -1751,6 +1771,7 @@ class MetaDataString(Value): class NamedMetaData(llvm.Wrapper): + __slots__ = () @staticmethod def get_or_insert(mod, name): @@ -1780,6 +1801,7 @@ class NamedMetaData(llvm.Wrapper): #===----------------------------------------------------------------------=== class Instruction(User): + __slots__ = () _type_ = api.llvm.Instruction @property @@ -1861,6 +1883,7 @@ class Instruction(User): class CallOrInvokeInstruction(Instruction): + __slots__ = () _type_ = api.llvm.CallInst, api.llvm.InvokeInst def _get_cc(self): @@ -1916,6 +1939,7 @@ class CallOrInvokeInstruction(Instruction): class PHINode(Instruction): + __slots__ = () _type_ = api.llvm.PHINode @property @@ -1933,6 +1957,7 @@ class PHINode(Instruction): class SwitchInstruction(Instruction): + __slots__ = () _type_ = api.llvm.SwitchInst def add_case(self, const, bblk): @@ -1940,6 +1965,7 @@ class SwitchInstruction(Instruction): class CompareInstruction(Instruction): + __slots__ = () _type_ = api.llvm.CmpInst @property @@ -1952,6 +1978,7 @@ class CompareInstruction(Instruction): class AllocaInstruction(Instruction): + __slots__ = () _type_ = api.llvm.AllocaInst @property @@ -1983,6 +2010,7 @@ class AllocaInstruction(Instruction): #===----------------------------------------------------------------------=== class BasicBlock(Value): + __slots__ = () _type_ = api.llvm.BasicBlock def insert_before(self, name): @@ -2009,6 +2037,7 @@ class BasicBlock(Value): class _ValueFactory(object): + __slots__ = () cache = weakref.WeakValueDictionary() # value ID -> class map @@ -2085,6 +2114,7 @@ _atomic_orderings = { } class Builder(llvm.Wrapper): + __slots__ = () @staticmethod def new(basic_block): diff --git a/llvm/tests/__init__.py b/llvm/tests/__init__.py index 483201b..2303204 100644 --- a/llvm/tests/__init__.py +++ b/llvm/tests/__init__.py @@ -37,7 +37,17 @@ def run(verbosity=1): if sys.version_info[:2] > (2, 6): kwargs['buffer'] = True runner = unittest.TextTestRunner(**kwargs) - testresult = runner.run(suite) + + try: + from guppy import hpy + except ImportError: + testresult = runner.run(suite) + else: + hp = hpy() + hp.setref() + testresult = runner.run(suite) + print(hp.heap()) + if testresult: # Run isolated tests diff --git a/llvmpy/capsule.py b/llvmpy/capsule.py index 7018ef8..5d0ead3 100644 --- a/llvmpy/capsule.py +++ b/llvmpy/capsule.py @@ -46,6 +46,7 @@ def _capsule_weakref_dtor(item): class Capsule(object): "Wraps PyCapsule so that we can build weakref of it." + __slots__ = 'pointer', 'capsule', 'name', '__weakref__' from ._capsule import check, getClassName, getName, getPointer @@ -97,7 +98,7 @@ class Capsule(object): return not (self == other) class WeakRef(ref): - pass + __slots__ = 'pointer', 'name', 'capsule' _addr2refct = defaultdict(lambda: 0) _capsule2weak = WeakKeyDictionary() @@ -126,7 +127,7 @@ def obtain_ownership(cap): if cls._has_dtor(): addr = cap.pointer name = cap.name - assert _addr2dtor[addr] is None + assert _addr2dtor[(name, addr)] is None _addr2dtor[(name, addr)] = cls._delete_ def has_ownership(cap): diff --git a/llvmpy/gen/binding.py b/llvmpy/gen/binding.py index 30e1614..5a6d8f4 100644 --- a/llvmpy/gen/binding.py +++ b/llvmpy/gen/binding.py @@ -1,5 +1,4 @@ import inspect, textwrap -import functools import codegen as cg import os @@ -8,10 +7,12 @@ namespaces = {} RESERVED = frozenset(['None']) + def makedir(directory): if not os.path.exists(directory): os.makedirs(directory) + class SubModule(object): def __init__(self): self.methods = [] @@ -293,6 +294,10 @@ class Class(SubModule, _Type): writer.println('@capsule.register_class("%s")' % self.fullname) with writer.block('class %(clsname)s(%(bases)s):' % locals()): writer.println('_llvm_type_ = "%s"' % self.fullname) + if self.bases: + writer.println('__slots__ = ()') + else: + writer.println('__slots__ = "__weakref__"') for enum in self.enums: enum.compile_py(writer) for meth in self.methods: @@ -399,6 +404,7 @@ class Enum(object): writer.println(fmt % locals()) writer.println() + class Method(object): _kind_ = 'meth' @@ -516,6 +522,7 @@ class Method(object): with writer.block('if len(%s) > %d:' % (unwrapped, i)): writer.release_ownership('%s[%d]' % (unwrapped, i)) + class CustomMethod(Method): def __init__(self, methodname, retty, *argtys): super(CustomMethod, self).__init__(retty, *argtys) @@ -594,6 +601,7 @@ class CustomFunction(Function): def fullname(self): return self.realname + class Destructor(Method): _kind_ = 'dtor' @@ -625,6 +633,7 @@ class Constructor(StaticMethod): ret = writer.declare(retty.fullname, stmt) return ret + class ref(_Type): def __init__(self, element): assert isinstance(element, Class), type(element) @@ -686,13 +695,16 @@ class ptr(_Type): return writer.pycapsule_new(val, self.element.capsule_name, self.element.fullname) + class ownedptr(ptr): pass + def const(ptr_or_ref): ptr_or_ref.const = True return ptr_or_ref + class cast(_Type): format = 'O' @@ -757,6 +769,7 @@ class CustomPythonMethod(object): for line in self.sourcelines: writer.println(line) + class CustomPythonStaticMethod(CustomPythonMethod): def compile_py(self, writer): writer.println('@staticmethod') @@ -845,6 +858,7 @@ class Attr(object): TARGETS_BUILT = os.environ.get('LLVM_TARGETS_BUILT', '').split() + def _parse_llvm_version(ver): import re m = re.compile(r'(\d+)\.(\d+)').match(ver)