Commit 7042854d authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent 8cad1d77
......@@ -198,16 +198,13 @@ func (p *XXXTest) NEOEncodedLen() int {
var size0 int
for i := 0; i < len(p.Zzz); i++ {
a := &p.Zzz[i]
var size1 int
for i := 0; i < len((*a)); i++ {
a := &(*a)[i]
size1 += 4 + len((*a))
size0 += len((*a))
}
size0 += 4 + size1
size0 += len((*a)) * 4
}
return 12 + size0
return 12 + len(p.Zzz)*4 + size0
}
func (p *XXXTest) NEOEncode(data []byte) {
......@@ -490,10 +487,9 @@ func (p *AcceptIdentification) NEOEncodedLen() int {
var size0 int
for i := 0; i < len(p.KnownMasterList); i++ {
a := &p.KnownMasterList[i]
size0 += 10 + len((*a).Address.Host)
size0 += len((*a).Address.Host)
}
return 30 + len(p.Primary.Host) + size0
return 30 + len(p.Primary.Host) + len(p.KnownMasterList)*10 + size0
}
func (p *AcceptIdentification) NEOEncode(data []byte) {
......@@ -775,10 +771,9 @@ func (p *AnswerPartitionTable) NEOEncodedLen() int {
var size0 int
for i := 0; i < len(p.RowList); i++ {
a := &p.RowList[i]
size0 += 8 + len((*a).CellList)*8
size0 += len((*a).CellList) * 8
}
return 12 + size0
return 12 + len(p.RowList)*8 + size0
}
func (p *AnswerPartitionTable) NEOEncode(data []byte) {
......@@ -864,10 +859,9 @@ func (p *NotifyPartitionTable) NEOEncodedLen() int {
var size0 int
for i := 0; i < len(p.RowList); i++ {
a := &p.RowList[i]
size0 += 8 + len((*a).CellList)*8
size0 += len((*a).CellList) * 8
}
return 12 + size0
return 12 + len(p.RowList)*8 + size0
}
func (p *NotifyPartitionTable) NEOEncode(data []byte) {
......@@ -2577,10 +2571,9 @@ func (p *AnswerPartitionList) NEOEncodedLen() int {
var size0 int
for i := 0; i < len(p.RowList); i++ {
a := &p.RowList[i]
size0 += 8 + len((*a).CellList)*8
size0 += len((*a).CellList) * 8
}
return 12 + size0
return 12 + len(p.RowList)*8 + size0
}
func (p *AnswerPartitionList) NEOEncode(data []byte) {
......@@ -2687,10 +2680,9 @@ func (p *AnswerNodeList) NEOEncodedLen() int {
var size0 int
for i := 0; i < len(p.NodeList); i++ {
a := &p.NodeList[i]
size0 += 26 + len((*a).Address.Host)
size0 += len((*a).Address.Host)
}
return 4 + size0
return 4 + len(p.NodeList)*26 + size0
}
func (p *AnswerNodeList) NEOEncode(data []byte) {
......@@ -2894,10 +2886,9 @@ func (p *NotifyNodeInformation) NEOEncodedLen() int {
var size0 int
for i := 0; i < len(p.NodeList); i++ {
a := &p.NodeList[i]
size0 += 26 + len((*a).Address.Host)
size0 += len((*a).Address.Host)
}
return 4 + size0
return 4 + len(p.NodeList)*26 + size0
}
func (p *NotifyNodeInformation) NEOEncode(data []byte) {
......
......@@ -267,7 +267,7 @@ func (c *commonCoder) var_(varname string) string {
// information about a size
// consists of numeric & symbolic parts
// size is <num> + expr1 + expr2 + ...
// size is num + expr1 + expr2 + ...
type size struct {
num int // numeric part of size
exprv []string // symbolic part of size
......@@ -277,16 +277,22 @@ func (s *size) Add(n int) {
s.num += n
}
func (s *size) AddExpr(expr string) {
func (s *size) AddExpr(format string, a ...interface{}) {
expr := fmt.Sprintf(format, a...)
s.exprv = append(s.exprv, expr)
}
func (s *size) String() string {
sizeExpr := fmt.Sprintf("%v", s.num)
if len(s.exprv) != 0 {
sizeExpr += " + " + strings.Join(s.exprv, " + ")
sizeStr := fmt.Sprintf("%v", s.num)
exprStr := s.ExprString()
if exprStr != "" {
sizeStr += " + " + exprStr
}
return sizeExpr
return sizeStr
}
func (s *size) ExprString() string {
return strings.Join(s.exprv, " + ")
}
......@@ -319,10 +325,12 @@ var _ CodecCodeGen = (*encoder)(nil)
var _ CodecCodeGen = (*decoder)(nil)
/*
// create new sizer for subsize calculation (e.g. inside loop)
func (s *sizer) subSizer() *sizer {
return &sizer{commonCoder: commonCoder{varN: s.varN + 1}}
}
*/
func (s *sizer) resultExpr() string {
size := s.size.String()
......@@ -447,7 +455,7 @@ func (d *decoder) genBasic(assignto string, typ *types.Basic, userType types.Typ
// TODO []byte support
func (s *sizer) genStrBytes(path string) {
s.size.Add(4)
s.size.AddExpr(fmt.Sprintf("len(%s)", path))
s.size.AddExpr("len(%s)", path)
}
func (e *encoder) genStrBytes(path string) {
......@@ -481,18 +489,34 @@ func (s *sizer) genSlice(path string, typ *types.Slice, obj types.Object) {
elemSize, ok := typeSizeFixed(typ.Elem())
if ok {
s.size.Add(4)
s.size.AddExpr(fmt.Sprintf("len(%v) * %v", path, elemSize))
s.size.AddExpr("len(%v) * %v", path, elemSize)
return
}
s.size.Add(4)
curSize := s.size
s.size = size{} // zero
s.emit("for i := 0; i < len(%v); i++ {", path)
s.emit("a := &%s[i]", path)
codegenType("(*a)", typ.Elem(), obj, s)
// merge-in size updates
s.emit("%v += %v", s.var_("size"), s.size.ExprString())
s.emit("}")
if s.size.num != 0 {
curSize.AddExpr("len(%v) * %v", path, s.size.num)
}
s.size = curSize
/*
sloop := s.subSizer()
codegenType("(*a)", typ.Elem(), obj, sloop)
s.emit(sloop.generatedCode())
s.emit("%v += %v", s.var_("size"), sloop.resultExpr())
s.emit("}")
*/
}
// TODO optimize for []byte
......@@ -551,7 +575,7 @@ func (s *sizer) genMap(path string, typ *types.Map, obj types.Object) {
if keyFixed && elemFixed {
s.size.Add(4)
s.size.AddExpr(fmt.Sprintf("len(%v) * %v", path, keySize + elemSize))
s.size.AddExpr("len(%v) * %v", path, keySize + elemSize)
return
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment