Skip to content

TensorStorageFactory violates its own copy/borrow contracts: "zero-copy" borrowFloatArray copies, "borrowed" fromTensorData owns #927

Closed

Description

@michalharakal

Two methods in skainet-lang-core/src/commonMain/kotlin/sk/ainet/lang/tensor/storage/TensorStorageFactory.kt do the opposite of what their docs and names promise:

  1. borrowFloatArray (line ~38) — KDoc says "(zero-copy)". The implementation allocates a fresh ByteArray(data.size * 4), re-encodes every float into it, and wraps the copy in BufferHandleFactory.borrow(...). So the caller gets a buffer labeled Borrowed (with the "caller owns, must not free" contract) that is actually a private copy — both the performance promise and the ownership label are wrong.

  2. fromTensorData (line ~128) — KDoc: "The underlying data is borrowed (not copied)." The FloatArrayTensorData / IntArrayTensorData branches call BufferHandleFactory.owned(...), which copies into a new array and labels it Owned. Only the packed-quant branches actually borrow. The else branch does data.copyToFloatArray() and then fromFloatArray(...) — a double copy.

Why it matters: BufferHandle.Ownership is the storage layer's authority on who may mutate/free a buffer, and MemoryTracker reports are keyed off it. Mislabeling copies as borrows makes both untrustworthy exactly where the layer is supposed to bring clarity, and hides real allocation cost from anyone profiling loads.

Suggested fix: make the labels truthful — borrowFloatArray should either genuinely alias (if a float-view representation is added) or be renamed/documented as an owned conversion; fromTensorData should borrow the backing arrays it can see (FloatArrayTensorData.buffer etc. are right there) and only copy in the else branch, with the doc updated to say which is which.

Happy to open a PR.

Activity

  1. added a commit that references this issue on Aug 10, 2026
  2. added 2 commits that reference this issue on Aug 11, 2026
    e8c50ce
    1334286
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions