Skip to main content

Borsh serialization for Python written in C.

Project description

Quick Borsh (qborsh)

An optimized Borsh (Binary Object Representation Serializer for Hashing) library for Python, written in C.

Install

pip install qborsh

Usage

Type Encoding/Decoding

import qborsh

encoded = qborsh.String.encode("Hello world!")
decoded = qborsh.String.decode(encoded)

array_of_u8s = qborsh.Array[qborsh.U8, 5]
encoded = array_of_u8s.encode([1, 2, 3, 4, 5])
decoded = array_of_u8s.decode(encoded)

Schema Encoding/Decoding

import qborsh

@qborsh.schema
class Example:
    # Numeric types
    u8_int: qborsh.U8
    u16_int: qborsh.U16
    u32_int: qborsh.U32
    u64_int: qborsh.U64
    u128_int: qborsh.U128
    i8_int: qborsh.I8
    i16_int: qborsh.I16
    i32_int: qborsh.I32
    i64_int: qborsh.I64
    i128_int: qborsh.I128
    f32_float: qborsh.F32
    f64_float: qborsh.F64

    # Bool, String, Bytes
    bool_val: qborsh.Bool
    string_val: qborsh.String
    bytes_val: qborsh.Bytes

    # Optionals
    optional_val: qborsh.Optional[qborsh.U32]

    # Collections
    set_val: qborsh.Set[qborsh.U8]
    vector_val: qborsh.Vector[qborsh.U16]
    array_val: qborsh.Array[qborsh.U8, 5]
    map_val: qborsh.Map[qborsh.U8, qborsh.String]

    # Helpers
    pubkey: qborsh.PubKey # Solana Base58 Encoded PubKey
    padding: qborsh.Padding[qborsh.Array[qborsh.U8, 10]]

@qborsh.schema
class ExampleNested:
    example: Example

data = {
    "u8_int": 255,
    "u16_int": 65535,
    "u32_int": 4294967295,
    "u64_int": 18446744073709551615,
    "u128_int": 340282366920938463463374607431768211455,
    "i8_int": -128,
    "i16_int": -32768,
    "i32_int": -2147483648,
    "i64_int": -9223372036854775808,
    "i128_int": -170141183460469231731687303715884105728,
    "f32_float": 3.4028234663852886e38,
    "f64_float": 1.7976931348623157e308,
    "bool_val": True,
    "string_val": "Hello, World!",
    "bytes_val": b"Hello, World!",
    "optional_val": 42,
    "set_val": {1, 2, 3, 4, 5},
    "vector_val": [1, 2, 3, 4, 5],
    "array_val": [1, 2, 3, 4, 5],
    "map_val": {1: "one", 2: "two", 3: "three"},
    "pubkey": "B62qoNf6QJk9kXJfzr6z7Z1J6VrYv6bQfMz7zD7c5Z9M",
    # Does not matter. Can be any value, or not present at all during
    # serialization. Must be present during deserialization however.
    "padding": "Does not matter. This is padding. Will write 0s.",
}

encoded = ExampleNested.encode({"example": data})
decoded = ExampleNested.decode(encoded)

# You may also do:
encoded = ExampleNested(example=data)   # kwargs resolve to encoding.
decoded = ExampleNested(encoded)        # args resolve to decoding.

There are three params to qborsh.schema (defaults in code-block below):

import qborsh

@qborsh.schema(
    validate: bool = False,
    dotdict: bool = False,
    exact_size: bool = True
)
class Example:
    ...
  • validate: Validate data keys during serialization. Checks if there are missing or extra keys when encoding only.
  • dotdict: Convert decoded dict into a dict with dot access for keys.
  • exact_size: If True, will return None when sizeof() is called and the schema has variable-sized field(s).

Package Wide Configuration

Buffer Size

The default size (in bytes) of buffers used throughout qborsh. If you expect very large or very small payloads, you may wish to adjust this. A too-small buffer may lead to more frequent resizing; a too-large buffer may waste memory on small payloads. Defaults to 512.

import qborsh

qborsh.set_buffer_size(1024)

or using environment variables:

QBORSH_BUFFER_SIZE=1024

Global Buffer

Whether to use a global buffer for faster serialization/deserialization. Enabling this can reduce repeated allocations in the underlying C layer, providing up to ~20-40% speedup in small- and medium-sized schemas. For large schemas, the gain is smaller but still measurable. This is not thread-safe. Defaults to True.

import qborsh

qborsh.set_global_buffer(False)

or using environment variables:

QBORSH_GLOBAL_BUFFER=False

Validate

Whether to enable validation (range checks) in the C extension. Defaults to True.

import qborsh

qborsh.set_validate(False)

or using environment variables:

QBORSH_VALIDATE=False

Benchmark

Comparing with another Python qborsh library:

Note that 'without val' means without schema validation.

--- Schema: Simple ---
borsh-construct         => avg: 258.0539 ms (std: 3.5250 ms)
qborsh (with val)       => avg: 36.9735 ms (std: 0.3608 ms)
qborsh (without val)    => avg: 36.1387 ms (std: 1.8255 ms)

--- Schema: Medium ---
borsh-construct         => avg: 334.0135 ms (std: 2.8085 ms)
qborsh (with val)       => avg: 36.2419 ms (std: 0.7954 ms)
qborsh (without val)    => avg: 36.5602 ms (std: 0.4150 ms)

--- Schema: Large ---
borsh-construct         => avg: 641.5983 ms (std: 21.6217 ms)
qborsh (with val)       => avg: 80.2482 ms (std: 2.8515 ms)
qborsh (without val)    => avg: 67.7815 ms (std: 2.1862 ms)

See scripts/benchmark.py for benchmarking details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qborsh-1.0.0.tar.gz (23.9 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

qborsh-1.0.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (44.4 kB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

qborsh-1.0.0-cp313-cp313-musllinux_1_2_x86_64.whl (119.0 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

qborsh-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (93.0 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

qborsh-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl (119.0 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

qborsh-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (93.2 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

qborsh-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl (119.7 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

qborsh-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (93.4 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

qborsh-1.0.0-cp310-cp310-musllinux_1_2_x86_64.whl (118.8 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

qborsh-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (92.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

File details

Details for the file qborsh-1.0.0.tar.gz.

File metadata

  • Download URL: qborsh-1.0.0.tar.gz
  • Upload date:
  • Size: 23.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for qborsh-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7e5a73c5fdcc8fad7cf476377a97e69dd12433bc78dc74cd74b6334000f89f12
MD5 9423dff07a0dc2b40c13c08d2fc4858e
BLAKE2b-256 a0816ddb95ee1d47a4089e7c09a6093a3562b78504ad4ee4849fb5655d31dadb

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0.tar.gz:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b81eb6362e9fcd15a0425d0df2277531a90571cb348fda7e4f73e0e4a5ed79f6
MD5 aebafb412070da675fd85d762ad69b2b
BLAKE2b-256 43b5c282589a39f8967f99732565ead42d16c0d54ccc221d30181f9a3c19d2df

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 6fbbceb74bcc05a7c9b0326a96ba8144118a0f7b6e48c5d9e3c4bc514239cbca
MD5 2dd80832584502d47977cc0781189730
BLAKE2b-256 256203d0a0aef5ab3aedbe114b6fb5b5b7739f7f69860bcf0fdac7d93444caac

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp313-cp313-musllinux_1_2_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 977376bbf82b59cb9fc42102df4012f138651714c0182cd96c359619675973d3
MD5 ccf45295c4bf4994686e8b77e724f815
BLAKE2b-256 d7e5f911d4f8c83498b689a2cc89a03ed71bf9c06b26591b0dace45267a150ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 333477a2cc187e2ef068870641c3981e417a308799f2bf9296c0c0e6ba9a2e5d
MD5 ebec9b100e655e90df0bbc38dce06ee3
BLAKE2b-256 f8dc9d2371958b1f5515051b20a00b5e2ee6996d7a1bbbe3989cebdccacdf67c

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp312-cp312-musllinux_1_2_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a9ee44b5ee850bc448939bf9cb4320735f55eeb2f89b3cd909c7aa110fbf79ee
MD5 a7bb2ff32b4ff77522954a539039014f
BLAKE2b-256 65866d94677c79855cb33e544f9a945fa67e544b98952ad41566660442898471

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 31a2b6a4366838ebbd409b9cd8a30cca084c02315edf6ce927537ee23881d21b
MD5 57dc8d78d8795805cb2ff256f3a620ce
BLAKE2b-256 289d590284445daa3c2263bdd0adf78da6fa20bfd65758fd53c1e958c0ebefef

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp311-cp311-musllinux_1_2_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ce26b84b2f0899e7abf03cb7fe767021b9913e118b2cb0bfe9745ecdc6db6a38
MD5 0350bc017a1dfa42090632d33a4a3002
BLAKE2b-256 21b5a0d2e2952a96349434ac317645e56f4ad525b58b9f1a46daf7397ea271bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 efc5bdb225d6996a96d0abd984eb212746deeb35dd6600d3a258bd1460bedb5d
MD5 a9af62dd409b8c9639a8b4c8fdd8c295
BLAKE2b-256 dd8e48d1027519e340cea1c13aae3de70fe9ac861e875d083d28cbac0967c850

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp310-cp310-musllinux_1_2_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qborsh-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c28ec611911d61094c74caa48ce3475277f096e4ed60e19cadcc3f5651f58290
MD5 ab92880ed04ca1e6aff2536fa3679fe8
BLAKE2b-256 eff5675f61657fa047ef760ed7b8afc138e1908e1794a6e75782ebfac250632b

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yaml on qvecs/qborsh

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page