Skip to content

Commit

Permalink
perf(encode): reduce alloc mem
Browse files Browse the repository at this point in the history
  • Loading branch information
nonzzz committed Aug 28, 2023
1 parent 2dc5868 commit ffdac20
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 29 deletions.
17 changes: 6 additions & 11 deletions benchmark.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ goos: windows
goarch: amd64
pkg: github.com/nonzzz/bencode
cpu: Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
BenchmarkEncode-12 10000 34307 ns/op 145993 B/op 80 allocs/op
BenchmarkEncode-12 10000 26206 ns/op 145992 B/op 80 allocs/op
BenchmarkEncode-12 10000 28097 ns/op 145992 B/op 80 allocs/op
BenchmarkEncode-12 10000 26583 ns/op 145992 B/op 80 allocs/op
BenchmarkEncode-12 10000 27728 ns/op 145992 B/op 80 allocs/op
BenchmarkDecode-12 10000 43816 ns/op 1088 B/op 29 allocs/op
BenchmarkDecode-12 10000 43490 ns/op 1088 B/op 29 allocs/op
BenchmarkDecode-12 10000 43359 ns/op 1088 B/op 29 allocs/op
BenchmarkDecode-12 10000 43092 ns/op 1088 B/op 29 allocs/op
BenchmarkDecode-12 10000 43342 ns/op 1088 B/op 29 allocs/op
BenchmarkEncode-12 10000 17554 ns/op 88344 B/op 70 allocs/op
BenchmarkEncode-12 10000 19004 ns/op 88344 B/op 70 allocs/op
BenchmarkEncode-12 10000 18961 ns/op 88344 B/op 70 allocs/op
BenchmarkEncode-12 10000 18592 ns/op 88344 B/op 70 allocs/op
BenchmarkEncode-12 10000 18454 ns/op 88344 B/op 70 allocs/op
PASS
ok github.com/nonzzz/bencode 3.863s
ok github.com/nonzzz/bencode 1.199s
12 changes: 3 additions & 9 deletions benchmark_result.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,15 @@ pkg: github.com/nonzzz/bencode
cpu: Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
│ benchmark.txt │
│ sec/op │
Encode-12 27.73µ ± ∞ ¹
Decode-12 43.36µ ± ∞ ¹
geomean 34.67µ
Encode-12 18.59µ ± ∞ ¹
¹ need >= 6 samples for confidence interval at level 0.95

│ benchmark.txt │
│ B/op │
Encode-12 142.6Ki ± ∞ ¹
Decode-12 1.062Ki ± ∞ ¹
geomean 12.31Ki
Encode-12 86.27Ki ± ∞ ¹
¹ need >= 6 samples for confidence interval at level 0.95

│ benchmark.txt │
│ allocs/op │
Encode-12 80.00 ± ∞ ¹
Decode-12 29.00 ± ∞ ¹
geomean 48.17
Encode-12 70.00 ± ∞ ¹
¹ need >= 6 samples for confidence interval at level 0.95
15 changes: 15 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package main

import (
"fmt"
"os"

"github.com/nonzzz/bencode"
)

var buf, _ = os.ReadFile("./test.torrent")

func main() {
s, _ := bencode.Decode(buf)
fmt.Println(s)
}
17 changes: 8 additions & 9 deletions encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,15 @@ func (encode *encode) next(input interface{}) {
}
}

// Someone need convert WTF string(non-standard ASCLL) if necessary.
// Someone need convert WTF string(non-standard ASCII) if necessary.

func (encode *encode) encodeString(s string) {
var sb strings.Builder
menu := []byte(s)
l := strconv.Itoa(len(menu))
l := strconv.Itoa(len(s))
sb.WriteString(l)
sb.WriteByte(StringDelim)
sb.WriteString(s)
encode.output = append(encode.output, []byte(sb.String())...)
encode.output = append(encode.output, sb.String()...)
}

// Should sort directory
Expand All @@ -95,9 +94,9 @@ func (encode *encode) encodeSlice(value reflect.Value) {
}

func (encode *encode) encodeNumeric(s string) {
var sb strings.Builder
sb.WriteByte(NumericStart)
sb.WriteString(s)
sb.WriteByte(EndOfType)
encode.output = append(encode.output, []byte(sb.String())...)
content := make([]byte, 0, 2+len(s))
content = append(content, NumericStart)
content = append(content, s...)
content = append(content, EndOfType)
encode.output = append(encode.output, content...)
}

0 comments on commit ffdac20

Please sign in to comment.