Skip to content

Commit

Permalink
Merge pull request #4 from joetifa2003/purego-ebiten
Browse files Browse the repository at this point in the history
feat: use purgo instead of cgo
  • Loading branch information
joetifa2003 authored May 10, 2023
2 parents ec615c0 + c334b3b commit 9ca3504
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 516 deletions.
204 changes: 39 additions & 165 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,60 +6,45 @@
Golang manages memory via GC and it's good for almost every use case but sometimes it can be a bottleneck.
and this is where mm-go comes in to play.

- [mm-go Generic manual memory management for golang](#mm-go-generic-manual-memory-management-for-golang)
- [Before using mm-go](#before-using-mm-go)
- [Installing](#installing)
- [Packages](#packages)
- [typedarena](#typedarena)
- [Alloc/Free](#allocfree)
- [AllocMany/FreeMany](#allocmanyfreemany)
- [ReAlloc](#realloc)
- [vector](#vector)
- [Methods](#methods)
- [New](#new)
- [Init](#init)
- [Push](#push)
- [Pop](#pop)
- [Len](#len)
- [Cap](#cap)
- [Slice](#slice)
- [Last](#last)
- [At](#at)
- [AtPtr](#atptr)
- [Free](#free)
- [linkedlist](#linkedlist)
- [Methods](#methods-1)
- [New](#new-1)
- [PushBack](#pushback)
- [PushFront](#pushfront)
- [PopBack](#popback)
- [PopFront](#popfront)
- [ForEach](#foreach)
- [At](#at-1)
- [AtPtr](#atptr-1)
- [RemoveAt](#removeat)
- [Remove](#remove)
- [RemoveAll](#removeall)
- [FindIndex](#findindex)
- [FindIndexes](#findindexes)
- [Len](#len-1)
- [Free](#free-1)
- [hashmap](#hashmap)
- [Methods](#methods-2)
- [New](#new-2)
- [Insert](#insert)
- [Delete](#delete)
- [Get](#get)
- [GetPtr](#getptr)
- [Free](#free-2)
- [mmstring](#mmstring)
- [Methods](#methods-3)
- [New](#new-3)
- [From](#from)
- [GetGoString](#getgostring)
- [AppendGoString](#appendgostring)
- [Free](#free-3)
- [Benchmarks](#benchmarks)
- [mm-go Generic manual memory management for golang](#mm-go-generic-manual-memory-management-for-golang)
- [Before using mm-go](#before-using-mm-go)
- [Installing](#installing)
- [Packages](#packages)
- [typedarena](#typedarena)
- [Alloc/Free](#allocfree)
- [AllocMany/FreeMany](#allocmanyfreemany)
- [ReAlloc](#realloc)
- [vector](#vector)
- [Methods](#methods)
- [New](#new)
- [Init](#init)
- [Push](#push)
- [Pop](#pop)
- [Len](#len)
- [Cap](#cap)
- [Slice](#slice)
- [Last](#last)
- [At](#at)
- [AtPtr](#atptr)
- [Free](#free)
- [linkedlist](#linkedlist)
- [Methods](#methods-1)
- [New](#new-1)
- [PushBack](#pushback)
- [PushFront](#pushfront)
- [PopBack](#popback)
- [PopFront](#popfront)
- [ForEach](#foreach)
- [At](#at-1)
- [AtPtr](#atptr-1)
- [RemoveAt](#removeat)
- [Remove](#remove)
- [RemoveAll](#removeall)
- [FindIndex](#findindex)
- [FindIndexes](#findindexes)
- [Len](#len-1)
- [Free](#free-1)
- [Benchmarks](#benchmarks)

## Before using mm-go

Expand Down Expand Up @@ -88,8 +73,6 @@ go get -u github.com/joetifa2003/mm-go

`linkedlist` - contains a manually managed Linkedlist implementation.

`hashmap` - contains a manually managed Hashmap implementation.

`mmstring` - contains a manually managed string implementation.

`malloc` - contains wrappers to raw C malloc and free.
Expand Down Expand Up @@ -422,112 +405,6 @@ func (ll *LinkedList[T]) Len() int
func (ll *LinkedList[T]) Free()
```

## hashmap

Manually managed hashmap, keys can be hashmap.String, hashmap.Int or any type that implements the hashmap.Hashable interface

```go
type Hashable interface {
comparable
Hash() uint32
}
```

### Methods

#### New

```go
// New creates a new Hashmap with key of type K and value of type V
func New[K Hashable, V any]() *Hashmap[K, V]
```

#### Insert

```go
// Insert inserts a new value V if key K doesn't exist,
// Otherwise update the key K with value V
func (hm *Hashmap[K, V]) Insert(key K, value V)
```

#### Delete

```go
// Delete delete value with key K
func (hm *Hashmap[K, V]) Delete(key K)
```

#### Get

```go
// Get takes key K and return value V
func (hm *Hashmap[K, V]) Get(key K) (value V, exists bool)
```

#### GetPtr

```go
// GetPtr takes key K and return a pointer to value V
func (hm *Hashmap[K, V]) GetPtr(key K) (value *V, exists bool)
```

#### Free

```go
// Free frees the Hashmap
func (hm *Hashmap[K, V]) Free()
```

## mmstring

MMString is a manually manged string that is basically a \*Vector[rune]
and contains all the methods of a vector plus additional helper functions

```go
type MMString struct {
*vector.Vector[rune]
}
```

### Methods

#### New

```go
// New create a new manually managed string
func New() *MMString
```

#### From

```go
// From creates a new manually managed string,
// And initialize it with a go string
func From(input string) *MMString
```

#### GetGoString

```go
// GetGoString returns go string from manually managed string.
// CAUTION: You also have to free the MMString
func (s *MMString) GetGoString() string
```

#### AppendGoString

```go
// AppendGoString appends go string to manually managed string
func (s *MMString) AppendGoString(input string)
```

#### Free

```go
// Free frees MMString
func (s *MMString) Free()
```

## Benchmarks

Check the test files and github actions for the benchmarks (linux, macos, windows).
Expand Down Expand Up @@ -556,7 +433,4 @@ BinaryTreeArena/chunk_size_100-2 1.47s ± 5%
BinaryTreeArena/chunk_size_150-2 1.42s ±36%
BinaryTreeArena/chunk_size_250-2 1.11s ± 0%
BinaryTreeArena/chunk_size_500-2 1.00s ± 0%
pkg:github.com/joetifa2003/mm-go/hashmap goos:linux goarch:amd64
HashMap-2 14.8ms ± 0%
GoMap-2 6.39ms ± 1%
```
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
module github.com/joetifa2003/mm-go

go 1.19
go 1.20

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
Loading

0 comments on commit 9ca3504

Please sign in to comment.