Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a faster way to copy from a bytearray to a mmap slice in Python?

I am writing code for an addon to XBMC that copies an image provided in a bytearray to a slice of a mmap object. Using Kern's line profiler, the bottleneck in my code is when I copy the bytearray into the mmap object at the appropriate location. In essence:

length = 1920 * 1080 * 4
mymmap = mmap.mmap(0, length + 11, 'Name', mmap.ACCESS_WRITE)
image = capture.getImage()  # This returns a bytearray that is 4*1920*1080 in size
mymmap[11:(11 + length)] = str(image)  # This is the bottleneck according to the line profiler

I cannot change the data types of either the image or mmap. XBMC provides the image as a bytearray and the mmap interface was designed by a developer who won't change the implementation. It is NOT being used to write to a file, however - it was his way of getting the data out of XBMC and into his C++ application. I recognize that he could write an interface using ctypes that might handle this better, but he is not interested in further development. The python implementation in XBMC is 2.7.

I looked at the possibility of using ctypes (in a self-contained way withing python) myself with memmove, but can't quite figure out how to convert the bytearray and mmap slice into c structures that can be used with memmove and don't know if that would be any faster. Any advice on a fast way to move these bytes between these two data types?

like image 259
KenV99 Avatar asked Nov 10 '22 04:11

KenV99


1 Answers

If the slice assignment to the mmap object is your bottleneck I don't think anything can be done to improve the performance of your code. All the assignment does internally is call memcpy.

like image 130
Ross Ridge Avatar answered Nov 14 '22 21:11

Ross Ridge