Generic Free List implementation to reuse memory and avoid allocations

gofl

GOFL provides a Generic Free List implementation for Go.

Installation

This package requires go1.18 or later.

go get github.com/akshaybharambe14/gofl

Why?

Free list pattern provides a way to reuse already initialized memory. GOFL helps you to grab an item from the free list and put it back to the free list once it is no longer needed. This helps in allocating limited amount of memory, resulting in better performance.

You can have a look at leaky buffer example from effective go documentation. This package is inspired by the same implementation.

A general usage would be:

newFn := func() T {...}
max := uint(10)
fl := gofl.NewFreeList(max, newFn)
...
t := fl.Get() // get t from free list
process(..., t) // reuse memory
fl.Put(t) // put t back to free list; will be reused eventually

Following benchmarks depict the use of free list in finding duplicates in a slice.

$ github.com/akshaybharambe14/gofl/example> go1.18beta1 test -bench . -benchmem
goos: windows
goarch: amd64
pkg: github.com/akshaybharambe14/gofl/example
cpu: Intel(R) Core(TM) i5-4200M CPU @ 2.50GHz
BenchmarkFindDuplicate-4                 1693132               681.7 ns/op           162 B/op          1 allocs/op
BenchmarkFindDuplicateWithFreeList-4     2151346               554.3 ns/op             2 B/op          0 allocs/op
PASS
ok      github.com/akshaybharambe14/gofl/example        3.855s

As you can see, it avoids creation and allocations of temporary map, resulting in better performance.

For more details see example.

Contact

Akshay Bharambe


If this is not something you are looking for, you can check other similar packages on pkg.go.dev, go.libhunt.com.

Do let me know if you have any feedback. Leave a this helps you!

Owner
Akshay Bharambe
Software Developer 👨‍💻️ by passion | Tech Geek | Open for contributions | ❤️ Go | On a mission to spread the knowledge ☑️ | Feel free to get in touch.
Akshay Bharambe
Similar Resources

Some utility functions for generic types in Go.

GOUF - Utility Functions for generic types Go team released Go 1.18 beta recently with support for Generics(a.k.a type parameters). This package provi

Apr 13, 2022

Di - A (very) WIP Go 1.18+ generic dependency injection package based on type reflection

di A (very) WIP Go 1.18+ generic dependency injection package based on type refl

Apr 26, 2022

🚀 GoDI: Generic based DI in Go

🚀 GoDI: Generic based DI in Go Installation DI: go get -u github.com/MaximZayats/godi/ import "github.com/MaximZayats/godi/di" CMD: go get -u github.

Aug 14, 2022

Generic-based collection tools

go-collection go collection is a tool implemented using generic, it can help you process slice/map data quickly and easily convert between them. Note:

Dec 29, 2022

Continuous profiling for analysis of CPU, memory usage over time, and down to the line number. Saving infrastructure cost, improving performance, and increasing reliability.

Continuous profiling for analysis of CPU, memory usage over time, and down to the line number. Saving infrastructure cost, improving performance, and increasing reliability.

Continuous profiling for analysis of CPU, memory usage over time, and down to the line number. Saving infrastructure cost, improving performance, and increasing reliability.

Jan 2, 2023

CDN-like in-memory cache with shielding, and Go 1.18 Generics

cache CDN-like, middleware memory cache for Go applications with integrated shielding and Go 1.18 Generics. Usage package main import ( "context" "

Apr 26, 2022

memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals

mem-resolver memresolver is an in-memory golang resolver that allows to override current golang Lookup func literals How to use it Create your custom

Jun 23, 2022

Execute a binary from memory, without touching the disk. Linux only.

Execute a binary from memory, without touching the disk. Linux only.

Memit Execute a binary from memory, without touching the disk. Linux only. Available as both a Go module and a binary. Using the Go module The Command

Jan 5, 2023

A comprehensive list of alternatives to your favorite software

alternativeto A comprehensive list of alternatives to your favorite software. Please do not edit this file directly. Instead, follow the steps outline

Jun 16, 2022
A Geiger counter for allocations

A Geiger counter for allocations

Dec 12, 2022
Go-generics-simple-doubly-linked-list - A simple doubly linked list implemented using generics (Golang)

Welcome to Go-Generics-Simple-Doubly-Linked-List! Hi, This repository contains a

Jun 30, 2022
List-Utils - 🔧 Utilities for maintaining the list of repost sites

SMR List Utils This is a Go CLI tool that helps with managing the StopModReposts blacklist. Install Use GitHub Releases and download binary. Linux Qui

Jan 3, 2022
A faster RWLock primitive in Go, 2-3 times faster than RWMutex. A Go implementation of concurrency control algorithm in paper

Go Left Right Concurrency A Go implementation of the left-right concurrency control algorithm in paper <Left-Right - A Concurrency Control Technique w

Jan 6, 2023
Go library for decoding generic map values into native Go structures and vice versa.

mapstructure mapstructure is a Go library for decoding generic map values to structures and vice versa, while providing helpful error handling. This l

Dec 28, 2022
conditiond is a generic constraint and policy evaluator.

conditiond conditiond is a generic constraint and policy evaluator. This tool lets you define constraints in data and evaluate them at run time. It's

Dec 5, 2022
Slice - provides generic Map, Reduce and Filter functions for Go.

slice slice is a simple Go package to provide generic versions of Map, Reduce and Filter on slices. I mainly wrote it as an exercise to get more famil

Jan 1, 2023
A small & fast dependency-free library for parsing micro expressions.

MicroExpr A small & fast dependency-free library for parsing micro expressions. This library was originally built for use in templating languages (e.g

Nov 25, 2022
A better Generic Pool (sync.Pool)
A better Generic Pool (sync.Pool)

This package is a thin wrapper over the Pool provided by the sync package. The Pool is an essential package to obtain maximum performance by reducing the number of memory allocations.

Dec 1, 2022
Package ethtool allows control of the Linux ethtool generic netlink interface.

ethtool Package ethtool allows control of the Linux ethtool generic netlink interface.

Dec 14, 2022