run_default


Overview

The `run_default` file is a lightweight Python benchmarking script designed to test the performance of serializing complex Python objects with the **orjson** library, specifically focusing on the behavior when using a custom fallback serialization function (`default`) and the option to serialize NumPy data types (`OPT_SERIALIZE_NUMPY`). It simulates a realistic scenario where large nested Python objects contain instances of user-defined classes that are not natively serializable, necessitating a fallback mechanism.

The script configures CPU affinity for consistent benchmarking, constructs a nested list of custom objects, and repeatedly serializes this structure using orjson's `dumps` method with custom fallback and NumPy serialization options enabled. This benchmarking helps measure the overhead and efficiency of fallback serialization in orjson.


File Purpose and Functionality


Detailed Explanation of Components

Imports and CPU Affinity Setup

import sys
import os

os.sched_setaffinity(os.getpid(), {0, 1})

Class: Custom

class Custom:
    pass

Function: default

def default(_):
    return None

**Usage Example:**

from orjson import dumps, OPT_SERIALIZE_NUMPY

class Custom:
    pass

def default(_):
    return None

obj = [[Custom()] * 1000] * 10
json_bytes = dumps(obj, default, OPT_SERIALIZE_NUMPY)
print(json_bytes)  # Output includes 'null' in place of Custom instances

Main Benchmark Logic

n = int(sys.argv[1]) if len(sys.argv) >= 2 else 10000

obj = [[Custom()] * 1000] * 10
for _ in range(n):
    dumps(obj, default, OPT_SERIALIZE_NUMPY)

Important Implementation Details


Interaction with Other System Components


Usage

Run the script from the command line, optionally specifying the number of iterations:

python3 run_default 5000

This runs the serialization benchmark 5000 times. Without arguments, it defaults to 10,000 iterations.


Example Output (Conceptual)

The script itself produces no output, but when run under a timing tool such as `time` or a profiler, you might see:

real    0m1.234s
user    0m1.200s
sys     0m0.020s

Indicating the time taken to perform 10,000 serializations with fallback.


Mermaid Diagram: Structure of run_default

flowchart TD
    A[Set CPU Affinity to cores 0,1]
    B[Define class Custom (unsupported type)]
    C[Define fallback function default -> returns None]
    D[Create nested list obj with Custom instances]
    E[Parse iteration count n from argv (default 10000)]
    F[Loop n times]
    G[Call orjson.dumps(obj, default, OPT_SERIALIZE_NUMPY)]

    A --> B --> C --> D --> E --> F --> G

This flowchart illustrates the sequential setup and execution steps in the script.


Summary

The `run_default` script benchmarks orjson’s serialization performance when handling complex Python objects containing unsupported types by leveraging a fallback serialization function. By setting CPU affinity and repeatedly serializing a large nested list of custom objects, it enables precise measurement of the overhead and behavior of orjson’s fallback mechanism combined with NumPy serialization support. This file is an essential part of the benchmarking suite aimed at validating orjson’s extensibility and performance in realistic serialization scenarios involving user-defined Python types.