From 7374c10afd1fe680961bc65bd7d1ed92aaff289a Mon Sep 17 00:00:00 2001 From: Dan <14043624+delivrance@users.noreply.github.com> Date: Mon, 11 Dec 2017 11:35:38 +0100 Subject: [PATCH] Faster Object deserialization (~3x) --- pyrogram/api/__init__.py | 9 +++++++++ pyrogram/api/core/object.py | 12 +++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/pyrogram/api/__init__.py b/pyrogram/api/__init__.py index b9ee0a98..36e3ebb9 100644 --- a/pyrogram/api/__init__.py +++ b/pyrogram/api/__init__.py @@ -15,3 +15,12 @@ # # You should have received a copy of the GNU Lesser General Public License # along with Pyrogram. If not, see . + +from importlib import import_module + +from .all import objects +from .core.object import Object + +for k, v in objects.items(): + path, name = v.rsplit(".", 1) + Object.all[k] = getattr(import_module("pyrogram.api." + path), name) diff --git a/pyrogram/api/core/object.py b/pyrogram/api/core/object.py index 72452c46..58b370bc 100644 --- a/pyrogram/api/core/object.py +++ b/pyrogram/api/core/object.py @@ -18,7 +18,6 @@ from collections import OrderedDict from datetime import datetime -from importlib import import_module from io import BytesIO from json import JSONEncoder, dumps @@ -26,16 +25,11 @@ from ..all import objects class Object: + all = {} + @staticmethod def read(b: BytesIO, *args): - id = int.from_bytes(b.read(4), "little") - name = objects.get(id) - path, name = name.rsplit(".", 1) - - return getattr( - import_module("pyrogram.api." + path), - name - ).read(b, *args) + return Object.all[int.from_bytes(b.read(4), "little")].read(b, *args) def write(self, *args) -> bytes: pass