From 812ae7eef816b4459566739767312842e37d37c5 Mon Sep 17 00:00:00 2001 From: Petr Pucil Date: Mon, 22 Jul 2024 20:15:27 +0200 Subject: [PATCH] read_bytes_term(): additional minor performance improvements See the `new_v2` version at https://gist.github.com/generalmimon/41dcb2bba48cb47c2f30ce9f086cbbf2 - it's about 10% faster than the existing `new` version. --- kaitaistruct.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/kaitaistruct.py b/kaitaistruct.py index 487aa4a..6457550 100644 --- a/kaitaistruct.py +++ b/kaitaistruct.py @@ -409,10 +409,11 @@ def read_bytes_full(self): def read_bytes_term(self, term, include_term, consume_term, eos_error): self.align_to_byte() + term_byte = KaitaiStream.byte_from_int(term) r = bytearray() while True: c = self._io.read(1) - if c == b'': + if not c: if eos_error: raise Exception( "end of stream reached, but no terminator %d found" % @@ -421,7 +422,7 @@ def read_bytes_term(self, term, include_term, consume_term, eos_error): return bytes(r) - if ord(c) == term: + if c == term_byte: if include_term: r += c if not consume_term: