From 214e6edcbfcf757fb278fc7161ccfca926ff44de Mon Sep 17 00:00:00 2001 From: claw Date: Wed, 20 May 2026 08:46:27 +0000 Subject: [PATCH 1/2] fix: optimize base64 decoding and reduce string allocations in ConnectorRest Fixes #232 by: - using pybase64 for faster decoding operations - utilizing response.json() instead of json.loads(response.text) to reduce redundant string parsing --- aperturedb/ConnectorRest.py | 8 ++++++-- pyproject.toml | 1 + 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/aperturedb/ConnectorRest.py b/aperturedb/ConnectorRest.py index 803ab733..dd336306 100644 --- a/aperturedb/ConnectorRest.py +++ b/aperturedb/ConnectorRest.py @@ -33,6 +33,11 @@ from types import SimpleNamespace from typing import Optional +try: + import pybase64 as base64 +except ImportError: + import base64 + from aperturedb.Connector import Connector from aperturedb.Configuration import Configuration from requests.adapters import HTTPAdapter @@ -164,8 +169,7 @@ def _query(self, query, blob_array = [], try_resume=True): verify = self.config.use_ssl and self.config.verify_hostname) if response.status_code == 200: # Parse response: - json_response = json.loads(response.text) - import base64 + json_response = response.json() response_blob_array = [base64.b64decode( b) for b in json_response['blobs']] self.last_response = json_response["json"] diff --git a/pyproject.toml b/pyproject.toml index de914ca0..2e77a4b8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,6 +30,7 @@ dependencies = [ 'keepalive-socket==0.0.1', 'graphviz==0.20.2', "python-dotenv", + "pybase64", ] [tool.setuptools.package-dir] From ee01d88290606d7af30c2911252d4c659ee3484a Mon Sep 17 00:00:00 2001 From: claw Date: Wed, 20 May 2026 09:25:43 +0000 Subject: [PATCH 2/2] Address PR review comments --- aperturedb/ConnectorRest.py | 5 ++++- pyproject.toml | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/aperturedb/ConnectorRest.py b/aperturedb/ConnectorRest.py index dd336306..c3231234 100644 --- a/aperturedb/ConnectorRest.py +++ b/aperturedb/ConnectorRest.py @@ -169,7 +169,10 @@ def _query(self, query, blob_array = [], try_resume=True): verify = self.config.use_ssl and self.config.verify_hostname) if response.status_code == 200: # Parse response: - json_response = response.json() + if hasattr(response, "json"): + json_response = response.json() + else: + json_response = json.loads(response.text) response_blob_array = [base64.b64decode( b) for b in json_response['blobs']] self.last_response = json_response["json"] diff --git a/pyproject.toml b/pyproject.toml index 2e77a4b8..e6c8f2eb 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -30,7 +30,6 @@ dependencies = [ 'keepalive-socket==0.0.1', 'graphviz==0.20.2', "python-dotenv", - "pybase64", ] [tool.setuptools.package-dir] @@ -41,6 +40,9 @@ aperturedb = "aperturedb" "Bug Reports" = "https://github.com/aperture-data/aperturedb-python/issues" [project.optional-dependencies] +speedups = [ + "pybase64", +] # This is used when we build the docker image for notebook notebook = [ "torch",