-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use maphash instead of hashstructure
- Loading branch information
1 parent
3c45a26
commit e21dc7f
Showing
5 changed files
with
54 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,35 @@ | ||
package hashmap_test | ||
|
||
import ( | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/joetifa2003/mm-go/hashmap" | ||
"github.com/joetifa2003/mm-go/mmstring" | ||
) | ||
|
||
const TIMES = 5000 | ||
|
||
func BenchmarkHashmap(b *testing.B) { | ||
func BenchmarkHashmapGo(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
h := hashmap.New[int, *mmstring.MMString]() | ||
h := newMap() | ||
|
||
for i := 0; i < TIMES; i++ { | ||
h.Insert(i, mmstring.From("foo bar")) | ||
h[i] = i | ||
} | ||
|
||
h.Free() | ||
runtime.GC() | ||
} | ||
} | ||
|
||
func BenchmarkHashmapGo(b *testing.B) { | ||
func BenchmarkHashmap(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
h := map[string]string{} | ||
h := hashmap.New[int, int]() | ||
|
||
for i := 0; i < TIMES; i++ { | ||
h["foo"] = "foo bar" | ||
h.Insert(i, i) | ||
} | ||
|
||
_ = h | ||
runtime.GC() | ||
h.Free() | ||
} | ||
} | ||
|
||
func newMap() map[int]int { | ||
return make(map[int]int) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,50 @@ | ||
package malloc | ||
|
||
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 := openLibrary(getSystemLibrary()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
purego.RegisterLibFunc(&calloc, libc, "calloc") | ||
purego.RegisterLibFunc(&realloc, libc, "realloc") | ||
purego.RegisterLibFunc(&free, libc, "free") | ||
} | ||
// #include <stdlib.h> | ||
import "C" | ||
|
||
// 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 := openLibrary(getSystemLibrary()) | ||
// 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 calloc(1, size) | ||
return C.calloc(1, C.size_t(size)) | ||
} | ||
|
||
// CMalloc raw binding to c free | ||
func Free(ptr unsafe.Pointer) { | ||
free(ptr) | ||
C.free(ptr) | ||
} | ||
|
||
// CMalloc raw binding to c realloc | ||
func Realloc(ptr unsafe.Pointer, size int) unsafe.Pointer { | ||
return realloc(ptr, size) | ||
return C.realloc(ptr, C.size_t(size)) | ||
} |