From 6024198f93cafbbdbd6ce8fb6848ba2e2fa4cc6b Mon Sep 17 00:00:00 2001 From: "andrew (from workstation)" Date: Wed, 16 Mar 2022 21:53:26 +0100 Subject: [PATCH 1/3] faster pyrogram lre encode implementation --- pyrogram/file_id.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pyrogram/file_id.py b/pyrogram/file_id.py index 95f6ccbf1a..a8d5a922e3 100644 --- a/pyrogram/file_id.py +++ b/pyrogram/file_id.py @@ -21,6 +21,7 @@ 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: From 629d52a6d8118043a3a59378519431db59e07a3c Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Thu, 17 Mar 2022 12:37:45 +0100 Subject: [PATCH 2/3] Update file_id.py --- pyrogram/file_id.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyrogram/file_id.py b/pyrogram/file_id.py index a8d5a922e3..bf15f79289 100644 --- a/pyrogram/file_id.py +++ b/pyrogram/file_id.py @@ -88,10 +88,10 @@ 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"" z: bool = False From 7e64f27d914f24430ad2d2101633d59da64736db Mon Sep 17 00:00:00 2001 From: "andrew (from workstation)" Date: Thu, 17 Mar 2022 20:30:47 +0100 Subject: [PATCH 3/3] optimized rle decode --- pyrogram/file_id.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pyrogram/file_id.py b/pyrogram/file_id.py index bf15f79289..1a54a49d61 100644 --- a/pyrogram/file_id.py +++ b/pyrogram/file_id.py @@ -93,7 +93,7 @@ def rle_decode(s: bytes) -> bytes: Returns: ``bytes``: The decoded bytes """ - r: bytes = b"" + r: List[int] = [] z: bool = False for b in s: @@ -102,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):