diff options
| author | Ophestra <cat@gensokyo.uk> | 2025-11-27 02:26:31 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2025-11-27 02:26:31 +0900 |
| commit | e028a61fc1567de71ba836a3a7caf64aef84b34c (patch) | |
| tree | 7a6a165e7f17f52ea43c05d283c2913e1cef3cde | |
| parent | 73987be7d4c850f8b95babb8f68e6ccb6cafcee4 (diff) | |
internal/pipewire: preallocate for known size
This is still not efficient by any means, but it should eliminate most non-reflect allocation (all allocation if PODMarshaler is not used).
Signed-off-by: Ophestra <cat@gensokyo.uk>
| -rw-r--r-- | internal/pipewire/core.go | 18 | ||||
| -rw-r--r-- | internal/pipewire/pod.go | 8 |
2 files changed, 13 insertions, 13 deletions
diff --git a/internal/pipewire/core.go b/internal/pipewire/core.go index 1ef2f790..57e1f5b0 100644 --- a/internal/pipewire/core.go +++ b/internal/pipewire/core.go @@ -172,10 +172,8 @@ type CoreHello struct { // Size satisfies [KnownSize] with a constant value. func (c *CoreHello) Size() Word { return SizePrefix + Size(SizeInt) } -// MarshalBinary satisfies [encoding.BinaryMarshaler] via [MarshalAppend]. -func (c *CoreHello) MarshalBinary() ([]byte, error) { - return MarshalAppend(make([]byte, 0, 24), c) -} +// MarshalBinary satisfies [encoding.BinaryMarshaler] via [Marshal]. +func (c *CoreHello) MarshalBinary() ([]byte, error) { return Marshal(c) } // UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal]. func (c *CoreHello) UnmarshalBinary(data []byte) error { return Unmarshal(data, c) } @@ -198,10 +196,8 @@ type CoreSync struct { // Size satisfies [KnownSize] with a constant value. func (c *CoreSync) Size() Word { return SizePrefix + Size(SizeInt) + Size(SizeInt) } -// MarshalBinary satisfies [encoding.BinaryMarshaler] via [MarshalAppend]. -func (c *CoreSync) MarshalBinary() ([]byte, error) { - return MarshalAppend(make([]byte, 0, 40), c) -} +// MarshalBinary satisfies [encoding.BinaryMarshaler] via [Marshal]. +func (c *CoreSync) MarshalBinary() ([]byte, error) { return Marshal(c) } // UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal]. func (c *CoreSync) UnmarshalBinary(data []byte) error { return Unmarshal(data, c) } @@ -224,10 +220,8 @@ type CoreGetRegistry struct { // Size satisfies [KnownSize] with a constant value. func (c *CoreGetRegistry) Size() Word { return SizePrefix + Size(SizeInt) + Size(SizeInt) } -// MarshalBinary satisfies [encoding.BinaryMarshaler] via [MarshalAppend]. -func (c *CoreGetRegistry) MarshalBinary() ([]byte, error) { - return MarshalAppend(make([]byte, 0, 40), c) -} +// MarshalBinary satisfies [encoding.BinaryMarshaler] via [Marshal]. +func (c *CoreGetRegistry) MarshalBinary() ([]byte, error) { return Marshal(c) } // UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal]. func (c *CoreGetRegistry) UnmarshalBinary(data []byte) error { return Unmarshal(data, c) } diff --git a/internal/pipewire/pod.go b/internal/pipewire/pod.go index 80c798e8..3a94db5d 100644 --- a/internal/pipewire/pod.go +++ b/internal/pipewire/pod.go @@ -125,7 +125,13 @@ type UnsupportedSizeError int func (e UnsupportedSizeError) Error() string { return "size out of range: " + strconv.Itoa(int(e)) } // Marshal returns the PipeWire POD encoding of v. -func Marshal(v any) ([]byte, error) { return MarshalAppend(make([]byte, 0), v) } +func Marshal(v any) ([]byte, error) { + var data []byte + if s, ok := v.(KnownSize); ok { + data = make([]byte, 0, s.Size()) + } + return MarshalAppend(data, v) +} // MarshalAppend appends the PipeWire POD encoding of v to data. func MarshalAppend(data []byte, v any) ([]byte, error) { |
