From 4ca23ee44a95019acdc166a8041f2b5ee13b17e0 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 24 Jun 2026 00:20:22 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[yenc=20decode=20optimizati?= =?UTF-8?q?on]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: xbmc4lyfe <273732874+xbmc4lyfe@users.noreply.github.com> --- .jules/bolt.md | 3 +++ verify_nzb.py | 28 ++++++++++++++++++---------- 2 files changed, 21 insertions(+), 10 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000..63256bd --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-06-24 - [yEnc decoding optimization] +**Learning:** Python's manual byte-by-byte iteration is slow for yEnc decoding. We can shift the workload to C-backed functions by using `bytes.translate` to decode non-escaped segments, and `bytes.find` to quickly locate the escape character (`=`). When escaping, `(char - 64 - 42) % 256` simplifies algebraically to `(char - 106) % 256`. Also, trailing escapes at line boundaries need to be correctly identified by checking length. +**Action:** Always prefer C-backed built-in methods like `translate()` and `find()` over manual loops when processing raw byte strings in Python. diff --git a/verify_nzb.py b/verify_nzb.py index 953dccd..623eadd 100644 --- a/verify_nzb.py +++ b/verify_nzb.py @@ -115,19 +115,27 @@ def _parse_yenc_attrs(line: bytes) -> dict[str, str]: return attrs +_YENC_DECODE_MAP = bytes((i - 42) % 256 for i in range(256)) + def _decode_yenc_lines(lines: Iterable[bytes]) -> bytes: decoded = bytearray() for line in lines: - index = 0 - while index < len(line): - byte = line[index] - if byte == 61: - index += 1 - if index >= len(line): - raise ValueError("dangling yEnc escape") - byte = (line[index] - 64) % 256 - decoded.append((byte - 42) % 256) - index += 1 + pos = 0 + line_len = len(line) + while True: + escape_pos = line.find(b"=", pos) + if escape_pos == -1: + decoded.extend(line[pos:].translate(_YENC_DECODE_MAP)) + break + + decoded.extend(line[pos:escape_pos].translate(_YENC_DECODE_MAP)) + + if escape_pos + 1 >= line_len: + raise ValueError("dangling yEnc escape") + + decoded.append((line[escape_pos + 1] - 106) % 256) + pos = escape_pos + 2 + return bytes(decoded)