Skip to content

Commit

Permalink
Use numpy.array's "offset" param instead of slicing
Browse files Browse the repository at this point in the history
Passing an offset into numpy.array instead of slicing should prevent an
additional copy of the data being created by the slice operation. This
is important for really big message types like OccupancyGrids.
  • Loading branch information
Alex Bencz committed Feb 8, 2016
1 parent c335328 commit 142de5f
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/genpy/generate_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@
'byte' : 'numpy.int8',
}
# TODO: this doesn't explicitly specify little-endian byte order on the numpy data instance
def unpack_numpy(var, count, dtype, buff):
def unpack_numpy(var, offset, count, dtype, buff):
"""
create numpy deserialization code
"""
return var + " = numpy.frombuffer(%s, dtype=%s, count=%s)"%(buff, dtype, count)
return var + " = numpy.frombuffer(%s, dtype=%s, count=%s, offset=%s)"%(buff, dtype, count, offset)

def pack_numpy(var):
"""
Expand Down
4 changes: 2 additions & 2 deletions src/genpy/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ def array_serializer_generator(msg_context, package, type_, name, serialize, is_
yield "end += struct.calcsize(pattern)"
if is_numpy:
dtype = NUMPY_DTYPE[base_type]
yield unpack_numpy(var, 'length', dtype, 'str[start:end]')
yield unpack_numpy(var, 'start', 'length', dtype, 'str')
else:
yield unpack2(var, 'pattern', 'str[start:end]')
else:
Expand All @@ -506,7 +506,7 @@ def array_serializer_generator(msg_context, package, type_, name, serialize, is_
yield "end += %s"%struct.calcsize('<%s'%pattern)
if is_numpy:
dtype = NUMPY_DTYPE[base_type]
yield unpack_numpy(var, length, dtype, 'str[start:end]')
yield unpack_numpy(var, 'start', length, dtype, 'str')
else:
yield unpack(var, pattern, 'str[start:end]')
if not serialize and base_type == 'bool':
Expand Down
2 changes: 1 addition & 1 deletion test/files/array/int16_fixed_deser_np.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
start = end
end += 20
data = numpy.frombuffer(str[start:end], dtype=numpy.int16, count=10)
data = numpy.frombuffer(str, dtype=numpy.int16, count=10, offset=start)
2 changes: 1 addition & 1 deletion test/files/array/int16_varlen_deser_np.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ end += 4
pattern = '<%sh'%length
start = end
end += struct.calcsize(pattern)
data = numpy.frombuffer(str[start:end], dtype=numpy.int16, count=length)
data = numpy.frombuffer(str, dtype=numpy.int16, count=length, offset=start)

0 comments on commit 142de5f

Please sign in to comment.