partmap

package
v0.0.0-...-f084d8c Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 3 Imported by: 0

README

PartMap

PartMap is a concurrent map implementation in Go, designed to reduce lock contention and improve performance in multithreaded environments. This package offers a thread-safe map by partitioning the map into several segments, each protected by its own lock.

Features

  • Concurrency Safe: Utilizes multiple locks to allow concurrent access with minimal contention.
  • Customizable Partitioning: Supports custom partition strategies through the partitioner interface.
  • Standard Map Interface: Familiar methods such as Get, Set, Delete, and Len.

Usage

package main

import (
  "fmt"
  "log"

  "github.com/shortlink-org/shortlink/pkg/types/partmap"
)

func main() {
  m, err := partmap.New(&hashSumPartitioner{1000}, 1000)
  if err != nil {
    log.Fatalf("Failed to create PartMap: %v", err)
  }

  // Set a value
  m.Set("key1", "value1")

  // Get a value
  if val, ok := m.Get("key1"); ok {
      fmt.Println("Value:", val)
  }

  // Delete a value
  m.Delete("key1")

  // Get the length
  fmt.Println("Length:", m.Len())
}

Benchmarks

[!NOTE]

goos: darwin goarch: amd64 cpu: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz

BenchmarkStd
BenchmarkStd/set_std_concurrently
BenchmarkStd/set_std_concurrently-16         	  220958	      6605 ns/op	     322 B/op	       4 allocs/op
BenchmarkSyncStd
BenchmarkSyncStd/set_sync_map_std_concurrently
BenchmarkSyncStd/set_sync_map_std_concurrently-16         	  132684	     26067 ns/op	     506 B/op	      10 allocs/op
BenchmarkPartitioned
BenchmarkPartitioned/set_partitioned_concurrently
BenchmarkPartitioned/set_partitioned_concurrently-16      	  251509	      6586 ns/op	     329 B/op	       6 allocs/op

References

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrPartitionsMustBeGreaterThanZero = errors.New("partitions must be greater than 0")

Functions

This section is empty.

Types

type HashSumPartitioner

type HashSumPartitioner struct {
	// contains filtered or unexported fields
}

HashSumPartitioner implements a Partitioner using a hash sum.

func (*HashSumPartitioner) Find

func (h *HashSumPartitioner) Find(key string) (uint, error)

type PartMap

type PartMap struct {
	// contains filtered or unexported fields
}

PartMap is a concurrent map with separate partitions to reduce lock contention.

func New

func New(partitioner Partitioner, partitions uint) (*PartMap, error)

New creates a new PartMap with the given number of partitions and Partitioner.

func (*PartMap) Delete

func (pm *PartMap) Delete(key string) error

Delete removes a key-value pair from the map.

func (*PartMap) Get

func (pm *PartMap) Get(key string) (any, bool)

Get retrieves a value from the map.

func (*PartMap) Len

func (pm *PartMap) Len() int

Len returns the total number of key-value pairs in the PartMap.

func (*PartMap) Set

func (pm *PartMap) Set(key string, val any) error

Set adds a key-value pair to the map.

type Partitioner

type Partitioner interface {
	Find(key string) (uint, error)
}

Partitioner defines the interface for partition finding strategies.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL