Skip to content

TinyBench Source Code Analysis

Data Slot Management

put — Put Data onto the Bench

tiny_bench_err_t tiny_bench_put(tiny_bench_t *bench,
                                const char *name,
                                void *data, size_t len,
                                tiny_type_t type)

Logic: 1. Validate parameters (bench, name non-null) 2. Validate name length (does not exceed TINY_BENCH_NAME_LEN) 3. Check if a slot with the same name already exists → return ERR_EXISTS 4. Find a free slot → return ERR_FULL if full 5. Copy name, store pointer/length/type → mark used = true

Zero-Copy Key Point

Only name is copied into the internal table; data stores only a pointer. The caller guarantees data validity until get.

get — Get Data

tiny_bench_err_t tiny_bench_get(tiny_bench_t *bench,
                                const char *name,
                                void **out_data, size_t *out_len,
                                tiny_type_t *out_type)

Logic: 1. Find the slot matching the name 2. If out_data, out_len, or out_type is non-null, write the corresponding value 3. Return ERR_NOT_FOUND if not present

Supports existence-only check: tiny_bench_get(&bench, "x", NULL, NULL, NULL).

list_data — List All Data

int tiny_bench_list_data(tiny_bench_t *bench,
                         const char **names, int max_count)

Iterates all slots, collecting name pointers of used slots into the output array. Returns the count written.

Tool Registration Management

register_tool — Register a Tool

tiny_bench_err_t tiny_bench_register_tool(tiny_bench_t *bench,
                                          const char *name,
                                          tiny_tool_fn fn,
                                          tiny_type_t in_type,
                                          tiny_type_t out_type)

Logic: 1. Validate parameters and name length 2. Check for duplicate name → ERR_EXISTS 3. Find a free slot → ERR_FULL 4. Record name, function pointer, input/output types

get_tool_fn — Get Tool Function Pointer

This is the key function called by TinyOrch during flow execution:

tiny_bench_err_t tiny_bench_get_tool_fn(tiny_bench_t *bench,
                                         const char *name,
                                         tiny_tool_fn *out_fn,
                                         tiny_type_t *out_in,
                                         tiny_type_t *out_out)

Returns the function pointer + input/output types, allowing the caller to execute the tool and perform type checking.

Type System

const char *tiny_bench_type_name(tiny_type_t type)

Converts enum values to readable strings. Used for debug output and MQTT responses. Implemented as a simple switch-case:

switch (type) {
    case TINY_TYPE_F32:      return "f32";
    case TINY_TYPE_F32_ARR:  return "f32[]";
    // ...
}

Error Handling Pattern

All error codes are unified under the tiny_bench_err_t enum, kept separate from ESP-IDF's esp_err_t to maintain module independence. Calling pattern:

tiny_bench_err_t e = tiny_bench_put(&bench, "x", data, len, type);
if (e == TINY_BENCH_ERR_FULL) {
    // Bench is full, consider resizing or cleanup
}