Skip to content

mempool is a simple memory pool implementation, which is used to improve memory utilization and performance. It provides a configurable-size cache pool for repeatedly allocating and recycling memory blocks of the same size.

Notifications You must be signed in to change notification settings

lambertxiao/go-mempool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mempool

Introduction

mempool is a simple memory pool implementation, which is used to improve memory utilization and performance. It provides a configurable-size cache pool for repeatedly allocating and recycling memory blocks of the same size.

The memory pool pre-allocates a certain number of memory blocks and stores them in a stack, avoiding frequent memory allocation and deallocation operations. When memory needs to be allocated, a usable memory block is directly taken from the pool; when the memory block is no longer needed, it is returned to the pool for future use.

mempool uses a lock-free structure to achieve concurrent safety of data access, avoiding the performance overhead of traditional lock mechanisms. It utilizes atomic operations and lock-free algorithms to ensure the correctness and efficiency of concurrent access.

The memory pool supports custom memory block generation functions fn, which can create memory blocks suitable for actual needs.

Usage

The following is a basic example of using mempool:

Import the required package:

import "github.com/lambertxiao/go-mempool"
  1. Create a new memory pool:
pool := mempool.NewGoMemPool(uint, func() interface{}) *GoMemPool // Pass the appropriate size and generation function according to actual needs
  1. Retrieve a memory block from the memory pool:
item := pool.Get() // If the method is called when the memory pool is empty, it will wait indefinitely until a memory block is returned to the pool.

or

item := pool.GetByTime(time.Second) // If an available memory block is not retrieved within the specified timeout time, the method returns a null value (zero value of the corresponding type).
  1. Return the memory block to the memory pool:
pool.Put(item) // Return item to the pool for future use
  1. Check the size or capacity of the memory pool:
capacity := pool.Cap() // Get the capacity of the memory pool
  1. Destroy the memory pool (empty all memory blocks):
pool.Destory() // Clear all memory blocks in the memory pool

About

mempool is a simple memory pool implementation, which is used to improve memory utilization and performance. It provides a configurable-size cache pool for repeatedly allocating and recycling memory blocks of the same size.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages