Skip to content

Conversation

@clipperhouse
Copy link
Contributor

@clipperhouse clipperhouse commented Nov 21, 2025

  • I have read CONTRIBUTING.md.
  • I have created a discussion that was approved by a maintainer (for new features).

A little optimization on maxRuneWidth, looks like ~2x.

goos: darwin
goarch: arm64
pkg: github.com/charmbracelet/lipgloss
cpu: Apple M2

BenchmarkMaxRuneWidth/Blank/Before-8  	       6.832 ns/op	       0 B/op	       0 allocs/op
BenchmarkMaxRuneWidth/Blank/After-8   	       2.110 ns/op	       0 B/op	       0 allocs/op

BenchmarkMaxRuneWidth/Markdown/Before-8        6.755 ns/op	       0 B/op	       0 allocs/op
BenchmarkMaxRuneWidth/Markdown/After-8         2.060 ns/op	       0 B/op	       0 allocs/op

BenchmarkMaxRuneWidth/Normal/Before-8         32.11 ns/op	       0 B/op	       0 allocs/op
BenchmarkMaxRuneWidth/Normal/After-8          14.55 ns/op	       0 B/op	       0 allocs/op

BenchmarkMaxRuneWidth/Rounded/Before-8        30.79 ns/op	       0 B/op	       0 allocs/op
BenchmarkMaxRuneWidth/Rounded/After-8         14.57 ns/op	       0 B/op	       0 allocs/op

BenchmarkMaxRuneWidth/Block/Before-8          33.84 ns/op	       0 B/op	       0 allocs/op
BenchmarkMaxRuneWidth/Block/After-8           14.65 ns/op	       0 B/op	       0 allocs/op

BenchmarkMaxRuneWidth/Emoji/Before-8          29.45 ns/op	       0 B/op	       0 allocs/op
BenchmarkMaxRuneWidth/Emoji/After-8           15.38 ns/op	       0 B/op	       0 allocs/op

@clipperhouse clipperhouse force-pushed the clipperhouse/maxrune-performance branch 2 times, most recently from b593eff to da58464 Compare November 22, 2025 19:30
@clipperhouse clipperhouse marked this pull request as ready for review November 22, 2025 19:32
Copilot AI review requested due to automatic review settings November 22, 2025 19:32
Copilot finished reviewing on behalf of clipperhouse November 22, 2025 19:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR optimizes the maxRuneWidth function in lipgloss by replacing the uniseg package with clipperhouse/displaywidth, achieving approximately 2x performance improvement. The optimization includes fast-path handling for empty and single-byte strings, along with a more efficient grapheme iteration approach for multi-character strings.

  • Replaced uniseg.FirstGraphemeClusterInString with displaywidth.StringGraphemes for better performance
  • Added fast-path optimizations for empty and single-byte strings
  • Simplified getBorderEdgeWidth implementation using built-in max() function

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated no comments.

File Description
borders.go Refactored maxRuneWidth with optimized implementation using displaywidth package and added fast paths; simplified getBorderEdgeWidth to use max()
borders_test.go Added comprehensive benchmark comparing old vs new maxRuneWidth implementations with various character types; retained old implementation for comparison
go.mod Added github.com/clipperhouse/displaywidth v0.6.0 dependency
go.sum Added checksums for new displaywidth dependency and its transitive dependencies (stringish, uax29)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@clipperhouse clipperhouse force-pushed the clipperhouse/maxrune-performance branch from e22f2cb to 1cf8b54 Compare November 30, 2025 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant