Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🚧 WIP: Replace default python json library with orjson #301

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions wrappers/python/aries_askar/bindings/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Low-level interaction with the aries-askar library."""

import asyncio
import json
import orjson
import logging

from ctypes import POINTER, byref, c_int8, c_int32, c_int64
Expand Down Expand Up @@ -605,7 +605,7 @@ def key_get_secret_bytes(handle: LocalKeyHandle) -> ByteBuffer:
def key_from_jwk(jwk: Union[dict, str, bytes]) -> LocalKeyHandle:
handle = LocalKeyHandle()
if isinstance(jwk, dict):
jwk = json.dumps(jwk)
jwk = orjson.dumps(jwk)
invoke(
"askar_key_from_jwk",
(FfiByteBuffer, POINTER(LocalKeyHandle)),
Expand Down
6 changes: 3 additions & 3 deletions wrappers/python/aries_askar/bindings/handle.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Handles for allocated resources."""

import json
import orjson
import logging

from ctypes import (
Expand Down Expand Up @@ -167,7 +167,7 @@ def get_tags(self, index: int) -> dict:
byref(tags),
)
if tags:
tags = json.loads(tags.value)
tags = orjson.loads(tags.value)
for t in tags:
if isinstance(tags[t], list):
tags[t] = set(tags[t])
Expand Down Expand Up @@ -227,7 +227,7 @@ def get_tags(self, index: int) -> dict:
index,
byref(tags),
)
return json.loads(tags.value) if tags else None
return orjson.loads(tags.value) if tags else None

def load_key(self, index: int) -> "LocalKeyHandle":
"""Load the key instance."""
Expand Down
12 changes: 6 additions & 6 deletions wrappers/python/aries_askar/bindings/lib.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Library instance and allocated buffer handling."""

import asyncio
import json
import orjson
import itertools
import logging
import os
Expand Down Expand Up @@ -280,8 +280,8 @@ def get_current_error(self, expect: bool = False) -> Optional[AskarError]:
)
if not method(byref(err_json)):
try:
msg = json.loads(err_json.value)
except json.JSONDecodeError:
msg = orjson.loads(err_json.value)
except orjson.JSONDecodeError:
LOGGER.warning("JSON decode error for askar_get_current_error")
msg = None
if msg and "message" in msg and "code" in msg:
Expand Down Expand Up @@ -540,7 +540,7 @@ def from_param(cls, value):
if isinstance(value, FfiStr):
return value
if isinstance(value, dict):
value = json.dumps(value)
value = orjson.dumps(value).decode()
return FfiStr(value)


Expand All @@ -550,12 +550,12 @@ def from_param(cls, tags):
if isinstance(tags, FfiStr):
return tags
if tags:
tags = json.dumps(
tags = orjson.dumps(
{
name: (list(value) if isinstance(value, set) else value)
for name, value in tags.items()
}
)
).decode()
else:
tags = None
return FfiStr(tags)
Expand Down
8 changes: 4 additions & 4 deletions wrappers/python/aries_askar/store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Handling of Store instances."""

import json
import orjson

from typing import Optional, Sequence, Union

Expand Down Expand Up @@ -52,7 +52,7 @@ def raw_value(self) -> memoryview:
@property
def value_json(self) -> dict:
"""Accessor for the entry value as JSON."""
return json.loads(self.value)
return orjson.loads(self.value)

@cached_property
def tags(self) -> dict:
Expand Down Expand Up @@ -580,7 +580,7 @@ async def insert(
if not self._handle:
raise AskarError(AskarErrorCode.WRAPPER, "Cannot update closed session")
if value is None and value_json is not None:
value = json.dumps(value_json)
value = orjson.dumps(value_json)
await bindings.session_update(
self._handle, EntryOperation.INSERT, category, name, value, tags, expiry_ms
)
Expand All @@ -598,7 +598,7 @@ async def replace(
if not self._handle:
raise AskarError(AskarErrorCode.WRAPPER, "Cannot update closed session")
if value is None and value_json is not None:
value = json.dumps(value_json)
value = orjson.dumps(value_json)
await bindings.session_update(
self._handle, EntryOperation.REPLACE, category, name, value, tags, expiry_ms
)
Expand Down
20 changes: 10 additions & 10 deletions wrappers/python/indy_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import asyncio
import indy
import json
import orjson
import time

PERF_ROWS = 10000
Expand Down Expand Up @@ -33,8 +33,8 @@ async def perf_test():
"admin_password": "pgpass",
}

config = json.dumps(wallet_config)
creds = json.dumps(wallet_creds)
config = orjson.dumps(wallet_config).decode()
creds = orjson.dumps(wallet_creds).decode()

try:
await indy.wallet.delete_wallet(config, creds)
Expand All @@ -47,7 +47,7 @@ async def perf_test():

insert_start = time.perf_counter()
for idx in range(PERF_ROWS):
tags_json = json.dumps({"~plaintag": "a", "enctag": "b"})
tags_json = orjson.dumps({"~plaintag": "a", "enctag": "b"}).decode()
await indy.non_secrets.add_wallet_record(
handle, "category", f"name-{idx}", "value", tags_json
)
Expand All @@ -56,35 +56,35 @@ async def perf_test():

rc = 0
tags = 0
options_json = json.dumps(
options_json = orjson.dumps(
{
"retrieveType": True,
"retrieveValue": True,
"retrieveTags": True,
}
)
).decode()
fetch_start = time.perf_counter()
for idx in range(PERF_ROWS):
result_json = await indy.non_secrets.get_wallet_record(
handle, "category", f"name-{idx}", options_json
)
result = json.loads(result_json)
result = orjson.loads(result_json)
rc += 1
tags += len(result["tags"])
dur = time.perf_counter() - fetch_start
print(f"fetch duration ({rc} rows, {tags} tags): {dur:0.2f}s")

rc = 0
tags = 0
options_json = json.dumps(
options_json = orjson.dumps(
{
"retrieveRecords": True,
"retrieveTotalCount": False,
"retrieveType": True,
"retrieveValue": True,
"retrieveTags": True,
}
)
).decode()
scan_start = time.perf_counter()
search_handle = await indy.non_secrets.open_wallet_search(
handle, "category", "{}", options_json
Expand All @@ -93,7 +93,7 @@ async def perf_test():
result_json = await indy.non_secrets.fetch_wallet_search_next_records(
handle, search_handle, 20
)
results = json.loads(result_json)
results = orjson.loads(result_json)
if results["records"]:
for row in results["records"]:
rc += 1
Expand Down
1 change: 1 addition & 0 deletions wrappers/python/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
cached_property~=1.5.2
orjson~=3.10.3
10 changes: 5 additions & 5 deletions wrappers/python/tests/test_keys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import orjson

from aries_askar.types import KeyBackend
import pytest
Expand Down Expand Up @@ -79,11 +79,11 @@ def test_ed25519():
kex = x25519_key.key_exchange(KeyAlg.XC20P, x25519_key_2)
assert isinstance(kex, Key)

jwk = json.loads(key.get_jwk_public())
jwk = orjson.loads(key.get_jwk_public())
assert jwk["kty"] == "OKP"
assert jwk["crv"] == "Ed25519"

jwk = json.loads(key.get_jwk_secret())
jwk = orjson.loads(key.get_jwk_secret())
assert jwk["kty"] == "OKP"
assert jwk["crv"] == "Ed25519"

Expand All @@ -99,13 +99,13 @@ def test_ec_curves(key_alg: KeyAlg):
sig = key.sign_message(message)
assert key.verify_signature(message, sig)

jwk = json.loads(key.get_jwk_public())
jwk = orjson.loads(key.get_jwk_public())
assert jwk["kty"] == "EC"
assert jwk["crv"]
assert jwk["x"]
assert jwk["y"]

jwk = json.loads(key.get_jwk_secret())
jwk = orjson.loads(key.get_jwk_secret())
assert jwk["kty"] == "EC"
assert jwk["crv"]
assert jwk["x"]
Expand Down
Loading