Skip to content

Commit

Permalink
feat: use purgo instead of cgo
Browse files Browse the repository at this point in the history
feat: pump purego version

feat: pump purego version
  • Loading branch information
Youssef ahmed committed May 10, 2023
1 parent ec615c0 commit 38cd911
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ module github.com/joetifa2003/mm-go

go 1.19

require github.com/stretchr/testify v1.8.1
require (
github.com/ebitengine/purego v0.4.0-alpha.4
github.com/stretchr/testify v1.8.1
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.7.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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/ebitengine/purego v0.4.0-alpha.4 h1:Y7yIV06Yo5M2BAdD7EVPhfp6LZ0tEcQo5770OhYUVes=
github.com/ebitengine/purego v0.4.0-alpha.4/go.mod h1:ah1In8AOtksoNK6yk5z1HTJeUkC1Ez4Wk2idgGslMwQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand All @@ -10,6 +12,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
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=
Expand Down
12 changes: 6 additions & 6 deletions hashmap/hashmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ const TIMES = 15000

func BenchmarkHashMap(b *testing.B) {
for n := 0; n < b.N; n++ {
hm := hashmap.New[hashmap.String, int]()
hm := hashmap.New[hashmap.Int, int]()
for i := 0; i < TIMES; i++ {
hm.Insert(hashmap.String(fmt.Sprint(i)), i)
hm.Insert(hashmap.Int(i), i)
}

sum := 0
for i := 0; i < TIMES; i++ {
v, _ := hm.Get(hashmap.String(fmt.Sprint(i)))
v, _ := hm.Get(hashmap.Int(i))
sum += v
}

Expand All @@ -126,14 +126,14 @@ func BenchmarkHashMap(b *testing.B) {

func BenchmarkGoMap(b *testing.B) {
for n := 0; n < b.N; n++ {
hm := map[string]int{}
hm := map[int]int{}
for i := 0; i < TIMES; i++ {
hm[fmt.Sprint(i)] = i
hm[i] = i
}

sum := 0
for i := 0; i < TIMES; i++ {
sum += hm[fmt.Sprint(i)]
sum += hm[i]
}

_ = sum
Expand Down
43 changes: 37 additions & 6 deletions malloc/malloc.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,51 @@
package malloc

// #include <stdlib.h>
import "C"
import "unsafe"
import (
"fmt"
"runtime"
"unsafe"

"github.com/ebitengine/purego"
)

var calloc func(n int, size int) unsafe.Pointer
var realloc func(ptr unsafe.Pointer, size int) unsafe.Pointer
var free func(ptr unsafe.Pointer)

func getSystemLibrary() string {
switch runtime.GOOS {
case "darwin":
return "/usr/lib/libSystem.B.dylib"
case "linux":
return "libc.so.6"
case "windows":
return "ucrtbase.dll"
default:
panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS))
}
}

func init() {
libc, err := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
panic(err)
}
purego.RegisterLibFunc(&calloc, libc, "calloc")
purego.RegisterLibFunc(&realloc, libc, "realloc")
purego.RegisterLibFunc(&free, libc, "free")
}

// CMalloc raw binding to c calloc(1, size)
func Malloc(size int) unsafe.Pointer {
return C.calloc(1, C.size_t(size))
return calloc(1, size)
}

// CMalloc raw binding to c free
func Free(ptr unsafe.Pointer) {
C.free(ptr)
free(ptr)
}

// CMalloc raw binding to c realloc
func Realloc(ptr unsafe.Pointer, size int) unsafe.Pointer {
return C.realloc(ptr, C.size_t(size))
return realloc(ptr, size)
}

0 comments on commit 38cd911

Please sign in to comment.