coredevice.comm_kernel: performance improvement

reduced latency by busy polling, and improved byte list performance.
This commit is contained in:
pca006132 2021-05-23 13:30:00 +08:00
parent 43b2a3791c
commit 1e9a131386
1 changed files with 8 additions and 8 deletions

View File

@ -66,7 +66,7 @@ def _receive_list(kernel, embedding_map):
tag = chr(kernel._read_int8()) tag = chr(kernel._read_int8())
if tag == "b": if tag == "b":
buffer = kernel._read(length) buffer = kernel._read(length)
return [bool(a) for a in buffer] return list(struct.unpack(kernel.endian + "%s?" % length, buffer))
elif tag == "i": elif tag == "i":
buffer = kernel._read(4 * length) buffer = kernel._read(4 * length)
return list(struct.unpack(kernel.endian + "%sl" % length, buffer)) return list(struct.unpack(kernel.endian + "%sl" % length, buffer))
@ -217,13 +217,13 @@ class CommKernel:
def _read(self, length): def _read(self, length):
# cache the reads to avoid frequent call to recv # cache the reads to avoid frequent call to recv
while len(self.read_buffer) < length: while len(self.read_buffer) < length:
# the number is just the maximum amount while True:
# when there is not much data, it would return earlier try:
diff = length - len(self.read_buffer) flag = socket.MSG_DONTWAIT
flag = 0 new_buffer = self.socket.recv(8192, flag)
if diff > 8192: break
flag |= socket.MSG_WAITALL except BlockingIOError:
new_buffer = self.socket.recv(8192, flag) pass
if not new_buffer: if not new_buffer:
raise ConnectionResetError("Core device connection closed unexpectedly") raise ConnectionResetError("Core device connection closed unexpectedly")
self.read_buffer += new_buffer self.read_buffer += new_buffer