Skip to content

Commit

Permalink
Reduce one more alloc (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
QuangTung97 committed Oct 9, 2023
1 parent 04f1933 commit 9458ba2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
29 changes: 18 additions & 11 deletions item/item.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ type getResultType[T any] struct {
err error
}

func (s *getState[T, K]) handleLeaseGranted(cas uint64) {
func (s *GetState[T, K]) handleLeaseGranted(cas uint64) {
fillFn := s.it.filler(s.ctx, s.key)
s.it.addNextCall(func(_ unsafe.Pointer) {
fillResp, err := fillFn()
Expand Down Expand Up @@ -275,7 +275,8 @@ func (s *getState[T, K]) handleLeaseGranted(cas uint64) {
})
}

type getState[T Value, K Key] struct {
// GetState store intermediate state when getting item
type GetState[T Value, K Key] struct {
ctx context.Context
key K

Expand All @@ -287,25 +288,25 @@ type getState[T Value, K Key] struct {
leaseGetResult memproxy.LeaseGetResult
}

func (s *getState[T, K]) setResponseError(err error) {
func (s *GetState[T, K]) setResponseError(err error) {
s.it.options.errorLogger(err)
s.it.getKeys[s.key] = getResultType[T]{
err: err,
}
}

func (s *getState[T, K]) setResponse(resp T) {
func (s *GetState[T, K]) setResponse(resp T) {
s.it.getKeys[s.key] = getResultType[T]{
resp: resp,
}
}

func (s *getState[T, K]) doFillFunc(cas uint64) {
func (s *GetState[T, K]) doFillFunc(cas uint64) {
s.it.stats.FillCount++
s.handleLeaseGranted(cas)
}

func (s *getState[T, K]) handleCacheError(err error) {
func (s *GetState[T, K]) handleCacheError(err error) {
s.it.stats.LeaseGetError++
if s.it.options.fillingOnCacheError {
s.it.options.errorLogger(err)
Expand All @@ -315,7 +316,7 @@ func (s *getState[T, K]) handleCacheError(err error) {
}
}

func (s *getState[T, K]) nextFunc(_ unsafe.Pointer) {
func (s *GetState[T, K]) nextFunc(_ unsafe.Pointer) {
leaseGetResp, err := s.leaseGetResult.Result()

s.leaseGetResult = nil
Expand Down Expand Up @@ -371,7 +372,8 @@ func (s *getState[T, K]) nextFunc(_ unsafe.Pointer) {
s.handleCacheError(ErrInvalidLeaseGetStatus)
}

func (s *getState[T, K]) returnFunc() (T, error) {
// Result returns result
func (s *GetState[T, K]) Result() (T, error) {
s.it.sess.Execute()

result := s.it.getKeys[s.key]
Expand All @@ -380,9 +382,14 @@ func (s *getState[T, K]) returnFunc() (T, error) {

// Get a single item with key
func (i *Item[T, K]) Get(ctx context.Context, key K) func() (T, error) {
return i.GetFast(ctx, key).Result
}

// GetFast is similar to Get but reduced one alloc
func (i *Item[T, K]) GetFast(ctx context.Context, key K) *GetState[T, K] {
keyStr := key.String()

state := &getState[T, K]{
state := &GetState[T, K]{
ctx: ctx,
key: key,

Expand All @@ -394,15 +401,15 @@ func (i *Item[T, K]) Get(ctx context.Context, key K) func() (T, error) {

_, existed := i.getKeys[key]
if existed {
return state.returnFunc
return state
}
i.getKeys[key] = getResultType[T]{}

state.leaseGetResult = i.pipeline.LeaseGet(keyStr, memproxy.LeaseGetOptions{})

i.addNextCall(state.nextFunc)

return state.returnFunc
return state
}

func (i *Item[T, K]) increaseRejectedCount(retryCount int) {
Expand Down
4 changes: 2 additions & 2 deletions mmap/mmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ func (m *Map[T, R, K]) Get(
) func() (Option[T], error) {
bucketKey := ComputeBucketKey(elemCount, rootKey, key, m.separator)

fn := m.item.Get(ctx, bucketKey)
getState := m.item.GetFast(ctx, bucketKey)

return func() (Option[T], error) {
bucket, err := fn()
bucket, err := getState.Result()
if err != nil {
return Option[T]{}, err
}
Expand Down

0 comments on commit 9458ba2

Please sign in to comment.