Type-safe atomic values for Go

Type-safe atomic values for Go

One issue with Go's sync/atomic package is that there is no guarantee from the type system that operations on an integer value will be applied through the sync/atomic functions. This package solves that and introduces two type-safe interfaces for use with integer and non-integer atomic values.

The first interface is for any value:

// Value represents a value that can be atomically loaded or replaced.
type Value[T any] interface {
	// Load value atomically.
	Load() T
	// Store value atomically.
	Store(value T)
	// Swap the previous value with the new value atomically.
	Swap(new T) (old T)
	// CompareAndSwap the previous value with new if its value is "old".
	CompareAndSwap(old, new T) (swapped bool)
}

The second interface is a Value[T] constrained to the 32 and 64 bit integer types and adds a single Add() method:

// Int expresses atomic operations on signed or unsigned integer values.
type Int[T atomicint] interface {
	Value[T]
	// Add a value and return the new result.
	Add(delta T) (new T)
}

Performance

BenchmarkInt64Add
BenchmarkInt64Add-8           	174217112	        6.887 ns/op
BenchmarkIntInterfaceAdd
BenchmarkIntInterfaceAdd-8    	174129980	        6.889 ns/op
BenchmarkStdlibInt64Add
BenchmarkStdlibInt64Add-8     	174152660	        6.887 ns/op
BenchmarkInterfaceStore
BenchmarkInterfaceStore-8     	16015668	       76.17 ns/op
BenchmarkValueStore
BenchmarkValueStore-8         	16155405	       75.03 ns/op
BenchmarkStdlibValueStore
BenchmarkStdlibValueStore-8   	16391035	       74.85 ns/op
Similar Resources

Start of a project that would let people stay informed about safe running spaces in their area.

SafeRun Start of a project that would let people stay informed about safe running spaces in their area. Too many people I'm friends with feel unsafe w

Feb 11, 2022

Analyze the binary outputted by `go build` to get type information etc.

Analyze the binary outputted by go build to get type information etc.

Oct 5, 2022

IBus Engine for GoVarnam. An easy way to type Indian languages on GNU/Linux systems.

IBus Engine For GoVarnam An easy way to type Indian languages on GNU/Linux systems. goibus - golang implementation of libibus Thanks to sarim and haun

Feb 10, 2022

Lithia is an experimental functional programming language with an implicit but strong and dynamic type system.

Lithia is an experimental functional programming language with an implicit but strong and dynamic type system. Lithia is designed around a few core concepts in mind all language features contribute to.

Dec 24, 2022

A tool to generate Pulumi Package schemas from Go type definitions

MkSchema A tool to generate Pulumi Package schemas from Go type definitions. This tool translates annotated Go files into Pulumi component schema meta

Sep 1, 2022

Atomic: a go package for atomic file writing

atomic import "github.com/natefinch/atomic" atomic is a go package for atomic file writing By default, writing to a file in go (and generally any lan

Nov 10, 2021

Go generator to copy values from type to type and fields from struct to struct. Copier without reflection.

Copygen is a command-line code generator that generates type-to-type and field-to-field struct code without adding any reflection or dependenc

Dec 29, 2022

Go concurrent-safe, goroutine-safe, thread-safe queue

Go concurrent-safe, goroutine-safe, thread-safe queue

goconcurrentqueue - Concurrent safe queues The package goconcurrentqueue offers a public interface Queue with methods for a queue. It comes with multi

Dec 31, 2022

Nat-type-identifier-go - A Go based implementation of Network Address Transalation (NAT) type identifier based on nat-type-identifier

nat-type-identifier-go A Go based implementation of Network Address Transalation

May 8, 2022

:steam_locomotive: Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. Dual Array and Full map support.

Package form Package form Decodes url.Values into Go value(s) and Encodes Go value(s) into url.Values. It has the following features: Supports map of

Dec 26, 2022

Package create provides a generic option pattern for creating new values of any type

create Package create provides a generic option pattern for creating new values

Dec 30, 2021

A Go package providing a generic data type to track maximum and minimum peak values.

go-peak Overview go-peak is a Go package providing a generic data type that tracks the maximum and minimum peak values within a specific period of tim

Mar 26, 2022

Supports the safe and convenient execution of asynchronous computations with goroutines and provides facilities for the safe retrieval of the computation results.

Rendezvous The Rendezvous library supports the safe and convenient execution of asynchronous computations with goroutines and provides facilities for

Dec 29, 2021

Null Types, Safe primitive type conversion and fetching value from complex structures.

Typ Typ is a library providing a powerful interface to impressive user experience with conversion and fetching data from built-in types in Golang Feat

Sep 26, 2022

Type safe SQL builder with code generation and automatic query result data mapping

Type safe SQL builder with code generation and automatic query result data mapping

Jet Jet is a complete solution for efficient and high performance database access, consisting of type-safe SQL builder with code generation and automa

Jan 6, 2023

Type safe SQL query builder and struct mapper for Go

sq (Structured Query) 🎯 🏆 sq is a code-generated, type safe query builder and struct mapper for Go. 🏆 🎯 Documentation • Reference • Examples This

Dec 19, 2022

Type-safe Redis client for Golang

Redis client for Golang ❤️ Uptrace.dev - distributed traces, logs, and errors in one place Join Discord to ask questions. Documentation Reference Exam

Jan 1, 2023

100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood.

go-queryset 100% type-safe ORM for Go (Golang) with code generation and MySQL, PostgreSQL, Sqlite3, SQL Server support. GORM under the hood. Contents

Dec 30, 2022

Type-safe Redis client for Golang

Redis client for Golang ❤️ Uptrace.dev - distributed traces, logs, and errors in one place Join Discord to ask questions. Documentation Reference Exam

Jan 4, 2023
Related tags
safe and easy casting from one type to another in Go

cast Easy and safe casting from one type to another in Go Don’t Panic! ... Cast What is Cast? Cast is a library to convert between different go types

Jan 7, 2023
Generate type-safe Go converters by simply defining an interface

goverter a "type-safe Go converter" generator goverter is a tool for creating type-safe converters. All you have to do is create an interface and exec

Jan 4, 2023
atomic measures + Prometheus exposition library

About Atomic measures with Prometheus exposition for the Go programming language. This is free and unencumbered software released into the public doma

Sep 27, 2022
An atomic counter that also tracks and calculates a rate written in Go.

An atomic counter that also tracks and calculates a rate written in Go.

Oct 27, 2021
Atomic Arbitrage - A base example of a bare implementation of an arbitrage bot

Atomic Arbitrage Atomic Arbitrage is a base example of a bare implementation of

Nov 23, 2022
[TOOL, CLI] - Filter and examine Go type structures, interfaces and their transitive dependencies and relationships. Export structural types as TypeScript value object or bare type representations.

typex Examine Go types and their transitive dependencies. Export results as TypeScript value objects (or types) declaration. Installation go get -u gi

Dec 6, 2022
Initialize structs with default values

defaults Initialize structs with default values Supports almost all kind of types Scalar types int/8/16/32/64, uint/8/16/32/64, float32/64 uintptr, bo

Jan 7, 2023
Best-effort CPU-local sharded values for Go
Best-effort CPU-local sharded values for Go

percpu Percpu is a Go package to support best-effort CPU-local sharded values. This package is something of an experiment. See Go issue #18802 for dis

Nov 9, 2022
Package reservoir samples values uniformly at random from an unbounded sequence of inputs

Package reservoir samples values uniformly at random from an unbounded sequence of inputs

Oct 5, 2022
Toy program for benchmarking safe and unsafe ways of saving a file

save-a-file benchmarks the many strategies an editor could use to save a file. Example output on a SSD: ext4: $ ./save-a-file ~/tmp/foo 29.195µs per s

Jan 4, 2023