zerocopy

package
v0.6.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package zerocopy verifies at runtime that a claimed zero-copy handoff really shares memory rather than copying it.

The probe is the only evidence that distinguishes an alias from a copy: a fast copy has the same timing as an alias, and APIs can fall back to copying silently. Check writes sentinel bytes through the producer's view and confirms each write is observed through the consumer's view:

if err := zerocopy.Check(producer, consumer); err != nil {
	log.Fatalf("handoff is a copy: %v", err)
}

For consumers whose view must be re-read or synchronized after a write (a GPU array, another process), use CheckFunc with a read function that performs the synchronization and returns the consumer's current bytes.

The probe proves visibility, not mechanism: a read function that re-copies from the producer's memory on every call is indistinguishable from an alias. That is impossible across a real device or process boundary, which is where the probe is meant to run.

Example
package main

import (
	"fmt"

	"github.com/tmc/apple/x/zerocopy"
)

func main() {
	buf := make([]byte, 1024)

	alias := buf[:512] // shares memory with buf
	fmt.Println("alias:", zerocopy.Check(alias, buf))

	copied := append([]byte(nil), buf...) // does not
	err := zerocopy.Check(buf, copied)
	fmt.Println("copy fails:", err != nil)
}
Output:
alias: <nil>
copy fails: true

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(w, r []byte) error

Check verifies that w and r are views of the same memory: it writes sentinel bytes through w at several offsets and confirms each write is observed through r, restoring w's original contents before returning. A non-nil error means r holds a copy (or a stale snapshot) of w.

r must be at least as long as w; probed offsets refer to the same underlying byte in both views.

func CheckFunc

func CheckFunc(w []byte, read func() ([]byte, error)) error

CheckFunc is Check for consumers whose view must be re-read or synchronized after each write. read performs whatever synchronization the consumer needs (evaluate a lazy array, message another process) and returns the consumer's current view of the same bytes as w. read is called at least twice per probed offset, so it must be repeatable.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL