Skip to content

feat(core): Add zero-copy as_bytes to ScriptPubkeyExt#190

Open
alexanderwiederin wants to merge 1 commit into
sedited:masterfrom
alexanderwiederin:as-bytes
Open

feat(core): Add zero-copy as_bytes to ScriptPubkeyExt#190
alexanderwiederin wants to merge 1 commit into
sedited:masterfrom
alexanderwiederin:as-bytes

Conversation

@alexanderwiederin
Copy link
Copy Markdown
Collaborator

@alexanderwiederin alexanderwiederin commented May 27, 2026

closes #20

Changes

Adds a zero-copy as_bytes method to ScriptPubkeyExt that returns a slice directly into kernel-managed memory valid for the lifetime of the script object.

Motivation

to_bytes allocates a Vec<u8> on every call. For callers that only need to inspect or pass along the raw bytes without owning them, as_bytes avoids that allocation.

Why this is not applied to other types

Scripts are opaque byte sequences - as_bytes is the complete and useful representation. Types like Block and Transaction have internal structure that callers need to deserialise to use meaningfully.

to_bytes allocates a Vec<u8> on every call. as_bytes returns a slice
directly into kernel-managed memory by capturing the pointer handed to
the writer callback, which btck_script_pubkey_to_bytes invokes
synchronously before returning.
@rustaceanrob
Copy link
Copy Markdown

LGTM. For the use case of silent payments this would save a vector allocation for each output script that is not a P2TR output, very useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feat: Script Type Detection

2 participants