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.1.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.1-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.1-cp313-cp313-musllinux_1_2_x86_64.whl (119.1 kB view details)

Uploaded CPython 3.13musllinux: musl 1.2+ x86-64

qborsh-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (93.1 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

qborsh-1.0.1-cp312-cp312-musllinux_1_2_x86_64.whl (119.1 kB view details)

Uploaded CPython 3.12musllinux: musl 1.2+ x86-64

qborsh-1.0.1-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.1-cp311-cp311-musllinux_1_2_x86_64.whl (119.8 kB view details)

Uploaded CPython 3.11musllinux: musl 1.2+ x86-64

qborsh-1.0.1-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.1-cp310-cp310-musllinux_1_2_x86_64.whl (118.9 kB view details)

Uploaded CPython 3.10musllinux: musl 1.2+ x86-64

qborsh-1.0.1-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.1.tar.gz.

File metadata

  • Download URL: qborsh-1.0.1.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.1.tar.gz
Algorithm Hash digest
SHA256 a8376bdefbfb9f920fd1a4a9dac34ff0f2b7ae110601e759f1119753a8f0e17b
MD5 30a9f636c9f0becae7fc44f47ddfd422
BLAKE2b-256 fd6be643b032df794ec58593c8f22f9db54f817572fd0a878197d1014c5552a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1.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.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-pp310-pypy310_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 818c26feaa701c04a98b1e28b86680b2f9421e599fbfcc117aee0fa962fb1575
MD5 81b4c4a3a0e50f3461ad4d89aac5288b
BLAKE2b-256 2cddaf8c2805eecab81b46e1e39c3c81c72f54b9c3e8a1a1724687f7c0488cba

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp313-cp313-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp313-cp313-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 7cbe42ac4e5c871bf36024225ec33d74b4ec2930dd1fbe8fe6c077bdb95a8db4
MD5 49beeabe63179ac46314d43c6f66945a
BLAKE2b-256 72b830bc3fb36c5abc3902476f97080eea4b094d7ca5a9930ea767662376d295

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 b17702388d9bfd60e66b49710fa936cd211188c465e5e40ca82b093178c63d70
MD5 5099e4d4e61815a3440a9c71fcd2808f
BLAKE2b-256 7d3824b6227c0635e64da47b7096cc5f69ba457ce0bf9afb9ed9ae063f1d355f

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp312-cp312-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp312-cp312-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 890c40eea4bf18257d0e2e240a84d2723d72de4403bf2f5a18bdca7ade09750e
MD5 19350166a026d33c95f80635b1425be3
BLAKE2b-256 8d208fc26f1f2024f20196e646e6d7d4c2fe9e3e81d9d9353d88c537844ed108

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6d42f6553b5bf251ac69432cecbe6013cfa44290c1c4cc14b7d2315f37fd7f0a
MD5 d78af86b31b0cc63a0b5e5749ad843e0
BLAKE2b-256 0a271625dd741692953d5d128b49de51a02d34fa2f6eb360b21abcdc21eb2d1a

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp311-cp311-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp311-cp311-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 e76d42a049e1ce66815a01f52339cd381944cefbdf620608e7b9092be0a9e03d
MD5 d15349a16bd56ed84de3a1897ea5b315
BLAKE2b-256 6a58583d198133f126b7e447a1a56ddf1034ef36f481755fc6ecfbed3d2c1c14

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9c77f2221606c0ec74493e5f2b0d23940bd05e106284866d181f04464d5705cc
MD5 8f5675b4802fa7f64af83aa63dee26c3
BLAKE2b-256 e019624061d3c1be91cec07b0501a7daad431059a6a8ffbfc07a0be913112c86

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp310-cp310-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp310-cp310-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 f9305efee15fa54489a3831be414b84abeb73783003b4dc713c540316f308285
MD5 2d061e42dd9fd850593b23951e228eaa
BLAKE2b-256 dbe208c3a4818f9d9c57efe8df25f3e786146b46b51195d40adb6112d7a29af9

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for qborsh-1.0.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e9935243a9f9fe9ebd259ae9824c8aaf43a3ff616876a510373ef38f41c32b21
MD5 e16c158afe802eee748f3f2d47c91fcb
BLAKE2b-256 76b18f8e01b26278aca5dac5c579605d3820069381e998c2cb5ba24a911c48be

See more details on using hashes here.

Provenance

The following attestation bundles were made for qborsh-1.0.1-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