Skip to content

Commit 66aa088

Browse files
committed
performance improvement
1 parent a5ec652 commit 66aa088

File tree

4 files changed

+40
-24
lines changed

4 files changed

+40
-24
lines changed

cpu.prof

80.7 KB
Binary file not shown.

internal/layout/comnt.go

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package layout
22

33
import (
4-
"bufio"
54
"bytes"
6-
"strings"
5+
)
6+
7+
const (
8+
// cmtCapacity is the initial capacity for comment line slices.
9+
cmtCapacity = 16
710
)
811

912
// A SAUCE comment block is an optional, variable sized structure that holds up to 255 lines
@@ -54,32 +57,45 @@ func CommentByBreak(b []byte) []string {
5457
if len(b) == 0 {
5558
return []string{}
5659
}
57-
r := bytes.NewReader(b)
58-
scanner := bufio.NewScanner(r)
59-
s := []string{}
60-
for scanner.Scan() {
61-
s = append(s, scanner.Text())
60+
61+
lines := make([]string, 0, cmtCapacity)
62+
start := 0
63+
64+
for i := 0; i < len(b); i++ {
65+
if b[i] == '\n' || b[i] == '\r' {
66+
// Handle line ending
67+
if i > start {
68+
lines = append(lines, string(b[start:i]))
69+
}
70+
start = i + 1
71+
72+
// Handle \r\n sequences
73+
if i+1 < len(b) && b[i] == '\r' && b[i+1] == '\n' {
74+
i++
75+
start = i + 1
76+
}
77+
}
78+
}
79+
if start < len(b) {
80+
lines = append(lines, string(b[start:]))
6281
}
63-
return s
82+
83+
return lines
6484
}
6585

6686
// CommentByLine parses the SAUCE comment by lines of 64 characters.
6787
func CommentByLine(b []byte) []string {
6888
if len(b) == 0 {
6989
return []string{}
7090
}
71-
var sb strings.Builder
72-
sb.Grow(ComntLineSize)
73-
lines := []string{}
74-
for i, c := range b {
75-
sb.WriteByte(c)
76-
if (i+1)%ComntLineSize == 0 {
77-
lines = append(lines, sb.String())
78-
sb.Reset()
79-
}
91+
expectedLines := len(b) / ComntLineSize
92+
if len(b)%ComntLineSize != 0 {
93+
expectedLines++
8094
}
81-
if sb.Len() > 0 {
82-
lines = append(lines, sb.String())
95+
lines := make([]string, 0, expectedLines)
96+
for i := 0; i < len(b); i += ComntLineSize {
97+
end := min(i+ComntLineSize, len(b))
98+
lines = append(lines, string(b[i:end]))
8399
}
84100
return lines
85101
}

mem.prof

10.4 KB
Binary file not shown.

sauce_bench_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ func BenchmarkContains(b *testing.B) {
225225
}
226226
}
227227

228-
// BenchmarkFileSizePerformance measures parsing performance with different file sizes
228+
// BenchmarkFileSizePerformance measures parsing performance with different file sizes.
229229
func BenchmarkFileSizePerformance(b *testing.B) {
230230
baseFile, err := static.ReadFile("static/sauce.txt")
231231
if err != nil {
@@ -253,7 +253,7 @@ func BenchmarkFileSizePerformance(b *testing.B) {
253253
}
254254
}
255255

256-
// BenchmarkMemoryAllocation measures memory allocation patterns
256+
// BenchmarkMemoryAllocation measures memory allocation patterns.
257257
func BenchmarkMemoryAllocation(b *testing.B) {
258258
raw, err := static.ReadFile("static/sauce.txt")
259259
if err != nil {
@@ -267,7 +267,7 @@ func BenchmarkMemoryAllocation(b *testing.B) {
267267
}
268268
}
269269

270-
// BenchmarkJSONSerializationSize measures JSON serialization with different record sizes
270+
// BenchmarkJSONSerializationSize measures JSON serialization with different record sizes.
271271
func BenchmarkJSONSerializationSize(b *testing.B) {
272272
baseFile, err := static.ReadFile("static/sauce.txt")
273273
if err != nil {
@@ -296,7 +296,7 @@ func BenchmarkJSONSerializationSize(b *testing.B) {
296296
}
297297
}
298298

299-
// BenchmarkConcurrentParsing measures performance under concurrent access
299+
// BenchmarkConcurrentParsing measures performance under concurrent access.
300300
func BenchmarkConcurrentParsing(b *testing.B) {
301301
raw, err := static.ReadFile("static/sauce.txt")
302302
if err != nil {
@@ -310,7 +310,7 @@ func BenchmarkConcurrentParsing(b *testing.B) {
310310
})
311311
}
312312

313-
// BenchmarkInvalidRecords measures performance with invalid/malformed records
313+
// BenchmarkInvalidRecords measures performance with invalid/malformed records.
314314
func BenchmarkInvalidRecords(b *testing.B) {
315315
// Test with empty data
316316
b.Run("Empty", func(b *testing.B) {

0 commit comments

Comments
 (0)