{"id":26290016,"url":"https://github.com/unsafe-risk/mi","last_synced_at":"2025-05-07T22:44:32.667Z","repository":{"id":49319296,"uuid":"496631548","full_name":"unsafe-risk/mi","owner":"unsafe-risk","description":"attach mimalloc to go with cgo","archived":false,"fork":false,"pushed_at":"2024-12-20T02:19:06.000Z","size":221,"stargazers_count":7,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-05-07T22:44:25.220Z","etag":null,"topics":["cgo","go","mimalloc"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/unsafe-risk.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-05-26T13:29:12.000Z","updated_at":"2025-01-06T11:28:48.000Z","dependencies_parsed_at":"2024-06-19T20:04:31.945Z","dependency_job_id":"d88cfd1c-5ac9-41b2-bf8e-5c9ff38bbc64","html_url":"https://github.com/unsafe-risk/mi","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unsafe-risk%2Fmi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unsafe-risk%2Fmi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unsafe-risk%2Fmi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/unsafe-risk%2Fmi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/unsafe-risk","download_url":"https://codeload.github.com/unsafe-risk/mi/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252967974,"owners_count":21833245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cgo","go","mimalloc"],"created_at":"2025-03-14T23:17:43.321Z","updated_at":"2025-05-07T22:44:32.639Z","avatar_url":"https://github.com/unsafe-risk.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mimalloc-go\n\nthis is a go wrapper of `mimalloc` with cgo.\n\n# installing\n\n`go get -u github.com/unsafe-risk/mi`\n\n# how to use\n\n## MAlloc and pointer\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/unsafe-risk/mi\"\n)\n\nfunc main() {\n\ta := mi.MAllocOf[int64]()\n\t*a = 99\n\tfmt.Println(*a)\n\tmi.FreeOf(a)\n}\n```\n\n`MAllocOf` is make a dynamic pointer of type `T`.\n\n`FreeOf` is free the memory of the pointer.\n\n## CAlloc and CArray\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/unsafe-risk/mi\"\n)\n\nfunc main() {\n\ta := mi.CAllocOf[int64](6)\n\ta.Set(0, 1)\n\ta.Set(1, 4)\n\ta.Set(2, 9)\n\ta.Set(3, 16)\n\ta.Set(4, 25)\n\ta.Set(5, 36)\n\tfor i := 0; i \u003c a.Length(); i++ {\n\t\tfmt.Println(a.At(i))\n\t}\n\tmi.FreeCArray(a)\n}\n```\n\n`CAllocOf` is make a `CArray` of type `T` with length.\n\n`FreeCArray` is free the memory of the pointer.\n\n## CArray and Slice\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"sort\"\n\n\t\"github.com/unsafe-risk/mi\"\n)\n\ntype Person struct {\n\tName string\n\tAge  int\n}\n\nfunc main() {\n\tc := mi.CAllocOf[int](10)\n\tfor i := 0; i \u003c 10; i++ {\n\t\tc.Set(i, -i*i)\n\t}\n\tsort.Ints(c.ToSlice())\n\tfmt.Println(c.ToSlice())\n\tmi.FreeCArray(c)\n}\n```\n\n`ToSlice()` method is return a fixed length slice of `CArray`.\n\nIt equals a slice of golang, but length is immutable.\n\n## ARENA\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n\t\"github.com/unsafe-risk/mi/arena\"\n)\n\ntype Person struct {\n\tName string\n\tAge  int\n}\n\nfunc main() {\n\ta := arena.New()\n\tdefer a.Free()\n\tp := arena.NewOf[Person](a)\n\tp.Name = \"John\"\n\tp.Age = 30\n\tfmt.Println(p)\n}\n```\n\n`arena` is a memory pool implementation from unsafe-risk/umem.\n\nThe `arena` of this project is based on `mimalloc`.\n\nAnd, this is test code. I cannot recommend to use `arena` for your project.\n\n### arena bench\n\n```bash\ngoos: darwin\ngoarch: amd64\npkg: github.com/unsafe-risk/mi/arena\ncpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz\nBenchmarkMiArenaPerson-16    \t      33\t  33580864 ns/op\t      42 B/op\t       1 allocs/op\nBenchmarkStdNew-16           \t      39\t  30365484 ns/op\t96002062 B/op\t 2000022 allocs/op\n```\n\n```bash\ngoos: linux\ngoarch: amd64\npkg: github.com/unsafe-risk/mi/arena\ncpu: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz\nBenchmarkMiArenaPerson-4   \t      19\t  59619689 ns/op\t     195 B/op\t       0 allocs/op\nBenchmarkStdNew-4          \t      20\t  54869378 ns/op\t96000574 B/op\t 2000006 allocs/op\n```\n\n```bash\ngoos: linux\ngoarch: amd64\npkg: github.com/unsafe-risk/mi/arena\ncpu: AMD Ryzen 7 4800H with Radeon Graphics         \nBenchmarkMiArenaPerson-16    \t      32\t  49734124 ns/op\t     861 B/op\t       2 allocs/op\nBenchmarkStdNew-16           \t      52\t  20565911 ns/op\t96001187 B/op\t 2000011 allocs/op\n```\n\n```bash\ngoos: linux\ngoarch: arm64\npkg: github.com/unsafe-risk/mi/arena\nBenchmarkMiArenaPerson-4   \t      85\t  11788425 ns/op\t      10 B/op\t       0 allocs/op\nBenchmarkStdNew-4          \t      26\t  44319635 ns/op\t96001133 B/op\t 2000011 allocs/op\n```\n\n```bash\ngoos: darwin\ngoarch: arm64\npkg: github.com/unsafe-risk/mi/arena\ncpu: Apple M3 Pro\nBenchmarkMiArenaPerson-12    \t     270\t   4534302 ns/op\t      21 B/op\t       0 allocs/op\nBenchmarkStdNew-12           \t      51\t  22088899 ns/op\t96001990 B/op\t 2000020 allocs/op\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funsafe-risk%2Fmi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Funsafe-risk%2Fmi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Funsafe-risk%2Fmi/lists"}