Skip to content

Commit

Permalink
refactor go
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed Nov 9, 2023
1 parent 25df1fc commit 023d47d
Show file tree
Hide file tree
Showing 37 changed files with 648 additions and 728 deletions.
14 changes: 3 additions & 11 deletions base64/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ include ../common/commands.mk

executables := \
target/base64_cr \
target/base64_go \
target/base64_go_base64x \
$(GO_TARGETS) \
target/base64_go_gccgo \
target/base64_cpp_gcc_crypto \
target/base64_cpp_clang_crypto \
Expand Down Expand Up @@ -56,14 +55,7 @@ build: $(artifacts)
target/base64_cr: test.cr | target
$(CRYSTAL_BUILD)

target/base64_go: test.go | $(gofmt)
$(GO_BUILD)

target/base64_go_base64x: test-base64x.go | $(gofmt)
go mod tidy
$(GO_BUILD)

target/base64_go_gccgo: test.go | $(gofmt)
target/base64_go_gccgo: go/base64 $(GO_SOURCE) | gofmt
$(GCC_GO_BUILD)

target/base64_cpp_gcc_crypto: test-crypto.cpp | target libnotify
Expand Down Expand Up @@ -225,7 +217,7 @@ run[test.rkt]:: run[%]: %
# Utilities

.PHONY: clean
clean:
clean: clean-go
if [ -d $(aklomp-base64-dir) ]; then \
$(MAKE) -C $(aklomp-base64-dir) clean; \
fi
Expand Down
12 changes: 0 additions & 12 deletions base64/go.mod

This file was deleted.

13 changes: 13 additions & 0 deletions base64/go/base64/test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"benchmark"
"encoding/base64"
)

func main() {
err := benchmark.Run("", base64.StdEncoding)
if err != nil {
panic(err)
}
}
13 changes: 13 additions & 0 deletions base64/go/base64x/test-base64x.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package main

import (
"benchmark"
"github.com/chenzhuoyu/base64x"
)

func main() {
err := benchmark.Run("base64x", base64x.StdEncoding)
if err != nil {
panic(err)
}
}
85 changes: 85 additions & 0 deletions base64/go/benchmark.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package benchmark

import (
"fmt"
"strings"
"time"

"benchmarks/common"
)

const (
strSize = 131_072
tries = 8_192
)

type Encoding interface {
EncodeToString([]byte) string
DecodeString(string) ([]byte, error)
}

func verify(encoding Encoding) error {
for _, fixture := range [][]string{
{"hello", "aGVsbG8="},
{"world", "d29ybGQ="},
} {
src := fixture[0]
dst := fixture[1]
encoded := encoding.EncodeToString([]byte(src))
if encoded != dst {
return fmt.Errorf("%+v != %+v\n", encoded, dst)
}
decoded, err := encoding.DecodeString(dst)
if err != nil {
return err
}
if string(decoded) != src {
return fmt.Errorf("%+v != %+v\n", decoded, src)
}
}
return nil
}

func Run(name string, encoding Encoding) error {
err := verify(encoding)
if err != nil {
return err
}

bytes := []byte(strings.Repeat("a", strSize))
str2 := encoding.EncodeToString(bytes)
str3, err := encoding.DecodeString(str2)
if err != nil {
return err
}

var encStart, encEnd, decStart, decEnd time.Time
sizeEncoded, sizeDecoded := 0, 0

err = common.RunBenchmark(name, func() {
encStart = time.Now()
for i := 0; i < tries; i += 1 {
sizeEncoded += len(encoding.EncodeToString(bytes))
}
encEnd = time.Now()

decStart = time.Now()
for i := 0; i < tries; i += 1 {
decoded, err := encoding.DecodeString(str2)
if err != nil {
return
}
sizeDecoded += len(decoded)
}
decEnd = time.Now()
})
if err != nil {
return err
}
fmt.Printf("encode %s... to %s...: %d, %.4f\n",
string(bytes[:4]), str2[:4], sizeEncoded, encEnd.Sub(encStart).Seconds())
fmt.Printf("decode %s... to %s...: %d, %.4f\n",
str2[:4], string(str3[:4]), sizeDecoded, decEnd.Sub(decStart).Seconds())

return nil
}
18 changes: 18 additions & 0 deletions base64/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module benchmark

go 1.21

require (
benchmarks/common v0.0.0-00010101000000-000000000000
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d
)

require (
github.com/bytedance/sonic v1.10.2 // indirect
github.com/chenzhuoyu/iasm v0.9.1 // indirect
github.com/klauspost/cpuid/v2 v2.2.6 // indirect
golang.org/x/arch v0.6.0 // indirect
golang.org/x/sys v0.14.0 // indirect
)

replace benchmarks/common => ../../common/go
16 changes: 11 additions & 5 deletions base64/go.sum → base64/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.1 h1:7a1wuFXL1cMy7a3f7/VFcEtriuXQnUBhtoVfOZiaysc=
github.com/bytedance/sonic v1.10.1/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0=
github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA=
github.com/chenzhuoyu/iasm v0.9.0 h1:9fhXjVzq5hUy2gkhhgHl95zG2cEAhw9OSGs8toWWAwo=
github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0=
github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc=
github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -25,8 +27,12 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc=
golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
72 changes: 0 additions & 72 deletions base64/test-base64x.go

This file was deleted.

71 changes: 0 additions & 71 deletions base64/test.go

This file was deleted.

8 changes: 3 additions & 5 deletions brainfuck/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ executables := \
target/bin_hs_marray \
target/bin_ocaml \
target/bin_go \
$(GO_TARGETS) \
target/bin_go_gccgo \
target/bin_vala_gcc \
target/bin_vala_clang \
Expand Down Expand Up @@ -103,10 +104,7 @@ fsharp/target/Release/net7.0/brainfuck: fsharp/brainfuck.fsproj | target
target/bf2-kt.jar: bf2.kt | target
$(KOTLINC_BUILD)

target/bin_go: bf.go | target
$(GO_BUILD)

target/bin_go_gccgo: bf.go | target
target/bin_go_gccgo: go/bf $(GO_SOURCE) | gofmt
$(GCC_GO_BUILD)

target/bin_d: bf.d | $(dfmt)
Expand Down Expand Up @@ -257,7 +255,7 @@ run[bf.php]:: run[%]: %
# Utilities

.PHONY: clean
clean:
clean: clean-go
$(DOTNET_CLEAN) fsharp/brainfuck.fsproj
-rm -rf target
-rm -rf zig-cache
Loading

0 comments on commit 023d47d

Please sign in to comment.