Giter VIP home page Giter VIP logo

multilock's Introduction

teivah/multilock

Go Report Card

multilock is a Go library allowing to store multiple sync.Mutex or sync.RWMutex.

The main benefit is to reduce the memory footprint if we need to have a mutex for every single structure of a set (e.g. thousand of maps or slices).

The internal data structure managed by multilock, depending on the use case, has either a fixed or a variable length.

Structures

  • Fixed length structure of Mutex: multilock.Fixed
  • Fixed length structure of RWMutex: multilock.RWFixed
  • Variable length structure of Mutex: multilock.Var
  • Variable length structure of RWMutex: multilock.RWVar

Examples

In the following example, we will create a 1/10 ratio multilock (10 mutexes to handle 100 maps):

const multilockLength = 10
const customStructLength = 100

// Initialize a fixed length multilock structure
mlock := multilock.NewFixed(multilockLength)

// Create custom structures
maps := make([]map[string]string, customStructLength)
for i := 0; i < customStructLength; i++ {
	maps[i] = make(map[string]string)
}

// Retrieve a lock for a given maps index
mutex := mlock.Get(maps[42])
mutex.Lock()
defer mutex.Unlock()

Internally, multilock has a distribution strategy which is basically hashing the key and returning a modulo based on the length provided.

It is also possible to override this distribution strategy this way:

mlock := multilock.NewFixed(10, multilock.WithCustomDistribution(func(i interface{}, length int) int {
    // Return an int between 0 and 10 depending on our distribution strategy
}))

The last point is related to variable length structures. In this example, we will create one and resize it:

mlock := multilock.NewVar(10)
mlock.Resize(15)

The resize operation is safe. To access/resize a variable length structure, it requires to acquire a shared lock first (which does not exist for a fixed structure hence making it faster than a variable one).

multilock's People

Contributors

teivah avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

isgasho vforks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.