Skip to content

Commit 05eb156

Browse files
committed
all: avoid unneeded heap allocations, especially since embedded display value is interface anyhow
Signed-off-by: deadprogram <ron@hybridgroup.com>
1 parent 34f5899 commit 05eb156

File tree

4 files changed

+16
-14
lines changed

4 files changed

+16
-14
lines changed

concrete.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ func (glyph Glyph) Draw(display drivers.Displayer, x int16, y int16, color color
5353
}
5454

5555
// Info returns glyph information.
56-
func (glyph Glyph) Info() *GlyphInfo {
57-
return &GlyphInfo{
56+
func (glyph Glyph) Info() GlyphInfo {
57+
return GlyphInfo{
5858
Rune: glyph.Rune,
5959
Width: glyph.Width,
6060
Height: glyph.Height,
@@ -69,8 +69,10 @@ func (f *Font) GetYAdvance() uint8 {
6969
return f.YAdvance
7070
}
7171

72+
var emptyBitmap [1]byte
73+
7274
// GetGlyph returns the glyph corresponding to the specified rune in the font.
73-
func (font *Font) GetGlyph(r rune) Glypher {
75+
func (font *Font) GetGlyph(r rune) Glyph {
7476
s := 0
7577
e := len(font.Glyphs) - 1
7678

@@ -92,7 +94,7 @@ func (font *Font) GetGlyph(r rune) Glypher {
9294
XAdvance: font.Glyphs[0].Info().XAdvance,
9395
XOffset: font.Glyphs[0].Info().XOffset,
9496
YOffset: font.Glyphs[0].Info().YOffset,
95-
Bitmaps: []byte{0},
97+
Bitmaps: emptyBitmap[:],
9698
}
9799
return g
98100
}

const2bit/const2bit.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func (glyph Glyph) Draw(display drivers.Displayer, x int16, y int16, c color.RGB
6565
}
6666

6767
// Info returns glyph information.
68-
func (glyph Glyph) Info() *tinyfont.GlyphInfo {
69-
return &tinyfont.GlyphInfo{
68+
func (glyph Glyph) Info() tinyfont.GlyphInfo {
69+
return tinyfont.GlyphInfo{
7070
Rune: glyph.Rune,
7171
Width: glyph.Width,
7272
Height: glyph.Height,

display.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,16 @@ type RotatedDisplay struct {
1313
Y int16
1414
}
1515

16-
func NewRotatedDisplay(display drivers.Displayer, rotation Rotation, x, y int16) *RotatedDisplay {
17-
return &RotatedDisplay{
16+
func NewRotatedDisplay(display drivers.Displayer, rotation Rotation, x, y int16) RotatedDisplay {
17+
return RotatedDisplay{
1818
display: display,
1919
Rotation: rotation,
2020
X: x,
2121
Y: y,
2222
}
2323
}
2424

25-
func (d *RotatedDisplay) Size() (int16, int16) {
25+
func (d RotatedDisplay) Size() (int16, int16) {
2626
x, y := d.display.Size()
2727
if d.Rotation == NO_ROTATION {
2828
return x, y
@@ -35,7 +35,7 @@ func (d *RotatedDisplay) Size() (int16, int16) {
3535
}
3636
}
3737

38-
func (d *RotatedDisplay) SetPixel(x, y int16, c color.RGBA) {
38+
func (d RotatedDisplay) SetPixel(x, y int16, c color.RGBA) {
3939
if d.Rotation == NO_ROTATION {
4040
d.display.SetPixel(d.X+x, d.Y+y, c)
4141
} else if d.Rotation == ROTATION_90 {
@@ -47,6 +47,6 @@ func (d *RotatedDisplay) SetPixel(x, y int16, c color.RGBA) {
4747
}
4848
}
4949

50-
func (d *RotatedDisplay) Display() error {
50+
func (d RotatedDisplay) Display() error {
5151
return d.display.Display()
5252
}

tinyfont.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ type GlyphInfo struct {
3030

3131
// Fonter is an interface that represents a set of glyphs.
3232
type Fonter interface {
33-
GetGlyph(r rune) Glypher
33+
GetGlyph(r rune) Glyph
3434
GetYAdvance() uint8
3535
}
3636

3737
// Glypher is glyph itself, and it knows how to draw itself.
3838
type Glypher interface {
3939
Draw(display drivers.Displayer, x int16, y int16, color color.RGBA)
40-
Info() *GlyphInfo
40+
Info() GlyphInfo
4141
}
4242

4343
// DrawChar sets a single rune in the buffer of the display.
@@ -123,6 +123,6 @@ func LineWidth(f Fonter, str string) (innerWidth uint32, outboxWidth uint32) {
123123
}
124124

125125
// GetGlyph returns the glyph corresponding to the specified rune in the font.
126-
func GetGlyph(f Fonter, r rune) Glypher {
126+
func GetGlyph(f Fonter, r rune) Glyph {
127127
return f.GetGlyph(r)
128128
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy