Skip to content

Commit 0570062

Browse files
author
Dylan
committed
cleanup and slight optimization
1 parent 9d5bfee commit 0570062

File tree

2 files changed

+36
-75
lines changed

2 files changed

+36
-75
lines changed

ansiproperties.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ var (
7272
"scrollback": 3,
7373
}
7474

75-
colorMode ColorMode = Color8Bit
75+
ansiFgSeq [256]string
76+
ansiBgSeq [256]string
7677

7778
rwLock = sync.RWMutex{}
7879
)
@@ -82,7 +83,6 @@ type ansiProperties struct {
8283
bg int
8384
clear int
8485
position []uint16
85-
bold bool
8686
htmlOnly bool
8787
}
8888

@@ -146,13 +146,13 @@ func (p ansiProperties) PropagateAnsiCode(previous *ansiProperties) string {
146146
colorCode = "\033[0m"
147147
} else {
148148
if p.fg > -1 {
149-
colorCode += "\033[38;5;" + strconv.Itoa(p.fg) + `m`
149+
colorCode += ansiFgSeq[p.fg]
150150
} else if p.fg == defaultFg256 {
151151
colorCode += "\033[39m"
152152
}
153153

154154
if p.bg > -1 {
155-
colorCode += "\033[48;5;" + strconv.Itoa(p.bg) + `m`
155+
colorCode += ansiBgSeq[p.bg]
156156
} else if p.bg == defaultBg256 {
157157
colorCode += "\033[49m"
158158
}
@@ -228,3 +228,11 @@ func extractProperties(tagStr string) *ansiProperties {
228228

229229
return ret
230230
}
231+
232+
// Speed up by pre-computing these values
233+
func init() {
234+
for i := 0; i < 256; i++ {
235+
ansiFgSeq[i] = "\033[38;5;" + strconv.Itoa(i) + "m"
236+
ansiBgSeq[i] = "\033[48;5;" + strconv.Itoa(i) + "m"
237+
}
238+
}

ansitags_test.go

Lines changed: 24 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -259,77 +259,14 @@ func TestSetAliasesInvalidValue(t *testing.T) {
259259
}
260260
}
261261

262-
//
263-
// Benchmarks
264-
// cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
265-
//
266-
/*
267-
//
268-
// BenchmarkSprintf
269-
// BenchmarkSprintf-16 500000 114.7 ns/op 8 B/op 1 allocs/op
270-
//
271-
func BenchmarkSprintf(b *testing.B) {
272-
for n := 0; n < b.N; n++ {
273-
_ = fmt.Sprintf("\033[%d;%dm", 8+60, 7)
274-
}
275-
}
276-
277-
//
278-
// BenchmarkAtoI
279-
// BenchmarkAtoI-16 500000 34.44 ns/op 0 B/op 0 allocs/op
280-
//
281-
func BenchmarkAtoI(b *testing.B) {
282-
for n := 0; n < b.N; n++ {
283-
_ = "\033[" + strconv.Itoa(8+60) + ";" + strconv.Itoa(7) + "m"
284-
}
285-
}
286-
287-
//
288-
// BenchmarkConcat
289-
// BenchmarkConcat-16 500000 168857 ns/op 1403048 B/op 2 allocs/op
290-
//
291-
292-
func BenchmarkConcat(b *testing.B) {
293-
var sConcat string = ""
294-
for n := 0; n < b.N; n++ {
295-
sConcat += strconv.Itoa(n)
296-
}
297-
}
298-
299-
//
300-
// BenchmarkStringBuilder
301-
// BenchmarkStringBuilder-16 500000 38.46 ns/op 41 B/op 0 allocs/op
302-
//
303-
func BenchmarkStringBuilder(b *testing.B) {
304-
var sBuilder strings.Builder
305-
for n := 0; n < b.N; n++ {
306-
sBuilder.WriteString(strconv.Itoa(n))
307-
}
308-
}
309-
310-
// BenchmarkParseColorString
311-
// BenchmarkParseColorString-16 617618 1665 ns/op 808 B/op 16 allocs/op
312-
func BenchmarkParseColorString(b *testing.B) {
313-
for n := 0; n < b.N; n++ {
314-
Parse("This is a prefix <ansi fg=blue bg=red>This is color</ansi> This is a suffix")
315-
}
316-
}
317-
318-
// BenchmarkParseColorInt
319-
// BenchmarkParseColorInt-16 880810 1393 ns/op 712 B/op 14 allocs/op
320-
func BenchmarkParseColorInt(b *testing.B) {
321-
for n := 0; n < b.N; n++ {
322-
Parse("This is a prefix <ansi fg='9' bg='2'>This is color</ansi> This is a suffix")
323-
}
324-
}
325-
*/
326-
// Name # Run Avg Runtime Bytes Allocated # of Allocat
327-
// BenchmarkParseStreaming-16 34398 32720 ns/op 14958 B/op 297 allocs/op
262+
// cpu: Apple M3 Max
263+
// Name # Run Avg Runtime Bytes Allocated # of Allocat
264+
// BenchmarkParseStreaming-14 39277 29422 ns/op 27360 B/op 491 allocs/o
328265
func BenchmarkParseStreaming(b *testing.B) {
329266

330267
testStr := "This is text"
331-
for i := 0; i < 20; i++ {
332-
testStr = "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
268+
for i := 0; i < 5; i++ { // heavily nested tags
269+
testStr = testStr + "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
333270
}
334271

335272
reader := strings.NewReader(testStr)
@@ -343,14 +280,30 @@ func BenchmarkParseStreaming(b *testing.B) {
343280
}
344281
}
345282

346-
// BenchmarkParseNew-16 33194 35641 ns/op 23330 B/op 304 allocs/op
283+
// cpu: Apple M3 Max
284+
// Name # Run Avg Runtime Bytes Allocated # of Allocat
285+
// BenchmarkParse-14 37892 30006 ns/op 33892 B/op 497 allocs/op
347286
func BenchmarkParse(b *testing.B) {
348287

349288
testStr := "This is text"
350-
for i := 0; i < 20; i++ {
351-
testStr = "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
289+
for i := 0; i < 5; i++ { // heavily nested tags
290+
testStr = testStr + "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
352291
}
353292
for n := 0; n < b.N; n++ {
354293
Parse(testStr)
355294
}
356295
}
296+
297+
// cpu: Apple M3 Max
298+
// Name # Run Avg Runtime Bytes Allocated # of Allocat
299+
// BenchmarkParseHTML-14 40748 28125 ns/op 32295 B/op 455 allocs/op
300+
func BenchmarkParseHTML(b *testing.B) {
301+
302+
testStr := "This is text"
303+
for i := 0; i < 5; i++ { // heavily nested tags
304+
testStr = testStr + "<ansi fg=black bg=\"white\">" + testStr + "</ansi>"
305+
}
306+
for n := 0; n < b.N; n++ {
307+
Parse(testStr, HTML)
308+
}
309+
}

0 commit comments

Comments
 (0)