From 48a2b261e2046002c8d3d8f69bbba7b9af0c9457 Mon Sep 17 00:00:00 2001 From: Injun Song Date: Fri, 23 Feb 2024 23:33:32 +0900 Subject: [PATCH] perf(client): optimize with atomic.Int64 for lastSubscribeAt in pkg/varlog.(subscriber) Switches the type of `pkg/varlog.(subscriber).lastSubscribeAt` from `atomic.Value` to `atomic.Int64` to eliminate heap escapes. The frequent updates with new `time.Time` values to `lastSubscribeAt` previously led to significant heap allocation due to heap escape. By adopting `atomic.Int64`, we can prevent heap escape, thereby reducing heap allocations and enhancing performance. --- pkg/varlog/subscribe.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/varlog/subscribe.go b/pkg/varlog/subscribe.go index 523ad73bf..1bc34b668 100644 --- a/pkg/varlog/subscribe.go +++ b/pkg/varlog/subscribe.go @@ -175,7 +175,7 @@ type subscriber struct { closed atomic.Bool complete atomic.Bool - lastSubscribeAt atomic.Value + lastSubscribeAt atomic.Int64 logger *zap.Logger } @@ -199,7 +199,7 @@ func newSubscriber(ctx context.Context, topicID types.TopicID, logStreamID types done: make(chan struct{}), logger: logger.Named("subscriber").With(zap.Int32("lsid", int32(logStreamID))), } - s.lastSubscribeAt.Store(time.Now()) + s.lastSubscribeAt.Store(time.Now().UnixNano()) s.closed.Store(false) s.complete.Store(false) return s, nil @@ -237,7 +237,7 @@ func (s *subscriber) subscribe(ctx context.Context) { needExit := r.result.Error != nil if res.GLSN != types.InvalidGLSN { - s.lastSubscribeAt.Store(time.Now()) + s.lastSubscribeAt.Store(time.Now().UnixNano()) } else if res.Error == io.EOF || errors.Is(res.Error, verrors.ErrTrimmed) { s.complete.Store(true) } @@ -256,7 +256,8 @@ func (s *subscriber) subscribe(ctx context.Context) { } func (s *subscriber) getLastSubscribeAt() time.Time { - return s.lastSubscribeAt.Load().(time.Time) + nsec := s.lastSubscribeAt.Load() + return time.Unix(0, nsec) } type transmitter struct {