Skip to content

Commit

Permalink
Optimize writing to engine stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Apr 22, 2022
1 parent 489b49d commit 8acbcc8
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions chess/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,13 +936,13 @@ def write(self, data: bytes) -> None:

if line.startswith("ping ") and self.expected_pings:
self.expected_pings -= 1
self.protocol.pipe_data_received(1, line.replace("ping ", "pong ").encode("utf-8") + b"\n")
self.protocol.pipe_data_received(1, (line.replace("ping ", "pong ") + "\n").encode("utf-8"))
else:
assert self.expectations, f"unexpected: {line!r}"
expectation, responses = self.expectations.popleft()
assert expectation == line, f"expected {expectation}, got: {line}"
if responses:
self.protocol.pipe_data_received(1, "\n".join(responses).encode("utf-8") + b"\n")
self.protocol.pipe_data_received(1, "\n".join(responses + [""]).encode("utf-8"))

def get_pid(self) -> int:
return id(self)
Expand Down Expand Up @@ -1010,8 +1010,7 @@ def send_line(self, line: str) -> None:
assert self.transport is not None, "cannot send line before connection is made"
stdin = self.transport.get_pipe_transport(0)
# WriteTransport expected, but not checked to allow duck typing.
stdin.write(line.encode("utf-8")) # type: ignore
stdin.write(b"\n") # type: ignore
stdin.write((line + "\n").encode("utf-8")) # type: ignore

def pipe_data_received(self, fd: int, data: Union[bytes, str]) -> None:
self.buffer[fd].extend(data) # type: ignore
Expand Down

0 comments on commit 8acbcc8

Please sign in to comment.