internal/pipewire: preallocate for known size
All checks were successful
Test / Create distribution (push) Successful in 37s
Test / Sandbox (push) Successful in 2m23s
Test / Hakurei (push) Successful in 3m19s
Test / Hpkg (push) Successful in 4m13s
Test / Sandbox (race detector) (push) Successful in 4m27s
Test / Hakurei (race detector) (push) Successful in 5m10s
Test / Flake checks (push) Successful in 1m27s
All checks were successful
Test / Create distribution (push) Successful in 37s
Test / Sandbox (push) Successful in 2m23s
Test / Hakurei (push) Successful in 3m19s
Test / Hpkg (push) Successful in 4m13s
Test / Sandbox (race detector) (push) Successful in 4m27s
Test / Hakurei (race detector) (push) Successful in 5m10s
Test / Flake checks (push) Successful in 1m27s
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>
This commit is contained in:
parent
73987be7d4
commit
e028a61fc1
@ -172,10 +172,8 @@ type CoreHello struct {
|
|||||||
// Size satisfies [KnownSize] with a constant value.
|
// Size satisfies [KnownSize] with a constant value.
|
||||||
func (c *CoreHello) Size() Word { return SizePrefix + Size(SizeInt) }
|
func (c *CoreHello) Size() Word { return SizePrefix + Size(SizeInt) }
|
||||||
|
|
||||||
// MarshalBinary satisfies [encoding.BinaryMarshaler] via [MarshalAppend].
|
// MarshalBinary satisfies [encoding.BinaryMarshaler] via [Marshal].
|
||||||
func (c *CoreHello) MarshalBinary() ([]byte, error) {
|
func (c *CoreHello) MarshalBinary() ([]byte, error) { return Marshal(c) }
|
||||||
return MarshalAppend(make([]byte, 0, 24), c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal].
|
// UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal].
|
||||||
func (c *CoreHello) UnmarshalBinary(data []byte) error { return Unmarshal(data, c) }
|
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.
|
// Size satisfies [KnownSize] with a constant value.
|
||||||
func (c *CoreSync) Size() Word { return SizePrefix + Size(SizeInt) + Size(SizeInt) }
|
func (c *CoreSync) Size() Word { return SizePrefix + Size(SizeInt) + Size(SizeInt) }
|
||||||
|
|
||||||
// MarshalBinary satisfies [encoding.BinaryMarshaler] via [MarshalAppend].
|
// MarshalBinary satisfies [encoding.BinaryMarshaler] via [Marshal].
|
||||||
func (c *CoreSync) MarshalBinary() ([]byte, error) {
|
func (c *CoreSync) MarshalBinary() ([]byte, error) { return Marshal(c) }
|
||||||
return MarshalAppend(make([]byte, 0, 40), c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal].
|
// UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal].
|
||||||
func (c *CoreSync) UnmarshalBinary(data []byte) error { return Unmarshal(data, c) }
|
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.
|
// Size satisfies [KnownSize] with a constant value.
|
||||||
func (c *CoreGetRegistry) Size() Word { return SizePrefix + Size(SizeInt) + Size(SizeInt) }
|
func (c *CoreGetRegistry) Size() Word { return SizePrefix + Size(SizeInt) + Size(SizeInt) }
|
||||||
|
|
||||||
// MarshalBinary satisfies [encoding.BinaryMarshaler] via [MarshalAppend].
|
// MarshalBinary satisfies [encoding.BinaryMarshaler] via [Marshal].
|
||||||
func (c *CoreGetRegistry) MarshalBinary() ([]byte, error) {
|
func (c *CoreGetRegistry) MarshalBinary() ([]byte, error) { return Marshal(c) }
|
||||||
return MarshalAppend(make([]byte, 0, 40), c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal].
|
// UnmarshalBinary satisfies [encoding.BinaryUnmarshaler] via [Unmarshal].
|
||||||
func (c *CoreGetRegistry) UnmarshalBinary(data []byte) error { return Unmarshal(data, c) }
|
func (c *CoreGetRegistry) UnmarshalBinary(data []byte) error { return Unmarshal(data, c) }
|
||||||
|
|||||||
@ -125,7 +125,13 @@ type UnsupportedSizeError int
|
|||||||
func (e UnsupportedSizeError) Error() string { return "size out of range: " + strconv.Itoa(int(e)) }
|
func (e UnsupportedSizeError) Error() string { return "size out of range: " + strconv.Itoa(int(e)) }
|
||||||
|
|
||||||
// Marshal returns the PipeWire POD encoding of v.
|
// 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.
|
// MarshalAppend appends the PipeWire POD encoding of v to data.
|
||||||
func MarshalAppend(data []byte, v any) ([]byte, error) {
|
func MarshalAppend(data []byte, v any) ([]byte, error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user