Author: Antonio Cuni <[email protected]>
Branch: faster-rstruct-2
Changeset: r91243:582e284508de
Date: 2017-05-11 11:25 +0200
http://bitbucket.org/pypy/pypy/changeset/582e284508de/
Log: we cannot malloc(STR, zero=True) because the GC does not support it.
So, simplify a bit the code, remove the needs_zeros flag and always
write zeros when it's needed
diff --git a/pypy/module/struct/formatiterator.py
b/pypy/module/struct/formatiterator.py
--- a/pypy/module/struct/formatiterator.py
+++ b/pypy/module/struct/formatiterator.py
@@ -16,7 +16,6 @@
self.args_index = 0
self.pos = 0
self.result = MutableStringBuffer(size)
- self.needs_zeros = False # MutableStringBuffer is already 0-inizialized
def advance(self, count):
self.pos += count
@@ -37,6 +36,7 @@
@jit.unroll_safe
def align(self, mask):
pad = (-self.pos) & mask
+ self.result.setzeros(self.pos, pad)
self.advance(pad)
def finished(self):
diff --git a/rpython/rlib/mutbuffer.py b/rpython/rlib/mutbuffer.py
--- a/rpython/rlib/mutbuffer.py
+++ b/rpython/rlib/mutbuffer.py
@@ -1,6 +1,6 @@
-from rpython.rtyper.lltypesystem import lltype, llmemory
+from rpython.rtyper.lltypesystem import lltype
from rpython.rtyper.lltypesystem.lloperation import llop
-from rpython.rtyper.lltypesystem.rstr import STR
+from rpython.rtyper.lltypesystem.rstr import STR, mallocstr
from rpython.rtyper.annlowlevel import llstr, hlstr
from rpython.rlib.buffer import Buffer
@@ -21,10 +21,8 @@
def __init__(self, size):
self.readonly = False
- # rstr.mallocstr does not pass zero=True, so we call lltype.malloc
- # directly
self.size = size
- self.ll_val = lltype.malloc(STR, size, zero=True)
+ self.ll_val = mallocstr(size)
def getlength(self):
return self.size
@@ -40,5 +38,13 @@
def as_str(self):
raise ValueError('as_str() is not supported. Use finish() instead')
+ def _hlstr(self):
+ assert not we_are_translated() # debug only
+ return hlstr(self.ll_val)
+
def setitem(self, index, char):
self.ll_val.chars[index] = char
+
+ def setzeros(self, index, count):
+ for i in range(index, index+count):
+ self.setitem(i, '\x00')
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
@@ -21,10 +21,7 @@
native_is_ieee754 = float.__getformat__('double').startswith('IEEE')
def pack_pad(fmtiter, count):
- if fmtiter.needs_zeros:
- pos = fmtiter.pos
- for i in range(count):
- fmtiter.result.setitem(pos+i, '\x00')
+ fmtiter.result.setzeros(fmtiter.pos, count)
fmtiter.advance(count)
def pack_char(fmtiter):
@@ -43,11 +40,9 @@
def _pack_string(fmtiter, string, count):
pos = fmtiter.pos
if len(string) < count:
+ n = len(string)
fmtiter.result.setslice(pos, string)
- if fmtiter.needs_zeros:
- pos += len(string)
- for i in range(count - len(string)):
- fmtiter.result.setitem(pos+i, '\x00')
+ fmtiter.result.setzeros(pos+n, count-n)
else:
assert count >= 0
fmtiter.result.setslice(pos, string[:count])
diff --git a/rpython/rlib/rstruct/test/test_pack.py
b/rpython/rlib/rstruct/test/test_pack.py
--- a/rpython/rlib/rstruct/test/test_pack.py
+++ b/rpython/rlib/rstruct/test/test_pack.py
@@ -10,11 +10,7 @@
self.value = value
self.bigendian = bigendian
self.result = MutableStringBuffer(size)
- # we set the buffer to non-zero, so ensure that we actively write 0s
- # where it's needed
- self.result.setslice(0, '\xff'*size)
self.pos = 0
- self.needs_zeros = True
def advance(self, count):
self.pos += count
diff --git a/rpython/rlib/test/test_mutbuffer.py
b/rpython/rlib/test/test_mutbuffer.py
--- a/rpython/rlib/test/test_mutbuffer.py
+++ b/rpython/rlib/test/test_mutbuffer.py
@@ -22,3 +22,9 @@
buf = MutableStringBuffer(6)
buf.setslice(2, 'ABCD')
assert buf.finish() == '\x00\x00ABCD'
+
+ def test_setzeros(self):
+ buf = MutableStringBuffer(8)
+ buf.setslice(0, 'ABCDEFGH')
+ buf.setzeros(2, 3)
+ assert buf.finish() == 'AB\x00\x00\x00FGH'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit