Files
gio/io/semantic/semantic.go
T
Dominik Honnef b4d93379c4 op: don't allocate for each string reference
When storing a string in an interface value that escapes, Go has to heap
allocate space for the string header, as interface values can only store
pointers. In text-heavy applications, this can lead to hundreds of
allocations per frame due to semantic.LabelOp, the primary user of
string-typed references in ops.

Instead of allocating each string header individually, provide a slice
of strings to store string-typed references in, and store pointers into
this slice as the actual references. This only allocates when resizing
the slice's backing array, and averages out to no allocations, as the
backing array gets reused between calls to Ops.Reset.

We introduce two new functions, Write1String and Write2String, which
make use of this new slice for their last argument. We could've
automated this in the existing Write1 and Write2 methods, but that would
require type assertions on each call, and the vast majority of ops do
not make use of strings.

Signed-off-by: Dominik Honnef <dominik@honnef.co>
2023-09-02 09:02:39 -06:00

92 lines
2.0 KiB
Go

// SPDX-License-Identifier: Unlicense OR MIT
// Package semantic provides operations for semantic descriptions of a user
// interface, to facilitate presentation and interaction in external software
// such as screen readers.
//
// Semantic descriptions are organized in a tree, with clip operations as
// nodes. Operations in this package are associated with the current semantic
// node, that is the most recent pushed clip operation.
package semantic
import (
"gioui.org/internal/ops"
"gioui.org/op"
)
// LabelOp provides the content of a textual component.
type LabelOp string
// DescriptionOp describes a component.
type DescriptionOp string
// ClassOp provides the component class.
type ClassOp int
const (
Unknown ClassOp = iota
Button
CheckBox
Editor
RadioButton
Switch
)
// SelectedOp describes the selected state for components that have
// boolean state.
type SelectedOp bool
// DisabledOp describes the disabled state.
type DisabledOp bool
func (l LabelOp) Add(o *op.Ops) {
data := ops.Write1String(&o.Internal, ops.TypeSemanticLabelLen, string(l))
data[0] = byte(ops.TypeSemanticLabel)
}
func (d DescriptionOp) Add(o *op.Ops) {
data := ops.Write1String(&o.Internal, ops.TypeSemanticDescLen, string(d))
data[0] = byte(ops.TypeSemanticDesc)
}
func (c ClassOp) Add(o *op.Ops) {
data := ops.Write(&o.Internal, ops.TypeSemanticClassLen)
data[0] = byte(ops.TypeSemanticClass)
data[1] = byte(c)
}
func (s SelectedOp) Add(o *op.Ops) {
data := ops.Write(&o.Internal, ops.TypeSemanticSelectedLen)
data[0] = byte(ops.TypeSemanticSelected)
if s {
data[1] = 1
}
}
func (d DisabledOp) Add(o *op.Ops) {
data := ops.Write(&o.Internal, ops.TypeSemanticDisabledLen)
data[0] = byte(ops.TypeSemanticDisabled)
if d {
data[1] = 1
}
}
func (c ClassOp) String() string {
switch c {
case Unknown:
return "Unknown"
case Button:
return "Button"
case CheckBox:
return "CheckBox"
case Editor:
return "Editor"
case RadioButton:
return "RadioButton"
case Switch:
return "Switch"
default:
panic("invalid ClassOp")
}
}