Skip to content

Commit

Permalink
Merge pull request xssnick#206 from AugustineAurelius/master
Browse files Browse the repository at this point in the history
micro tvm optimization
  • Loading branch information
xssnick authored Jul 5, 2024
2 parents 35c68eb + 6bbba3b commit 09eedeb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 30 deletions.
68 changes: 49 additions & 19 deletions tvm/cell/cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,51 +147,78 @@ func (c *Cell) DumpBits(limitLength ...int) string {
func (c *Cell) dump(deep int, bin bool, limitLength uint64) string {
sz, data, _ := c.BeginParse().RestBits()

var val string
builder := strings.Builder{}

if bin {
for _, n := range data {
val += fmt.Sprintf("%08b", n)
builder.WriteString(fmt.Sprintf("%08b", n))
}
if sz%8 != 0 {
val = val[:uint(len(val))-(8-(sz%8))]
tmp := builder.String()
builder.Reset()
builder.WriteString(tmp[:uint(len(tmp))-(8-(sz%8))])
}
} else {
val = strings.ToUpper(hex.EncodeToString(data))
tmp := make([]byte, len(data)*2)
hex.Encode(tmp, data)
builder.WriteString(strings.ToUpper(string(tmp)))

if sz%8 <= 4 && sz%8 > 0 {
// fift hex
val = val[:len(val)-1] + "_"
tmp := builder.String()
builder.Reset()
builder.WriteString(tmp[:len(tmp)-1])
builder.WriteByte('_')

}
}

str := strings.Repeat(" ", deep) + fmt.Sprint(sz) + "[" + val + "]"
val := builder.String()
builder.Reset()
builder.WriteString(strings.Repeat(" ", deep))
builder.WriteString(strconv.FormatUint(uint64(sz), 10))
builder.WriteByte('[')
builder.WriteString(val)
builder.WriteByte(']')

if c.levelMask.GetLevel() > 0 {
str += fmt.Sprintf("{%d}", c.levelMask.GetLevel())
builder.WriteByte('{')
builder.WriteString(strconv.Itoa(c.levelMask.GetLevel()))
builder.WriteByte('}')

}
if c.special {
str += "*"
builder.WriteByte('*')
}
if len(c.refs) > 0 {
str += " -> {"

builder.WriteString(" -> {")

for i, ref := range c.refs {
str += "\n" + ref.dump(deep+1, bin, limitLength)

builder.WriteByte('\n')
builder.WriteString(ref.dump(deep+1, bin, limitLength))

if i == len(c.refs)-1 {
str += "\n"
builder.WriteByte('\n')
} else {
str += ","
builder.WriteByte(',')
}

if uint64(len(str)) > limitLength {
if uint64(builder.Len()) > limitLength {
break
}
}
str += strings.Repeat(" ", deep) + "}"
builder.WriteString(strings.Repeat(" ", deep))
builder.WriteByte('}')
}

if uint64(len(str)) > limitLength {
str = str[:limitLength]
if uint64(builder.Len()) > limitLength {
tmp := builder.String()
builder.Reset()
builder.WriteString(tmp[:limitLength])
}

return str
return builder.String()
}

const _DataCellMaxLevel = 3
Expand Down Expand Up @@ -259,10 +286,13 @@ func (c *Cell) UnmarshalJSON(bytes []byte) error {
}
bytes = bytes[1 : len(bytes)-1]

data, err := base64.StdEncoding.DecodeString(string(bytes))
data := make([]byte, base64.StdEncoding.DecodedLen(len(bytes)))

n, err := base64.StdEncoding.Decode(data, bytes)
if err != nil {
return err
}
data = data[:n]

cl, err := FromBOC(data)
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions tvm/cell/dict.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,8 +242,8 @@ func (d *Dictionary) Set(key, value *Cell) error {
if err != nil {
return fmt.Errorf("failed to set value in dict, err: %w", err)
}
d.root = newRoot

d.root = newRoot
return nil
}

Expand Down Expand Up @@ -314,13 +314,15 @@ func (d *Dictionary) IsEmpty() bool {
// Deprecated: use LoadAll, dict was reimplemented, so it will be parsed during this call, and it can return error now.
func (d *Dictionary) All() []*HashmapKV {
list, _ := d.LoadAll()
var old []*HashmapKV
for _, kv := range list {
old = append(old, &HashmapKV{
Key: kv.Key.MustToCell(),
Value: kv.Value.MustToCell(),
})

old := make([]*HashmapKV, len(list))
for i := 0; i < len(list); i++ {
old[i] = &HashmapKV{
Key: list[i].Key.MustToCell(),
Value: list[i].Value.MustToCell(),
}
}

return old
}

Expand Down Expand Up @@ -474,10 +476,10 @@ func loadLabel(sz uint, loader *Slice, key *Builder) (uint, *Builder, error) {
return 0, nil, err
}

bitsLen := uint(math.Ceil(math.Log2(float64(sz + 1))))

// hml_long$10
if second == 0 {
bitsLen := uint(math.Ceil(math.Log2(float64(sz + 1))))

ln, err := loader.LoadUInt(bitsLen)
if err != nil {
return 0, nil, err
Expand All @@ -503,8 +505,6 @@ func loadLabel(sz uint, loader *Slice, key *Builder) (uint, *Builder, error) {
return 0, nil, err
}

bitsLen := uint(math.Ceil(math.Log2(float64(sz + 1))))

ln, err := loader.LoadUInt(bitsLen)
if err != nil {
return 0, nil, err
Expand Down

0 comments on commit 09eedeb

Please sign in to comment.