Skip to content

v0.1.1

Choose a tag to compare

@ableinc ableinc released this 28 May 20:40
· 1 commit to main since this release

This release was focused on performance, specifically memory allocation for Go structs. Below you'll find cost and savings on a 64-bit OS:

RunwareOptions (original struct)

Total size: 136 bytes
Wasted: 17 bytes (12.5%)

Original layout:

type RunwareOptions struct {
	TaskType        TaskType
	TaskUUID        string
	Prompt          string
	Width           Definition
	Height          Definition
	Model           string
	NumberOfResults int8
	UploadEndpoint  string
	CheckNSFW       bool
	IncludeCost     bool
	OutputType      OutputType
	OutputFormat    OutputFormat
}

RunwareOptions (optimized struct)

Total size: 120 bytes
Saved: 16 bytes (0.8333333333333334% waste)
Why is there still waste? There is still 1-byte padding added for memory alignment. This is unavoidable.

Optimized layout:

type RunwareOptions struct {
	TaskType        TaskType
	TaskUUID        string
	Prompt          string
	Model           string
	UploadEndpoint  string
	OutputType      OutputType
	OutputFormat    OutputFormat
	Width           Definition
	Height          Definition
	NumberOfResults uint8
	CheckNSFW       bool
	IncludeCost     bool
}

Memory savings at scale: 15.26 MB per million allocations (or 3-5 MP3 songs)

Field breakdown:

TaskType: Offset=0, Size=16, Type=temp.TaskType, Align=8
TaskUUID: Offset=16, Size=16, Type=string, Align=8
Prompt: Offset=32, Size=16, Type=string, Align=8
Width: Offset=48, Size=2, Type=temp.Definition, Align=2
Height: Offset=50, Size=2, Type=temp.Definition, Align=2 padding: Offset=52, Size=4
Model: Offset=56, Size=16, Type=string, Align=8
NumberOfResults: Offset=72, Size=1, Type=int8, Align=1 padding: Offset=73, Size=7
UploadEndpoint: Offset=80, Size=16, Type=string, Align=8
CheckNSFW: Offset=96, Size=1, Type=bool, Align=1
IncludeCost: Offset=97, Size=1, Type=bool, Align=1 padding: Offset=98, Size=6
OutputType: Offset=104, Size=16, Type=temp.OutputType, Align=8
OutputFormat: Offset=120, Size=16, Type=temp.OutputFormat, Align=8