Bläddra i källkod

Updates/notes for profiling.

Less allocations, less garbage.
Steve Thielemann 1 år sedan
förälder
incheckning
e3e63bfbf3
4 ändrade filer med 74 tillägg och 17 borttagningar
  1. 1 15
      Makefile.win
  2. 4 0
      door/color.go
  3. 43 1
      door/line.go
  4. 26 1
      door/line_test.go

+ 1 - 15
Makefile.win

@@ -24,26 +24,12 @@ door32${EXT}: door32.go convert.go
 #	gcc -g -o door32d door32.c
 #	gcc -o door32 door32.c
 
-testdoor/fonts.go: font-util/font-util
-	./font-util/font-util extract -f "Amazon Cyan,Anarchy Blue,Unchained,Asylum,ArmageonRed,BrainDmgBlu,Boner,Descent,Remorse,Dungeon" TDFONTS.TDF TDFONTS2.TDF TDFONTS9.TDF > testdoor/fonts.go
-
 # Extract font, change colors, rename font to RedGreen.
-testdoor/rgfont.go: font-util/font-util
-	./font-util/font-util extract -f Armageddon -c 7,1 -c 4,2 TDFONTS2.TDF > testdoor/rgfont.go
-	sed -i 's/Armageddon/RedGreen/g' testdoor/rgfont.go
-
-ansi-to-go/ansi-to-go: ansi-to-go/ansi-to-go.go
-	cd ansi-to-go; ${BUILD}
 
-testdoor/art.go: testdoor/growl.ans ansi-to-go/ansi-to-go
-	./ansi-to-go/ansi-to-go testdoor/growl.ans > testdoor/art.go
 
-space-ace/space.go: space-ace/space.ans
-	./ansi-to-go/ansi-to-go space-ace/space.ans > space-ace/space.go
-	
 # ./ansi-to-go.py main space.ans > space-ace/space.go
 
-testdoor/testdoor${EXT}: testdoor/art.go testdoor/fonts.go testdoor/rgfont.go testdoor/*.go door/*.go
+testdoor/testdoor${EXT}: testdoor/*.go door/*.go
 	cd testdoor; ${BUILD}
 
 # This builds just for amd64.  (I might not be amd64...)

+ 4 - 0
door/color.go

@@ -6,6 +6,10 @@ import (
 	"strings"
 )
 
+// Profiling:
+// go build -gcflags=-m=2 . 2>&1 | less
+// What leaks to heap?
+
 // Possibly make this so it is part of an ANSI package.
 // DRY - Reuse.
 

+ 43 - 1
door/line.go

@@ -197,7 +197,7 @@ func (r *Render) Append(color string, len int) {
 
 // RenderBlueYellow - Uppercase is Bold Blue, everything else is Yellow.
 // This is an example of using the door.Render routines.
-func RenderBlueYellow(text string) string {
+func RenderBlueYellowOld(text string) string {
 	var r Render = Render{Line: text}
 
 	var blue string = ColorText("BOLD BLUE")
@@ -212,3 +212,45 @@ func RenderBlueYellow(text string) string {
 	}
 	return r.Result
 }
+
+/*
+var RenderPool = sync.Pool{
+	New: func() any {
+		return new(strings.Builder)
+	},
+}
+*/
+
+// Render is the old way of doing things...
+// Using strings.Builder ...
+
+func RenderBlueYellow(text string) string {
+	var output strings.Builder
+
+	// var output = RenderPool.Get().(*strings.Builder)
+	// output.Reset()
+
+	var blue string = ColorText("BOLD BLUE")
+	var yellow string = ColorText("BOLD YELLOW")
+	var last *string
+
+	for _, letter := range text {
+		if unicode.IsUpper(letter) {
+			if last != &blue {
+				output.WriteString(blue)
+				last = &blue
+			}
+		} else {
+			if last != &yellow {
+				output.WriteString(yellow)
+				last = &yellow
+			}
+		}
+		output.WriteRune(letter)
+	}
+
+	// var result = output.String()
+	// RenderPool.Put(output)
+	// return result
+	return output.String()
+}

+ 26 - 1
door/line_test.go

@@ -115,9 +115,34 @@ func TestLineCP437(t *testing.T) {
 	}
 }
 
-// Benchmarks / profiling
+// Benchmarks for profiling
+// go test -bench=BenchmarkLine -benchmem -memprofile memory.out -cpuprofile cpu.out
+// go tool pprof memory.out
+// go tool pprof cpu.out
 
 func BenchmarkLine(b *testing.B) {
+	Unicode = false
+
+	for n := 0; n < b.N; n++ {
+		var line Line = Line{Text: fmt.Sprintf("Line %d of %d", n, b.N)}
+		var output string = line.Output()
+		_ = output
+	}
+}
+
+func BenchmarkLineRender(b *testing.B) {
+	Unicode = false
+
+	for n := 0; n < b.N; n++ {
+		var line Line = Line{Text: fmt.Sprintf("Line %d of %d", n, b.N), RenderF: RenderBlueYellow}
+		var output string = line.Output()
+		_ = output
+	}
+}
+
+// Benchmarks / profiling
+
+func BenchmarkLineColor(b *testing.B) {
 
 	var render = func(text string) string {
 		var output strings.Builder