Skip to content

Commit

Permalink
avoid store key formatting with fmt.Sprintf
Browse files Browse the repository at this point in the history
  • Loading branch information
czarcas7ic committed Mar 18, 2023
1 parent d5c94f8 commit 3cd13a6
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions x/concentrated-liquidity/types/keys.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package types

import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -116,10 +119,27 @@ func keyTickPrefixByPoolIdPrealloc(poolId uint64, preAllocBytes int) []byte {
// KeyFullPosition uses pool Id, owner, lower tick, upper tick, joinTime, freezeDuration, and positionId for keys
func KeyFullPosition(poolId uint64, addr sdk.AccAddress, lowerTick, upperTick int64, joinTime time.Time, freezeDuration time.Duration, positionId uint64) []byte {
joinTimeKey := osmoutils.FormatTimeString(joinTime)
// var builder strings.Builder
// fmt.Fprintf(&builder, "%x%s%x%s%d%s%d%s%d%s%s%s%d%s%d", PositionPrefix, KeySeparator, addr.Bytes(), KeySeparator, poolId, KeySeparator, lowerTick, KeySeparator, upperTick, KeySeparator, joinTimeKey, KeySeparator, uint64(freezeDuration), KeySeparator, positionId)
// return []byte(builder.String())
return []byte(fmt.Sprintf("%s%s%x%s%d%s%d%s%d%s%s%s%d%s%d", PositionPrefix, KeySeparator, addr.Bytes(), KeySeparator, poolId, KeySeparator, lowerTick, KeySeparator, upperTick, KeySeparator, joinTimeKey, KeySeparator, uint64(freezeDuration), KeySeparator, positionId))

var builder strings.Builder
builder.Grow(len(PositionPrefix) + 9*len(KeySeparator) + len(addr.Bytes())*2 + 5*20) // pre-allocating the buffer

builder.Write(PositionPrefix)
builder.WriteString(KeySeparator)
builder.WriteString(hex.EncodeToString(addr.Bytes()))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatUint(poolId, 10))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatInt(lowerTick, 10))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatInt(upperTick, 10))
builder.WriteString(KeySeparator)
builder.WriteString(joinTimeKey)
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatUint(uint64(freezeDuration), 10))
builder.WriteString(KeySeparator)
builder.WriteString(strconv.FormatUint(positionId, 10))

return []byte(builder.String())
}

// KeyPosition uses pool Id, owner, lower tick and upper tick for keys
Expand Down

0 comments on commit 3cd13a6

Please sign in to comment.