Skip to content

Commit 06ee482

Browse files
Faster RLE codec implementation (pyrogram#938)
* faster pyrogram lre encode implementation * Update file_id.py * optimized rle decode Co-authored-by: andrew (from workstation) <andrew-ld@protonmail.com> Co-authored-by: Dan <14043624+delivrance@users.noreply.github.com>
1 parent 190760c commit 06ee482

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

pyrogram/file_id.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import struct
2222
from enum import IntEnum
2323
from io import BytesIO
24+
from typing import List
2425

2526
from pyrogram.raw.core import Bytes, String
2627

@@ -63,36 +64,36 @@ def rle_encode(s: bytes) -> bytes:
6364
Returns:
6465
``bytes``: The encoded bytes
6566
"""
66-
r: bytes = b""
67+
r: List[int] = []
6768
n: int = 0
6869

6970
for b in s:
7071
if not b:
7172
n += 1
7273
else:
7374
if n:
74-
r += bytes([0, n])
75+
r.extend((0, n))
7576
n = 0
7677

77-
r += bytes([b])
78+
r.append(b)
7879

7980
if n:
80-
r += bytes([0, n])
81+
r.extend((0, n))
8182

82-
return r
83+
return bytes(r)
8384

8485

8586
def rle_decode(s: bytes) -> bytes:
8687
"""Zero-value RLE decoder
8788
8889
Parameters:
8990
s (``bytes``):
90-
Bytes to encode
91+
Bytes to decode
9192
9293
Returns:
93-
``bytes``: The encoded bytes
94+
``bytes``: The decoded bytes
9495
"""
95-
r: bytes = b""
96+
r: List[int] = []
9697
z: bool = False
9798

9899
for b in s:
@@ -101,12 +102,12 @@ def rle_decode(s: bytes) -> bytes:
101102
continue
102103

103104
if z:
104-
r += b"\x00" * b
105+
r.extend((0,) * b)
105106
z = False
106107
else:
107-
r += bytes([b])
108+
r.append(b)
108109

109-
return r
110+
return bytes(r)
110111

111112

112113
class FileType(IntEnum):

0 commit comments

Comments
 (0)