From 207163b87c8329fc015fde8ad85a2af2ae3e84cc Mon Sep 17 00:00:00 2001 From: Evan Blaudy Date: Mon, 25 Sep 2023 17:04:36 +0200 Subject: [PATCH] [qa] use orjson instead of json to improve performances --- setup.cfg | 1 + zou/app/__init__.py | 3 ++- zou/app/utils/{user_agent.py => flask.py} | 17 +++++++++++++++++ zou/event_stream.py | 2 ++ 4 files changed, 22 insertions(+), 1 deletion(-) rename zou/app/utils/{user_agent.py => flask.py} (57%) diff --git a/setup.cfg b/setup.cfg index 2717bfd313..edd8bcd853 100644 --- a/setup.cfg +++ b/setup.cfg @@ -56,6 +56,7 @@ install_requires = matterhook==0.2 meilisearch==0.28.3 OpenTimelineIO==0.15.0 + orjson==3.9.7 pillow==10.0.1 psutil==5.9.5 psycopg[binary]==3.1.11 diff --git a/zou/app/__init__.py b/zou/app/__init__.py index 3a832411e4..2dd2852471 100644 --- a/zou/app/__init__.py +++ b/zou/app/__init__.py @@ -29,10 +29,11 @@ from zou.app.utils import cache, fs, logs from zou.app.utils.sentry import init_sentry -from zou.app.utils.user_agent import ParsedUserAgent +from zou.app.utils.flask import ParsedUserAgent, ORJSONProvider init_sentry() app = Flask(__name__) +app.json = ORJSONProvider(app) app.request_class.user_agent_class = ParsedUserAgent app.config.from_object(config) diff --git a/zou/app/utils/user_agent.py b/zou/app/utils/flask.py similarity index 57% rename from zou/app/utils/user_agent.py rename to zou/app/utils/flask.py index c4891c4bc0..6261d7d5e4 100644 --- a/zou/app/utils/user_agent.py +++ b/zou/app/utils/flask.py @@ -1,6 +1,23 @@ from ua_parser import user_agent_parser from werkzeug.user_agent import UserAgent from werkzeug.utils import cached_property +from flask.json.provider import JSONProvider +import orjson + + +class ORJSONProvider(JSONProvider): + def __init__(self, *args, **kwargs): + self.options = kwargs + super().__init__(*args, **kwargs) + + def loads(self, s, **kwargs): + return orjson.loads(s) + + def dumps(self, obj, **kwargs): + # decode back to str, as orjson returns bytes + return orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS).decode( + "utf-8" + ) class ParsedUserAgent(UserAgent): diff --git a/zou/event_stream.py b/zou/event_stream.py index a53e5e17a4..b0d7cc3edf 100644 --- a/zou/event_stream.py +++ b/zou/event_stream.py @@ -13,6 +13,7 @@ from zou.app import config from zou.app.stores import auth_tokens_store from zou.app.utils.sentry import init_sentry +from zou.app.utils.flask import ORJSONProvider server_stats = {"nb_connections": 0} rooms_data = {} @@ -216,6 +217,7 @@ def create_app(): ) init_sentry() app = Flask(__name__) + app.json = ORJSONProvider(app) app.config.from_object(config) set_info_routes(socketio, app) set_application_routes(socketio, app)