[pypy-commit] pypy erase-raw-mem: (alex, fijal): commented out float support and fix the gc trace func
alex_gaynor
noreply at buildbot.pypy.org
Tue Mar 13 07:05:38 CET 2012
Author: Alex Gaynor <alex.gaynor at gmail.com>
Branch: erase-raw-mem
Changeset: r53419:6156ab467d03
Date: 2012-03-12 23:05 -0700
http://bitbucket.org/pypy/pypy/changeset/6156ab467d03/
Log: (alex, fijal): commented out float support and fix the gc trace func
diff --git a/pypy/rlib/rerased_raw.py b/pypy/rlib/rerased_raw.py
--- a/pypy/rlib/rerased_raw.py
+++ b/pypy/rlib/rerased_raw.py
@@ -14,6 +14,7 @@
INT = "i"
+FLOAT = "f"
INSTANCE = "o"
class UntypedStorage(object):
@@ -32,6 +33,17 @@
assert isinstance(v, int)
self.storage[idx] = v
+ def getfloat(self, idx):
+ assert self.shape[idx] == FLOAT
+ v = self.storage[idx]
+ assert isinstance(v, float)
+ return v
+
+ def setfloat(self, idx, f):
+ assert self.shape[idx] == FLOAT
+ assert isinstance(f, float)
+ self.storage[idx] = f
+
def getinstance(self, idx, cls):
obj = self.storage[idx]
assert self.shape[idx] == INSTANCE
@@ -68,6 +80,14 @@
self._check_idx(s_idx)
assert annmodel.SomeInteger().contains(s_v)
+ def method_getfloat(self, s_idx):
+ self._check_idx(s_idx)
+ return annmodel.SomeFloat()
+
+ def method_setfloat(self, s_idx, s_f):
+ self._check_idx(s_idx)
+ assert annmodel.SomeFloat().contains(s_f)
+
def method_getinstance(self, s_idx, s_cls):
self._check_idx(s_idx)
assert isinstance(s_cls, annmodel.SomePBC)
@@ -89,6 +109,8 @@
CUSTOMTRACEFUNC = lltype.FuncType([llmemory.Address, llmemory.Address],
llmemory.Address)
def trace_untypedstorage(obj_addr, prev):
+ # XXX: This has O(n**2) complexity because of the below loop, if we could
+ # do more arithmetic ops on addresses then it could be O(n).
shape_addr = obj_addr + llmemory.offsetof(UNTYPEDSTORAGE, "shape")
if not prev:
return shape_addr
@@ -96,17 +118,26 @@
length_offset = (llmemory.offsetof(STR, "chars") +
llmemory.arraylengthoffset(STR.chars))
length = (shape + length_offset).signed[0]
- if prev == shape_addr:
- i = 0
- while i < length:
- char = (shape + llmemory.offsetof(STR, "chars") +
- llmemory.itemoffsetof(STR.chars, 0) +
- (llmemory.sizeof(STR.chars.OF) * i)).char[0]
- if char == INSTANCE:
- return (obj_addr + llmemory.offsetof(UNTYPEDSTORAGE, "data") +
- llmemory.itemoffsetof(UNTYPEDSTORAGE.data, 0) +
- llmemory.sizeof(UNTYPEDSTORAGE.data.OF) * i)
+
+ seen_prev = prev == shape_addr
+ i = 0
+ while i < length:
+ data_ptr = (obj_addr + llmemory.offsetof(UNTYPEDSTORAGE, "data") +
+ llmemory.itemoffsetof(UNTYPEDSTORAGE.data, 0) +
+ llmemory.sizeof(UNTYPEDSTORAGE.data.OF) * i)
+
+ if not seen_prev:
+ if data_ptr == prev:
+ seen_prev = True
i += 1
+ continue
+
+ char = (shape + llmemory.offsetof(STR, "chars") +
+ llmemory.itemoffsetof(STR.chars, 0) +
+ (llmemory.sizeof(STR.chars.OF) * i)).char[0]
+ if char == INSTANCE:
+ return data_ptr
+ i += 1
return llmemory.NULL
trace_untypedstorage_ptr = llhelper(lltype.Ptr(CUSTOMTRACEFUNC), trace_untypedstorage)
@@ -143,6 +174,16 @@
v_addr = hop.genop("force_cast", [v_value], resulttype=llmemory.Address)
self._write_index(hop, v_addr)
+ def rtype_method_getfloat(self, hop):
+ v_value = self._read_index(hop)
+ return hop.genop("cast_adr_to_float", [v_value], resulttype=lltype.Float)
+
+ def rtype_method_setfloat(self, hop):
+ v_value = hop.inputarg(lltype.Float, arg=2)
+
+ v_addr = hop.genop("cast_float_to_adr", [v_value], resulttype=llmemory.Address)
+ self._write_index(hop, v_addr)
+
def rtype_method_getinstance(self, hop):
v_addr = self._read_index(hop)
return hop.genop("cast_adr_to_ptr", [v_addr], resulttype=hop.r_result.lowleveltype)
diff --git a/pypy/rlib/test/test_rerased_raw.py b/pypy/rlib/test/test_rerased_raw.py
--- a/pypy/rlib/test/test_rerased_raw.py
+++ b/pypy/rlib/test/test_rerased_raw.py
@@ -13,6 +13,12 @@
storage.setint(1, 5)
assert storage.getint(1) == 5
+def test_direct_float():
+ storage = rerased_raw.UntypedStorage("f")
+ storage.setfloat(0, 5.5)
+
+ assert storage.getfloat(0) == 5.5
+
def test_direct_instance():
class A(object):
def __init__(self, value):
@@ -47,6 +53,15 @@
res = self.interpret(f, [27])
assert res == 27
+ # def test_float(self):
+ # def f(x):
+ # storage = rerased_raw.UntypedStorage("f")
+ # storage.setfloat(0, x)
+ # return storage.getfloat(0)
+
+ # res = self.interpret(f, [12.3])
+ # assert res == 12.3
+
def test_exception_catching(self):
class A(object):
def __init__(self, v):
diff --git a/pypy/rpython/memory/test/test_gc.py b/pypy/rpython/memory/test/test_gc.py
--- a/pypy/rpython/memory/test/test_gc.py
+++ b/pypy/rpython/memory/test/test_gc.py
@@ -761,6 +761,27 @@
res = self.interpret(fn, [10])
assert res == 20
+ def test_untyped_storage_multipled_objects(self):
+ class A(object):
+ def __init__(self, v):
+ self.v = v
+
+ def fn():
+ s = UntypedStorage("oioio")
+ s.setinstance(0, A(1))
+ s.setint(1, 2)
+ s.setinstance(2, A(3))
+ s.setint(3, 4)
+ s.setinstance(4, A(5))
+ rgc.collect()
+ return (s.getinstance(0, A).v * 1 + s.getint(1) * 10 +
+ s.getinstance(2, A).v * 100 + s.getint(3) * 1000 +
+ s.getinstance(4, A).v * 10000)
+
+ res = self.interpret(fn, [])
+ assert res == 54321
+
+
from pypy.rlib.objectmodel import UnboxedValue
diff --git a/pypy/rpython/memory/test/test_transformed_gc.py b/pypy/rpython/memory/test/test_transformed_gc.py
--- a/pypy/rpython/memory/test/test_transformed_gc.py
+++ b/pypy/rpython/memory/test/test_transformed_gc.py
@@ -947,6 +947,29 @@
res = run([])
assert res == 20
+ def define_untyped_storage_multiple_objects(cls):
+ class A(object):
+ def __init__(self, v):
+ self.v = v
+
+ def fn():
+ s = UntypedStorage("oioio")
+ s.setinstance(0, A(1))
+ s.setint(1, 2)
+ s.setinstance(2, A(3))
+ s.setint(3, 4)
+ s.setinstance(4, A(5))
+ rgc.collect()
+ return (s.getinstance(0, A).v * 1 + s.getint(1) * 10 +
+ s.getinstance(2, A).v * 100 + s.getint(3) * 1000 +
+ s.getinstance(4, A).v * 10000)
+ return fn
+
+ def test_untyped_storage_multipled_objects(self):
+ run = self.runner("untyped_storage_multiple_objects")
+ res = run([])
+ assert res == 54321
+
# ________________________________________________________________
More information about the pypy-commit
mailing list