[pypy-commit] pypy faster-rstruct-2: kill rlib/strstorage.py since it is no longer needed: the only place where it was still used was in the slow-path of float/double unpacking in runpck.py: replace it with the equivalent functionality exposed by StringBuffer.typed_read
antocuni
pypy.commits at gmail.com
Tue May 9 15:45:15 EDT 2017
Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: faster-rstruct-2
Changeset: r91216:7a021235c44a
Date: 2017-05-09 21:44 +0200
http://bitbucket.org/pypy/pypy/changeset/7a021235c44a/
Log: kill rlib/strstorage.py since it is no longer needed: the only place
where it was still used was in the slow-path of float/double
unpacking in runpck.py: replace it with the equivalent functionality
exposed by StringBuffer.typed_read
diff --git a/rpython/jit/metainterp/test/test_strstorage.py b/rpython/jit/metainterp/test/test_strstorage.py
deleted file mode 100644
--- a/rpython/jit/metainterp/test/test_strstorage.py
+++ /dev/null
@@ -1,53 +0,0 @@
-import py
-import sys
-import struct
-from rpython.rtyper.lltypesystem import lltype, rffi
-from rpython.rlib.strstorage import str_storage_getitem
-from rpython.rlib.test.test_strstorage import BaseStrStorageTest
-from rpython.jit.codewriter import longlong
-from rpython.jit.metainterp.history import getkind
-from rpython.jit.metainterp.test.support import LLJitMixin
-
-class TestStrStorage(BaseStrStorageTest, LLJitMixin):
-
- # for the individual tests see
- # ====> ../../../rlib/test/test_strstorage.py
-
- def str_storage_getitem(self, TYPE, buf, offset):
- def f():
- return str_storage_getitem(TYPE, buf, offset)
- res = self.interp_operations(f, [], supports_singlefloats=True)
- #
- kind = getkind(TYPE)[0] # 'i' or 'f'
- self.check_operations_history({'gc_load_indexed_%s' % kind: 1,
- 'finish': 1})
- #
- if TYPE == lltype.SingleFloat:
- # interp_operations returns the int version of r_singlefloat, but
- # our tests expects to receive an r_singlefloat: let's convert it
- # back!
- return longlong.int2singlefloat(res)
- return res
-
- #def str_storage_supported(self, TYPE):
- # py.test.skip('this is not a JIT test')
-
- def test_force_virtual_str_storage(self):
- byteorder = sys.byteorder
- size = rffi.sizeof(lltype.Signed)
- def f(val):
- if byteorder == 'little':
- x = chr(val) + '\x00'*(size-1)
- else:
- x = '\x00'*(size-1) + chr(val)
- return str_storage_getitem(lltype.Signed, x, 0)
- res = self.interp_operations(f, [42], supports_singlefloats=True)
- assert res == 42
- self.check_operations_history({
- 'newstr': 1, # str forcing
- 'strsetitem': 1, # str forcing
- 'call_pure_r': 1, # str forcing (copystrcontent)
- 'guard_no_exception': 1, # str forcing
- 'gc_load_indexed_i': 1, # str_storage_getitem
- 'finish': 1
- })
diff --git a/rpython/rlib/rstruct/standardfmttable.py b/rpython/rlib/rstruct/standardfmttable.py
--- a/rpython/rlib/rstruct/standardfmttable.py
+++ b/rpython/rlib/rstruct/standardfmttable.py
@@ -12,7 +12,8 @@
from rpython.rlib.rstruct import ieee
from rpython.rlib.rstruct.error import StructError, StructOverflowError
from rpython.rlib.unroll import unrolling_iterable
-from rpython.rlib.strstorage import str_storage_getitem
+from rpython.rlib.buffer import StringBuffer
+#from rpython.rlib.strstorage import str_storage_getitem
from rpython.rlib import rarithmetic
from rpython.rlib.buffer import CannotRead
from rpython.rtyper.lltypesystem import rffi
@@ -210,9 +211,12 @@
# fast path
val = unpack_fastpath(TYPE)(fmtiter)
except CannotRead:
- # slow path, take the slice
+ # slow path: we should arrive here only if we could not unpack
+ # because of alignment issues. So we copy the slice into a new
+ # string, which is guaranteed to be properly aligned, and read the
+ # float/double from there
input = fmtiter.read(size)
- val = str_storage_getitem(TYPE, input, 0)
+ val = StringBuffer(input).typed_read(TYPE, 0)
fmtiter.appendobj(float(val))
return unpack_ieee
diff --git a/rpython/rlib/strstorage.py b/rpython/rlib/strstorage.py
deleted file mode 100644
--- a/rpython/rlib/strstorage.py
+++ /dev/null
@@ -1,39 +0,0 @@
-# Support for str_storage: i.e., reading primitive types out of RPython string
-#
-# There are various possible ways to implement it, however not all of them are
-# easily supported by the JIT:
-#
-# 1. use _get_raw_str_buf and cast the chars buffer to RAW_STORAGE_PTR: this
-# works well without the JIT, but the cast to RAW_STORAGE_PTR needs to
-# happen inside a short "no GC" section (like the one in
-# rstr.py:copy_string_contents), which has no chance to work during
-# tracing
-#
-# 2. cast rpy_string to a GcStruct which has the very
-# same layout, with the only difference that its 'chars' field is no
-# longer an Array(Char) but e.e. an Array(Signed). Then, we just need to
-# read the appropriate index into the array. To support this solution,
-# the JIT's optimizer needed a few workarounds. This was removed.
-#
-# 3. use the newly introduced 'llop.gc_load_indexed'.
-#
-
-
-from rpython.rtyper.lltypesystem import lltype, llmemory
-from rpython.rtyper.lltypesystem.lloperation import llop
-from rpython.rtyper.lltypesystem.rstr import STR
-from rpython.rtyper.annlowlevel import llstr
-from rpython.rlib.objectmodel import specialize
-
-
- at specialize.ll()
-def str_storage_getitem(TP, s, byte_offset):
- # WARNING: the 'byte_offset' is, as its name says, measured in bytes;
- # however, it should be aligned for TP, otherwise on some platforms this
- # code will crash!
- lls = llstr(s)
- base_ofs = (llmemory.offsetof(STR, 'chars') +
- llmemory.itemoffsetof(STR.chars, 0))
- scale_factor = llmemory.sizeof(lltype.Char)
- return llop.gc_load_indexed(TP, lls, byte_offset,
- scale_factor, base_ofs)
diff --git a/rpython/rlib/test/test_strstorage.py b/rpython/rlib/test/test_strstorage.py
deleted file mode 100644
--- a/rpython/rlib/test/test_strstorage.py
+++ /dev/null
@@ -1,96 +0,0 @@
-import py
-import sys
-import struct
-from rpython.rtyper.lltypesystem import lltype, rffi
-from rpython.rlib.strstorage import str_storage_getitem
-from rpython.rlib.rarithmetic import r_singlefloat
-from rpython.rtyper.test.tool import BaseRtypingTest
-
-IS_32BIT = (sys.maxint == 2147483647)
-
-class BaseStrStorageTest:
-
- ## def test_str_getitem_supported(self):
- ## if IS_32BIT:
- ## expected = False
- ## else:
- ## expected = True
- ## #
- ## assert self.str_storage_supported(rffi.LONGLONG) == expected
- ## assert self.str_storage_supported(rffi.DOUBLE) == expected
-
- def test_signed(self):
- buf = struct.pack('@ll', 42, 43)
- size = struct.calcsize('@l')
- assert self.str_storage_getitem(lltype.Signed, buf, 0) == 42
- assert self.str_storage_getitem(lltype.Signed, buf, size) == 43
-
- def test_short(self):
- buf = struct.pack('@hh', 42, 43)
- size = struct.calcsize('@h')
- x = self.str_storage_getitem(rffi.SHORT, buf, 0)
- assert int(x) == 42
- x = self.str_storage_getitem(rffi.SHORT, buf, size)
- assert int(x) == 43
-
- def test_float(self):
- ## if not str_storage_supported(lltype.Float):
- ## py.test.skip('str_storage_getitem(lltype.Float) not supported on this machine')
- buf = struct.pack('@dd', 12.3, 45.6)
- size = struct.calcsize('@d')
- assert self.str_storage_getitem(lltype.Float, buf, 0) == 12.3
- assert self.str_storage_getitem(lltype.Float, buf, size) == 45.6
-
- def test_singlefloat(self):
- buf = struct.pack('@ff', 12.3, 45.6)
- size = struct.calcsize('@f')
- x = self.str_storage_getitem(lltype.SingleFloat, buf, 0)
- assert x == r_singlefloat(12.3)
- x = self.str_storage_getitem(lltype.SingleFloat, buf, size)
- assert x == r_singlefloat(45.6)
-
-
-class TestDirect(BaseStrStorageTest):
-
- ## def str_storage_supported(self, TYPE):
- ## return str_storage_supported(TYPE)
-
- def str_storage_getitem(self, TYPE, buf, offset):
- return str_storage_getitem(TYPE, buf, offset)
-
-class TestRTyping(BaseStrStorageTest, BaseRtypingTest):
-
- ## def str_storage_supported(self, TYPE):
- ## def fn():
- ## return str_storage_supported(TYPE)
- ## return self.interpret(fn, [])
-
- def str_storage_getitem(self, TYPE, buf, offset):
- def fn(offset):
- return str_storage_getitem(TYPE, buf, offset)
- return self.interpret(fn, [offset])
-
-
-class TestCompiled(BaseStrStorageTest):
- cache = {}
-
- def str_storage_getitem(self, TYPE, buf, offset):
- if TYPE not in self.cache:
- from rpython.translator.c.test.test_genc import compile
-
- assert isinstance(TYPE, lltype.Primitive)
- if TYPE in (lltype.Float, lltype.SingleFloat):
- TARGET_TYPE = lltype.Float
- else:
- TARGET_TYPE = lltype.Signed
-
- def llf(buf, offset):
- x = str_storage_getitem(TYPE, buf, offset)
- return lltype.cast_primitive(TARGET_TYPE, x)
-
- fn = compile(llf, [str, int])
- self.cache[TYPE] = fn
- #
- fn = self.cache[TYPE]
- x = fn(buf, offset)
- return lltype.cast_primitive(TYPE, x)
More information about the pypy-commit
mailing list