Skip to content

Commit 6fe92f9

Browse files
wesenmaaslalani
andauthored
perf(textarea): implement wrapping memoization
* Improve textarea performance by caching the wrap results * Add memoization utility * Fix failing github jobs due to requirement for 1.18 * Address linting issues * Fix redundant docstrings * fix: minor changes to memoization * fix: soft-wrapped hidden line numbers * perf: switch to `uniseg.StringWidth` --------- Co-authored-by: Maas Lalani <[email protected]>
1 parent ec88302 commit 6fe92f9

File tree

9 files changed

+426
-52
lines changed

9 files changed

+426
-52
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ jobs:
44
test:
55
strategy:
66
matrix:
7-
go-version: [~1.17, ^1]
7+
go-version: [~1.18, ^1]
88
os: [ubuntu-latest, macos-latest, windows-latest]
99
runs-on: ${{ matrix.os }}
1010
env:

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ jobs:
55
coverage:
66
strategy:
77
matrix:
8-
go-version: [^1]
8+
go-version: [^1.18]
99
os: [ubuntu-latest]
1010
runs-on: ${{ matrix.os }}
1111
env:

.github/workflows/lint-soft.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Install Go
1717
uses: actions/setup-go@v5
1818
with:
19-
go-version: ^1
19+
go-version: ^1.18
2020

2121
- uses: actions/checkout@v4
2222
- name: golangci-lint

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
- name: Install Go
1717
uses: actions/setup-go@v5
1818
with:
19-
go-version: ^1
19+
go-version: ^1.18
2020

2121
- uses: actions/checkout@v4
2222
- name: golangci-lint

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/charmbracelet/bubbles
22

3-
go 1.17
3+
go 1.18
44

55
require (
66
github.com/atotto/clipboard v0.1.4
@@ -12,6 +12,7 @@ require (
1212
github.com/mattn/go-runewidth v0.0.15
1313
github.com/muesli/reflow v0.3.0
1414
github.com/muesli/termenv v0.15.2
15+
github.com/rivo/uniseg v0.4.4
1516
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f
1617
)
1718

@@ -23,7 +24,6 @@ require (
2324
github.com/mattn/go-localereader v0.0.1 // indirect
2425
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
2526
github.com/muesli/cancelreader v0.2.2 // indirect
26-
github.com/rivo/uniseg v0.2.0 // indirect
2727
golang.org/x/sync v0.1.0 // indirect
2828
golang.org/x/sys v0.12.0 // indirect
2929
golang.org/x/term v0.6.0 // indirect

go.sum

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D
2121
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
2222
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
2323
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
24-
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
25-
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2624
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
2725
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
2826
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
@@ -34,42 +32,18 @@ github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKt
3432
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
3533
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
3634
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
37-
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
3835
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
36+
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
37+
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
3938
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f h1:MvTmaQdww/z0Q4wrYjDSCcZ78NoftLQyHBSLW/Cx79Y=
4039
github.com/sahilm/fuzzy v0.1.1-0.20230530133925-c48e322e2a8f/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
41-
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
42-
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
43-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
44-
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
45-
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
46-
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
47-
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
48-
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
49-
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
5040
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
5141
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
52-
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
53-
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
54-
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
55-
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
56-
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
57-
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5842
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
5943
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
60-
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
6144
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
6245
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
63-
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
64-
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
6546
golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw=
6647
golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U=
67-
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
68-
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
69-
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
7048
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
7149
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
72-
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
73-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
74-
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
75-
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package memoization
2+
3+
import (
4+
"container/list"
5+
"crypto/sha256"
6+
"fmt"
7+
"sync"
8+
)
9+
10+
// Hasher is an interface that requires a Hash method. The Hash method is
11+
// expected to return a string representation of the hash of the object.
12+
type Hasher interface {
13+
Hash() string
14+
}
15+
16+
// entry is a struct that holds a key-value pair. It is used as an element
17+
// in the evictionList of the MemoCache.
18+
type entry[T any] struct {
19+
key string
20+
value T
21+
}
22+
23+
// MemoCache is a struct that represents a cache with a set capacity. It
24+
// uses an LRU (Least Recently Used) eviction policy. It is safe for
25+
// concurrent use.
26+
type MemoCache[H Hasher, T any] struct {
27+
capacity int
28+
mutex sync.Mutex
29+
cache map[string]*list.Element // The cache holding the results
30+
evictionList *list.List // A list to keep track of the order for LRU
31+
hashableItems map[string]T // This map keeps track of the original hashable items (optional)
32+
}
33+
34+
// NewMemoCache is a function that creates a new MemoCache with a given
35+
// capacity. It returns a pointer to the created MemoCache.
36+
func NewMemoCache[H Hasher, T any](capacity int) *MemoCache[H, T] {
37+
return &MemoCache[H, T]{
38+
capacity: capacity,
39+
cache: make(map[string]*list.Element),
40+
evictionList: list.New(),
41+
hashableItems: make(map[string]T),
42+
}
43+
}
44+
45+
// Capacity is a method that returns the capacity of the MemoCache.
46+
func (m *MemoCache[H, T]) Capacity() int {
47+
return m.capacity
48+
}
49+
50+
// Size is a method that returns the current size of the MemoCache. It is
51+
// the number of items currently stored in the cache.
52+
func (m *MemoCache[H, T]) Size() int {
53+
m.mutex.Lock()
54+
defer m.mutex.Unlock()
55+
return m.evictionList.Len()
56+
}
57+
58+
// Get is a method that returns the value associated with the given
59+
// hashable item in the MemoCache. If there is no corresponding value, the
60+
// method returns nil.
61+
func (m *MemoCache[H, T]) Get(h H) (T, bool) {
62+
m.mutex.Lock()
63+
defer m.mutex.Unlock()
64+
65+
hashedKey := h.Hash()
66+
if element, found := m.cache[hashedKey]; found {
67+
m.evictionList.MoveToFront(element)
68+
return element.Value.(*entry[T]).value, true
69+
}
70+
var result T
71+
return result, false
72+
}
73+
74+
// Set is a method that sets the value for the given hashable item in the
75+
// MemoCache. If the cache is at capacity, it evicts the least recently
76+
// used item before adding the new item.
77+
func (m *MemoCache[H, T]) Set(h H, value T) {
78+
m.mutex.Lock()
79+
defer m.mutex.Unlock()
80+
81+
hashedKey := h.Hash()
82+
if element, found := m.cache[hashedKey]; found {
83+
m.evictionList.MoveToFront(element)
84+
element.Value.(*entry[T]).value = value
85+
return
86+
}
87+
88+
// Check if the cache is at capacity
89+
if m.evictionList.Len() >= m.capacity {
90+
// Evict the least recently used item from the cache
91+
toEvict := m.evictionList.Back()
92+
if toEvict != nil {
93+
evictedEntry := m.evictionList.Remove(toEvict).(*entry[T])
94+
delete(m.cache, evictedEntry.key)
95+
delete(m.hashableItems, evictedEntry.key) // if you're keeping track of original items
96+
}
97+
}
98+
99+
// Add the value to the cache and the evictionList
100+
newEntry := &entry[T]{
101+
key: hashedKey,
102+
value: value,
103+
}
104+
element := m.evictionList.PushFront(newEntry)
105+
m.cache[hashedKey] = element
106+
m.hashableItems[hashedKey] = value // if you're keeping track of original items
107+
}
108+
109+
// HString is a type that implements the Hasher interface for strings.
110+
type HString string
111+
112+
// Hash is a method that returns the hash of the string.
113+
func (h HString) Hash() string {
114+
return fmt.Sprintf("%x", sha256.Sum256([]byte(h)))
115+
}
116+
117+
// HInt is a type that implements the Hasher interface for integers.
118+
type HInt int
119+
120+
// Hash is a method that returns the hash of the integer.
121+
func (h HInt) Hash() string {
122+
return fmt.Sprintf("%x", sha256.Sum256([]byte(fmt.Sprintf("%d", h))))
123+
}

0 commit comments

Comments
 (0)