From 30943ded71e123886291ad393e55bfb6aa837df3 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 8 Jun 2022 02:28:37 -0600 Subject: BIG change: generic Var[T], txVar, etc. --- rate/ratelimit.go | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'rate/ratelimit.go') diff --git a/rate/ratelimit.go b/rate/ratelimit.go index a44f383..7fde1ce 100644 --- a/rate/ratelimit.go +++ b/rate/ratelimit.go @@ -13,9 +13,9 @@ import ( type numTokens = int type Limiter struct { - max *stm.Var - cur *stm.Var - lastAdd *stm.Var + max *stm.Var[numTokens] + cur *stm.Var[numTokens] + lastAdd *stm.Var[time.Time] rate Limit } @@ -36,9 +36,9 @@ func Every(interval time.Duration) Limit { func NewLimiter(rate Limit, burst numTokens) *Limiter { rl := &Limiter{ - max: stm.NewVar(burst), + max: stm.NewVar[int](burst), cur: stm.NewBuiltinEqVar(burst), - lastAdd: stm.NewVar(time.Now()), + lastAdd: stm.NewVar[time.Time](time.Now()), rate: rate, } if rate != Inf { @@ -49,7 +49,7 @@ func NewLimiter(rate Limit, burst numTokens) *Limiter { func (rl *Limiter) tokenGenerator(interval time.Duration) { for { - lastAdd := stm.AtomicGet(rl.lastAdd).(time.Time) + lastAdd := stm.AtomicGet(rl.lastAdd) time.Sleep(time.Until(lastAdd.Add(interval))) now := time.Now() available := numTokens(now.Sub(lastAdd) / interval) @@ -57,17 +57,17 @@ func (rl *Limiter) tokenGenerator(interval time.Duration) { continue } stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { - cur := tx.Get(rl.cur).(numTokens) - max := tx.Get(rl.max).(numTokens) + cur := rl.cur.Get(tx) + max := rl.max.Get(tx) tx.Assert(cur < max) newCur := cur + available if newCur > max { newCur = max } if newCur != cur { - tx.Set(rl.cur, newCur) + rl.cur.Set(tx, newCur) } - tx.Set(rl.lastAdd, lastAdd.Add(interval*time.Duration(available))) + rl.lastAdd.Set(tx, lastAdd.Add(interval*time.Duration(available))) })) } } @@ -90,9 +90,9 @@ func (rl *Limiter) takeTokens(tx *stm.Tx, n numTokens) bool { if rl.rate == Inf { return true } - cur := tx.Get(rl.cur).(numTokens) + cur := rl.cur.Get(tx) if cur >= n { - tx.Set(rl.cur, cur-n) + rl.cur.Set(tx, cur-n) return true } return false @@ -106,17 +106,17 @@ func (rl *Limiter) WaitN(ctx context.Context, n int) error { ctxDone, cancel := stmutil.ContextDoneVar(ctx) defer cancel() if err := stm.Atomically(func(tx *stm.Tx) interface{} { - if tx.Get(ctxDone).(bool) { + if ctxDone.Get(tx) { return ctx.Err() } if rl.takeTokens(tx, n) { return nil } - if n > tx.Get(rl.max).(numTokens) { + if n > rl.max.Get(tx) { return errors.New("burst exceeded") } if dl, ok := ctx.Deadline(); ok { - if tx.Get(rl.cur).(numTokens)+numTokens(dl.Sub(tx.Get(rl.lastAdd).(time.Time))/rl.rate.interval()) < n { + if rl.cur.Get(tx)+numTokens(dl.Sub(rl.lastAdd.Get(tx))/rl.rate.interval()) < n { return context.DeadlineExceeded } } -- cgit v1.2.3 From 2ef3a53065a49ba3238203484588f1f72ccf25fd Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 8 Jun 2022 03:16:02 -0600 Subject: remove unnecessary type parameters --- bench_test.go | 8 ++++---- cmd/santa-example/main.go | 8 ++++---- doc_test.go | 2 +- external_test.go | 4 ++-- rate/ratelimit.go | 4 ++-- stm_test.go | 22 +++++++++++----------- stmutil/context.go | 2 +- 7 files changed, 25 insertions(+), 25 deletions(-) (limited to 'rate/ratelimit.go') diff --git a/bench_test.go b/bench_test.go index 0b84715..0d40caf 100644 --- a/bench_test.go +++ b/bench_test.go @@ -8,14 +8,14 @@ import ( ) func BenchmarkAtomicGet(b *testing.B) { - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < b.N; i++ { AtomicGet(x) } } func BenchmarkAtomicSet(b *testing.B) { - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < b.N; i++ { AtomicSet(x, 0) } @@ -24,7 +24,7 @@ func BenchmarkAtomicSet(b *testing.B) { func BenchmarkIncrementSTM(b *testing.B) { for i := 0; i < b.N; i++ { // spawn 1000 goroutines that each increment x by 1 - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < 1000; i++ { go Atomically(VoidOperation(func(tx *Tx) { cur := x.Get(tx) @@ -83,7 +83,7 @@ func BenchmarkReadVarSTM(b *testing.B) { for i := 0; i < b.N; i++ { var wg sync.WaitGroup wg.Add(1000) - x := NewVar[int](0) + x := NewVar(0) for i := 0; i < 1000; i++ { go func() { AtomicGet(x) diff --git a/cmd/santa-example/main.go b/cmd/santa-example/main.go index 1a93ae4..dcc8067 100644 --- a/cmd/santa-example/main.go +++ b/cmd/santa-example/main.go @@ -64,7 +64,7 @@ func (g gate) operate() { func newGate(capacity int) gate { return gate{ capacity: capacity, - remaining: stm.NewVar[int](0), // gate starts out closed + remaining: stm.NewVar(0), // gate starts out closed } } @@ -77,9 +77,9 @@ type group struct { func newGroup(capacity int) *group { return &group{ capacity: capacity, - remaining: stm.NewVar[int](capacity), // group starts out with full capacity - gate1: stm.NewVar[gate](newGate(capacity)), - gate2: stm.NewVar[gate](newGate(capacity)), + remaining: stm.NewVar(capacity), // group starts out with full capacity + gate1: stm.NewVar(newGate(capacity)), + gate2: stm.NewVar(newGate(capacity)), } } diff --git a/doc_test.go b/doc_test.go index 670d07b..ae1af9a 100644 --- a/doc_test.go +++ b/doc_test.go @@ -6,7 +6,7 @@ import ( func Example() { // create a shared variable - n := stm.NewVar[int](3) + n := stm.NewVar(3) // read a variable var v int diff --git a/external_test.go b/external_test.go index d0f5ecf..201712d 100644 --- a/external_test.go +++ b/external_test.go @@ -99,9 +99,9 @@ func BenchmarkInvertedThunderingHerd(b *testing.B) { for i := 0; i < b.N; i++ { done := stm.NewBuiltinEqVar(false) tokens := stm.NewBuiltinEqVar(0) - pending := stm.NewVar[stmutil.Settish](stmutil.NewSet()) + pending := stm.NewVar(stmutil.NewSet()) for range iter.N(1000) { - ready := stm.NewVar[bool](false) + ready := stm.NewVar(false) stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { pending.Set(tx, pending.Get(tx).Add(ready)) })) diff --git a/rate/ratelimit.go b/rate/ratelimit.go index 7fde1ce..6645e04 100644 --- a/rate/ratelimit.go +++ b/rate/ratelimit.go @@ -36,9 +36,9 @@ func Every(interval time.Duration) Limit { func NewLimiter(rate Limit, burst numTokens) *Limiter { rl := &Limiter{ - max: stm.NewVar[int](burst), + max: stm.NewVar(burst), cur: stm.NewBuiltinEqVar(burst), - lastAdd: stm.NewVar[time.Time](time.Now()), + lastAdd: stm.NewVar(time.Now()), rate: rate, } if rate != Inf { diff --git a/stm_test.go b/stm_test.go index 4103563..8726d40 100644 --- a/stm_test.go +++ b/stm_test.go @@ -11,7 +11,7 @@ import ( ) func TestDecrement(t *testing.T) { - x := NewVar[int](1000) + x := NewVar(1000) for i := 0; i < 500; i++ { go Atomically(VoidOperation(func(tx *Tx) { cur := x.Get(tx) @@ -35,7 +35,7 @@ func TestDecrement(t *testing.T) { // read-only transaction aren't exempt from calling tx.inputsChanged func TestReadVerify(t *testing.T) { read := make(chan struct{}) - x, y := NewVar[int](1), NewVar[int](2) + x, y := NewVar(1), NewVar(2) // spawn a transaction that writes to x go func() { @@ -61,7 +61,7 @@ func TestReadVerify(t *testing.T) { } func TestRetry(t *testing.T) { - x := NewVar[int](10) + x := NewVar(10) // spawn 10 transactions, one every 10 milliseconds. This will decrement x // to 0 over the course of 100 milliseconds. go func() { @@ -93,7 +93,7 @@ func TestVerify(t *testing.T) { type foo struct { i int } - x := NewVar[*foo](&foo{3}) + x := NewVar(&foo{3}) read := make(chan struct{}) // spawn a transaction that modifies x @@ -128,14 +128,14 @@ func TestSelect(t *testing.T) { require.Panics(t, func() { Atomically(Select[struct{}]()) }) // with one arg, Select adds no effect - x := NewVar[int](2) + x := NewVar(2) Atomically(Select(VoidOperation(func(tx *Tx) { tx.Assert(x.Get(tx) == 2) }))) picked := Atomically(Select( // always blocks; should never be selected - func(tx *Tx)int { + func(tx *Tx) int { tx.Retry() panic("unreachable") }, @@ -179,7 +179,7 @@ func TestPanic(t *testing.T) { func TestReadWritten(t *testing.T) { // reading a variable written in the same transaction should return the // previously written value - x := NewVar[int](3) + x := NewVar(3) Atomically(VoidOperation(func(tx *Tx) { x.Set(tx, 5) tx.Assert(x.Get(tx) == 5) @@ -188,7 +188,7 @@ func TestReadWritten(t *testing.T) { func TestAtomicSetRetry(t *testing.T) { // AtomicSet should cause waiting transactions to retry - x := NewVar[int](3) + x := NewVar(3) done := make(chan struct{}) go func() { Atomically(VoidOperation(func(tx *Tx) { @@ -207,9 +207,9 @@ func TestAtomicSetRetry(t *testing.T) { func testPingPong(t testing.TB, n int, afterHit func(string)) { ball := NewBuiltinEqVar(false) - doneVar := NewVar[bool](false) - hits := NewVar[int](0) - ready := NewVar[bool](true) // The ball is ready for hitting. + doneVar := NewVar(false) + hits := NewVar(0) + ready := NewVar(true) // The ball is ready for hitting. var wg sync.WaitGroup bat := func(from, to bool, noise string) { defer wg.Done() diff --git a/stmutil/context.go b/stmutil/context.go index 9d23e12..6f8ba9b 100644 --- a/stmutil/context.go +++ b/stmutil/context.go @@ -26,7 +26,7 @@ func ContextDoneVar(ctx context.Context) (*stm.Var[bool], func()) { v := stm.NewBuiltinEqVar(true) return v, func() {} } - v := stm.NewVar[bool](false) + v := stm.NewVar(false) go func() { <-ctx.Done() stm.AtomicSet(v, true) -- cgit v1.2.3 From a7900ffa1ba5655a5fad903c6634c888d3025d86 Mon Sep 17 00:00:00 2001 From: Chris Wendt Date: Wed, 8 Jun 2022 03:18:43 -0600 Subject: eliminate some type assertions --- README.md | 2 +- external_test.go | 12 ++++++------ rate/ratelimit.go | 8 ++++---- 3 files changed, 11 insertions(+), 11 deletions(-) (limited to 'rate/ratelimit.go') diff --git a/README.md b/README.md index 8073b9d..ba770d9 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ type foo struct { } f := foo{i: stm.NewVar[int](2)} stm.Atomically(func(tx *stm.Tx) { - i := f.i.Get(tx).(int) + i := f.i.Get(tx) i = 7 f.i.Set(tx, i) }) diff --git a/external_test.go b/external_test.go index 201712d..a56aeee 100644 --- a/external_test.go +++ b/external_test.go @@ -78,14 +78,14 @@ func BenchmarkThunderingHerd(b *testing.B) { }() } go func() { - for stm.Atomically(func(tx *stm.Tx) interface{} { + for stm.Atomically(func(tx *stm.Tx) bool { if done.Get(tx) { return false } tx.Assert(tokens.Get(tx) < maxTokens) tokens.Set(tx, tokens.Get(tx)+1) return true - }).(bool) { + }) { } }() stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { @@ -118,18 +118,18 @@ func BenchmarkInvertedThunderingHerd(b *testing.B) { }() } go func() { - for stm.Atomically(func(tx *stm.Tx) interface{} { + for stm.Atomically(func(tx *stm.Tx) bool { if done.Get(tx) { return false } tx.Assert(tokens.Get(tx) < maxTokens) tokens.Set(tx, tokens.Get(tx)+1) return true - }).(bool) { + }) { } }() go func() { - for stm.Atomically(func(tx *stm.Tx) interface{} { + for stm.Atomically(func(tx *stm.Tx) bool { tx.Assert(tokens.Get(tx) > 0) tokens.Set(tx, tokens.Get(tx)-1) pending.Get(tx).Range(func(i interface{}) bool { @@ -141,7 +141,7 @@ func BenchmarkInvertedThunderingHerd(b *testing.B) { return true }) return !done.Get(tx) - }).(bool) { + }) { } }() stm.Atomically(stm.VoidOperation(func(tx *stm.Tx) { diff --git a/rate/ratelimit.go b/rate/ratelimit.go index 6645e04..f521f66 100644 --- a/rate/ratelimit.go +++ b/rate/ratelimit.go @@ -77,9 +77,9 @@ func (rl *Limiter) Allow() bool { } func (rl *Limiter) AllowN(n numTokens) bool { - return stm.Atomically(func(tx *stm.Tx) interface{} { + return stm.Atomically(func(tx *stm.Tx) bool { return rl.takeTokens(tx, n) - }).(bool) + }) } func (rl *Limiter) AllowStm(tx *stm.Tx) bool { @@ -105,7 +105,7 @@ func (rl *Limiter) Wait(ctx context.Context) error { func (rl *Limiter) WaitN(ctx context.Context, n int) error { ctxDone, cancel := stmutil.ContextDoneVar(ctx) defer cancel() - if err := stm.Atomically(func(tx *stm.Tx) interface{} { + if err := stm.Atomically(func(tx *stm.Tx) error { if ctxDone.Get(tx) { return ctx.Err() } @@ -123,7 +123,7 @@ func (rl *Limiter) WaitN(ctx context.Context, n int) error { tx.Retry() panic("unreachable") }); err != nil { - return err.(error) + return err } return nil -- cgit v1.2.3