Giter VIP home page Giter VIP logo

go-stm's Introduction

go-stm

import "github.com/decillion/go-stm"

Package stm is a software transactional memory implementation for Go, which is based on the Transactional Locking II (TL2) proposed by Dice et al. https://doi.org/10.1007/11864219_14

Documents

See the godoc page for further information. Here is a blog post about go-stm (in Japanese).

Example

// There are two bank accounts of Alice and Bob.
accountA := New(100)
accountB := New(0)

// Transfer 20 from Alice's account to Bob's one.
transfer := func(rec *TRec) interface{} {
	currA := rec.Load(accountA).(int)
	currB := rec.Load(accountB).(int)
	rec.Store(accountA, currA-20)
	rec.Store(accountB, currB+20)
	return nil
}
Atomically(transfer)

// Check the balance of accounts of Alice and Bob.
inquiries := func(rec *TRec) interface{} {
	balance := make(map[*TVar]int)
	balance[accountA] = rec.Load(accountA).(int)
	balance[accountB] = rec.Load(accountB).(int)
	return balance
}
balance := Atomically(inquiries).(map[*TVar]int)
fmt.Printf("The account of Alice holds %v.\nThe account of Bob holds %v.",
	balance[accountA], balance[accountB])
// Output:
// The account of Alice holds 80.
// The account of Bob holds 20.

Benchmark

There exists an another STM package written by lukechampine. The lukechampine's package provides a richer interface but is less efficient than the present package. Here is the result of a very simple benchmark, in which two transactional variables are atomically incremented or read. The benchmark is taken at a DigitalOcean's High CPU Droplet with 32 cores.

Benchmark_Read90Write10_decillion-2      	10000000	       230 ns/op
Benchmark_Read90Write10_decillion-4      	10000000	       156 ns/op
Benchmark_Read90Write10_decillion-8      	10000000	       144 ns/op
Benchmark_Read90Write10_decillion-16       	10000000	       214 ns/op
Benchmark_Read90Write10_decillion-32       	 5000000	       289 ns/op

Benchmark_Read90Write10_lukechampine-2   	 2000000	       715 ns/op
Benchmark_Read90Write10_lukechampine-4   	 2000000	       761 ns/op
Benchmark_Read90Write10_lukechampine-8   	 2000000	       822 ns/op
Benchmark_Read90Write10_lukechampine-16    	 2000000	       912 ns/op
Benchmark_Read90Write10_lukechampine-32    	 2000000	       966 ns/op

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.