Skip to content

proxystore.endpoint.p2p.bindings

Patches to the iroh bindings.

The iroh bindings are generated by uniffi. Bindings generated by any uniffi version older than 0.32.0 (including 0.31.x patch releases) copy the data of each bytes argument passed to Rust one byte at a time in a Python loop, which limits the throughput of peer connections. uniffi 0.32.0 fixes this (mozilla/uniffi-rs#2824).

The version of uniffi which generated the bindings is not available at runtime, so patch_buffer_write() only replaces the copy if the bindings still contain the per-byte loop. This module can be removed once iroh requires bindings generated by uniffi 0.32.0 or newer.

patch_buffer_write

patch_buffer_write(builder: type | None = None) -> bool

Replace the per-byte copy of bytes arguments in the iroh bindings.

Parameters:

  • builder (type | None, default: None ) –

    Buffer builder class to patch. Defaults to the _UniffiRustBufferBuilder of the iroh bindings.

Returns:

  • bool –

    If the write() method of the builder was patched. The method is only patched if it copies data one byte at a time.

Source code in proxystore/endpoint/p2p/bindings.py
def patch_buffer_write(builder: type | None = None) -> bool:
    """Replace the per-byte copy of bytes arguments in the iroh bindings.

    Args:
        builder: Buffer builder class to patch. Defaults to the
            `_UniffiRustBufferBuilder` of the iroh bindings.

    Returns:
        If the `write()` method of the builder was patched. The method is \
        only patched if it copies data one byte at a time.
    """
    if builder is None:
        builder = getattr(iroh_ffi, '_UniffiRustBufferBuilder', None)
    write = getattr(builder, 'write', None)
    if builder is None or write is None:
        logger.debug('Skipped patching iroh bindings: builder not found')
        return False

    try:
        source = inspect.getsource(write)
    except (OSError, TypeError):
        logger.debug('Skipped patching iroh bindings: source not found')
        return False

    if _PER_BYTE_LOOP not in source:
        return False

    builder.write = _memmove_write  # type: ignore[attr-defined]
    logger.debug('Patched per-byte copy in iroh bindings')
    return True