From 06ee482b2331e94c6494248a59d10ab53ba15a24 Mon Sep 17 00:00:00 2001 From: andrew-ld <43882924+andrew-ld@users.noreply.github.com> Date: Mon, 28 Mar 2022 20:10:52 +0200 Subject: [PATCH] Faster RLE codec implementation (#938) * faster pyrogram lre encode implementation * Update file_id.py * optimized rle decode Co-authored-by: andrew (from workstation) Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com> --- pyrogram/file_id.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/pyrogram/file_id.py b/pyrogram/file_id.py index 95f6ccbf..1a54a49d 100644 --- a/pyrogram/file_id.py +++ b/pyrogram/file_id.py @@ -21,6 +21,7 @@ import logging import struct from enum import IntEnum from io import BytesIO +from typing import List from pyrogram.raw.core import Bytes, String @@ -63,7 +64,7 @@ def rle_encode(s: bytes) -> bytes: Returns: ``bytes``: The encoded bytes """ - r: bytes = b"" + r: List[int] = [] n: int = 0 for b in s: @@ -71,15 +72,15 @@ def rle_encode(s: bytes) -> bytes: n += 1 else: if n: - r += bytes([0, n]) + r.extend((0, n)) n = 0 - r += bytes([b]) + r.append(b) if n: - r += bytes([0, n]) + r.extend((0, n)) - return r + return bytes(r) def rle_decode(s: bytes) -> bytes: @@ -87,12 +88,12 @@ def rle_decode(s: bytes) -> bytes: Parameters: s (``bytes``): - Bytes to encode + Bytes to decode Returns: - ``bytes``: The encoded bytes + ``bytes``: The decoded bytes """ - r: bytes = b"" + r: List[int] = [] z: bool = False for b in s: @@ -101,12 +102,12 @@ def rle_decode(s: bytes) -> bytes: continue if z: - r += b"\x00" * b + r.extend((0,) * b) z = False else: - r += bytes([b]) + r.append(b) - return r + return bytes(r) class FileType(IntEnum):