diff --git a/plotly/io/_json.py b/plotly/io/_json.py index 0e741711f4f..6c0e377a156 100644 --- a/plotly/io/_json.py +++ b/plotly/io/_json.py @@ -8,6 +8,10 @@ from _plotly_utils.optional_imports import get_module from _plotly_utils.basevalidators import ImageUriValidator +_ORJSON_MODULE = get_module("orjson", should_load=True) + +_VALIDATE_ORJSON_CALLED = False + # Orca configuration class # ------------------------ @@ -325,7 +329,11 @@ def from_json_plotly(value, engine=None): -------- from_json_plotly : Parse JSON with plotly conventions into a dict """ - orjson = get_module("orjson", should_load=True) + # Use statically imported _ORJSON_MODULE instead of calling get_module each time + orjson = _ORJSON_MODULE + + # Validate value + # -------------- # Validate value # -------------- @@ -349,7 +357,12 @@ def from_json_plotly(value, engine=None): raise ValueError("Invalid json engine: %s" % engine) if engine == "orjson": - JsonConfig.validate_orjson() + # Cache result of validate_orjson, which is expensive and unnecessary to call each time + global _VALIDATE_ORJSON_CALLED + if not _VALIDATE_ORJSON_CALLED: + config.__class__.validate_orjson() # Preserve side effect and exception raising + _VALIDATE_ORJSON_CALLED = True + # orjson handles bytes input natively # orjson handles bytes input natively value_dict = orjson.loads(value) else: