Skip to content

Commit

Permalink
messageID: fix atomic operation suggested by brainexe
Browse files Browse the repository at this point in the history
name                  old time/op    new time/op    delta
NewSafeID-20            7.60ns ± 1%    5.93ns ± 1%  -21.97%  (p=0.008 n=5+5)
NewSafeIDParallel-20    21.0ns ± 1%    21.0ns ± 2%     ~     (p=0.952 n=5+5)

name                  old alloc/op   new alloc/op   delta
NewSafeID-20             0.00B          0.00B          ~     (all equal)
NewSafeIDParallel-20     8.00B ± 0%     8.00B ± 0%     ~     (all equal)

name                  old allocs/op  new allocs/op  delta
NewSafeID-20              0.00           0.00          ~     (all equal)
NewSafeIDParallel-20      1.00 ± 0%      1.00 ± 0%     ~     (all equal)

See also:
- #1035 (review)

Signed-off-by: Koichi Shiraishi <zchee.io@gmail.com>
  • Loading branch information
zchee committed Feb 24, 2022
1 parent 1265b2e commit daa4329
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions messageID.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ type safeID struct {
nextID int64
}

func (s *safeID) Next() (id int) {
id = int(atomic.LoadInt64(&s.nextID))
atomic.AddInt64(&s.nextID, 1)
func (s *safeID) Next() int {
id := atomic.AddInt64(&s.nextID, 1)

return id
return int(id)
}

0 comments on commit daa4329

Please sign in to comment.