Skip to content

Commit

Permalink
perf(cellbuf): optimize Equal methods using pointers and int comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Feb 4, 2025
1 parent 7fdc09e commit 1e7ecf7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
22 changes: 11 additions & 11 deletions cellbuf/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ func (c *Cell) Equal(o *Cell) bool {
c.Width == o.Width &&
c.Rune == o.Rune &&
runesEqual(c.Comb, o.Comb) &&
c.Style.Equal(o.Style) &&
c.Link.Equal(o.Link)
c.Style.Equal(&o.Style) &&
c.Link.Equal(&o.Link)
}

// Empty returns whether the cell is empty.
func (c Cell) Empty() bool {
return c.Rune == 0 &&
return c.Width == 0 &&
c.Rune == 0 &&
len(c.Comb) == 0 &&
c.Width == 0 &&
c.Style.Empty() &&
c.Link.Empty()
}
Expand Down Expand Up @@ -114,8 +114,8 @@ func (h *Link) Reset() {
}

// Equal returns whether the hyperlink is equal to the other hyperlink.
func (h Link) Equal(o Link) bool {
return h == o
func (h *Link) Equal(o *Link) bool {
return o != nil && h.URL == o.URL && h.URLID == o.URLID
}

// Empty returns whether the hyperlink is empty.
Expand Down Expand Up @@ -320,12 +320,12 @@ func (s Style) DiffSequence(o Style) string {
}

// Equal returns true if the style is equal to the other style.
func (s Style) Equal(o Style) bool {
return colorEqual(s.Fg, o.Fg) &&
func (s *Style) Equal(o *Style) bool {
return s.Attrs == o.Attrs &&
s.UlStyle == o.UlStyle &&
colorEqual(s.Fg, o.Fg) &&
colorEqual(s.Bg, o.Bg) &&
colorEqual(s.Ul, o.Ul) &&
s.Attrs == o.Attrs &&
s.UlStyle == o.UlStyle
colorEqual(s.Ul, o.Ul)
}

func colorEqual(c, o ansi.Color) bool {
Expand Down
14 changes: 5 additions & 9 deletions cellbuf/screen.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ func relativeCursorMove(s *Screen, fx, fy, tx, ty int, overwrite, useTabs bool)
if cuu := ansi.CursorUp(n); yseq == "" || len(cuu) < len(yseq) {
yseq = cuu
}
if n == 1 && fy-1 > 0 {
// TODO: Ensure we're not unintentionally scrolling the screen up.
yseq = ansi.ReverseIndex
}
}

seq.WriteString(yseq)
Expand Down Expand Up @@ -117,9 +113,9 @@ func relativeCursorMove(s *Screen, fx, fy, tx, ty int, overwrite, useTabs bool)
if overwrite && ty >= 0 {
for i := 0; i < n; i++ {
cell := s.newbuf.Cell(fx+i, ty)
if cell != nil {
if cell != nil && cell.Width > 0 {
i += cell.Width - 1
if !cell.Style.Equal(s.cur.Style) || !cell.Link.Equal(s.cur.Link) {
if !cell.Style.Equal(&s.cur.Style) || !cell.Link.Equal(&s.cur.Link) {
overwrite = false
break
}
Expand All @@ -130,7 +126,7 @@ func relativeCursorMove(s *Screen, fx, fy, tx, ty int, overwrite, useTabs bool)
if overwrite && ty >= 0 {
for i := 0; i < n; i++ {
cell := s.newbuf.Cell(fx+i, ty)
if cell != nil {
if cell != nil && cell.Width > 0 {
ovw += cell.String()
i += cell.Width - 1
} else {
Expand Down Expand Up @@ -623,15 +619,15 @@ func (s *Screen) updatePen(cell *Cell) {
link = ConvertLink(link, s.opts.Profile)
}

if !style.Equal(s.cur.Style) {
if !style.Equal(&s.cur.Style) {
seq := style.DiffSequence(s.cur.Style)
if style.Empty() && len(seq) > len(ansi.ResetStyle) {
seq = ansi.ResetStyle
}
s.buf.WriteString(seq) //nolint:errcheck
s.cur.Style = style
}
if !link.Equal(s.cur.Link) {
if !link.Equal(&s.cur.Link) {
s.buf.WriteString(ansi.SetHyperlink(link.URL, link.URLID)) //nolint:errcheck
s.cur.Link = link
}
Expand Down

0 comments on commit 1e7ecf7

Please sign in to comment.