Skip to content

Commit 7374c10

Browse files
committed
Faster Object deserialization (~3x)
1 parent 33263e9 commit 7374c10

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

pyrogram/api/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,12 @@
1515
#
1616
# You should have received a copy of the GNU Lesser General Public License
1717
# along with Pyrogram. If not, see <http://www.gnu.org/licenses/>.
18+
19+
from importlib import import_module
20+
21+
from .all import objects
22+
from .core.object import Object
23+
24+
for k, v in objects.items():
25+
path, name = v.rsplit(".", 1)
26+
Object.all[k] = getattr(import_module("pyrogram.api." + path), name)

pyrogram/api/core/object.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,18 @@
1818

1919
from collections import OrderedDict
2020
from datetime import datetime
21-
from importlib import import_module
2221
from io import BytesIO
2322
from json import JSONEncoder, dumps
2423

2524
from ..all import objects
2625

2726

2827
class Object:
28+
all = {}
29+
2930
@staticmethod
3031
def read(b: BytesIO, *args):
31-
id = int.from_bytes(b.read(4), "little")
32-
name = objects.get(id)
33-
path, name = name.rsplit(".", 1)
34-
35-
return getattr(
36-
import_module("pyrogram.api." + path),
37-
name
38-
).read(b, *args)
32+
return Object.all[int.from_bytes(b.read(4), "little")].read(b, *args)
3933

4034
def write(self, *args) -> bytes:
4135
pass

0 commit comments

Comments
 (0)