optimise bunit

This commit is contained in:
Azareal 2020-04-29 09:49:26 +10:00
parent 5b0fcd91d1
commit a7773d0ca2
1 changed files with 16 additions and 14 deletions

View File

@ -517,6 +517,8 @@ if !ok {
iw = gzw.ResponseWriter iw = gzw.ResponseWriter
} }
_ = iw _ = iw
var tmp []byte
_ = tmp
` `
} else { } else {
fout += "// nolint\nfunc Tmpl_" + fname + "(tmpl_" + fname + "_vars interface{}, w io.Writer) error {\n" fout += "// nolint\nfunc Tmpl_" + fname + "(tmpl_" + fname + "_vars interface{}, w io.Writer) error {\n"
@ -856,11 +858,11 @@ func (c *CTemplateSet) skipStructPointers(cur reflect.Value, id string) reflect.
} }
// For compileSubSwitch and compileSubTemplate // For compileSubSwitch and compileSubTemplate
func (c *CTemplateSet) checkIfValid(cur reflect.Value, varHolder string, holdreflect reflect.Value, varBit string, multiline bool) { func (c *CTemplateSet) checkIfValid(cur reflect.Value, varHolder string, holdReflect reflect.Value, varBit string, multiline bool) {
if !cur.IsValid() { if !cur.IsValid() {
c.critical("Debug Data:") c.critical("Debug Data:")
c.critical("Holdreflect:", holdreflect) c.critical("Holdreflect:", holdReflect)
c.critical("Holdreflect.Kind():", holdreflect.Kind()) c.critical("Holdreflect.Kind():", holdReflect.Kind())
if !c.config.SuperDebug { if !c.config.SuperDebug {
c.critical("cur.Kind():", cur.Kind().String()) c.critical("cur.Kind():", cur.Kind().String())
} }
@ -1100,7 +1102,7 @@ func (c *CTemplateSet) compileIdentSwitch(con CContext, node *parse.CommandNode)
c.dumpCall("compileIdentSwitch", con, node) c.dumpCall("compileIdentSwitch", con, node)
litString := func(inner string, bytes bool) { litString := func(inner string, bytes bool) {
if !bytes { if !bytes {
inner = "StringToBytes(" + inner + ")" inner = "StringToBytes(" + inner + "/*,tmp*/)"
} }
out = "w.Write(" + inner + ")\n" out = "w.Write(" + inner + ")\n"
literal = true literal = true
@ -1307,7 +1309,7 @@ ArgLoop:
} }
leftParam, _ := c.compileIfVarSub(con, leftOp) leftParam, _ := c.compileIfVarSub(con, leftOp)
out = "{\nbyteFloat, unit := c.ConvertByteUnit(float64(" + leftParam + "))\n" out = "{\nbyteFloat, unit := c.ConvertByteUnit(float64(" + leftParam + "))\n"
out += "w.Write(StringToBytes(fmt.Sprintf(\"%.1f\", byteFloat) + unit))\n}\n" out += "w.Write(StringToBytes(fmt.Sprintf(\"%.1f\", byteFloat)/*,tmp*/))\nw.Write(StringToBytes(unit/*,tmp*/))\n}\n"
literal = true literal = true
c.importMap["fmt"] = "fmt" c.importMap["fmt"] = "fmt"
break ArgLoop break ArgLoop
@ -1629,7 +1631,7 @@ func (c *CTemplateSet) compileVarSub(con CContext, varname string, val reflect.V
// Is this a literal string? // Is this a literal string?
if len(varname) != 0 && varname[0] == '"' { if len(varname) != 0 && varname[0] == '"' {
con.Push("lvarsub", onEnd(assLines+"w.Write(StringToBytes("+varname+"))\n")) con.Push("lvarsub", onEnd(assLines+"w.Write(StringToBytes("+varname+"/*,tmp*/))\n"))
return return
} }
for _, varItem := range c.varList { for _, varItem := range c.varList {
@ -1660,7 +1662,7 @@ func (c *CTemplateSet) compileVarSub(con CContext, varname string, val reflect.V
switch val.Kind() { switch val.Kind() {
case reflect.Int: case reflect.Int:
c.importMap["strconv"] = "strconv" c.importMap["strconv"] = "strconv"
base = "StringToBytes(strconv.Itoa(" + varname + "))" base = "StringToBytes(strconv.Itoa(" + varname + ")/*,tmp*/)"
case reflect.Bool: case reflect.Bool:
// TODO: Take c.memberOnly into account // TODO: Take c.memberOnly into account
// TODO: Make this a template fragment so more optimisations can be applied to this // TODO: Make this a template fragment so more optimisations can be applied to this
@ -1710,28 +1712,28 @@ func (c *CTemplateSet) compileVarSub(con CContext, varname string, val reflect.V
if val.Type().Name() != "string" && !strings.HasPrefix(varname, "string(") { if val.Type().Name() != "string" && !strings.HasPrefix(varname, "string(") {
varname = "string(" + varname + ")" varname = "string(" + varname + ")"
} }
base = "StringToBytes(" + varname + ")" base = "StringToBytes(" + varname + "/*,tmp*/)"
// We don't to waste time on this conversion / w.Write call when guests don't have sessions // We don't to waste time on this conversion / w.Write call when guests don't have sessions
// TODO: Implement this properly // TODO: Implement this properly
if c.guestOnly && base == "StringToBytes("+con.RootHolder+".CurrentUser.Session))" { if c.guestOnly && base == "StringToBytes("+con.RootHolder+".CurrentUser.Session/*,tmp*/))" {
return return
} }
case reflect.Int8, reflect.Int16, reflect.Int32: case reflect.Int8, reflect.Int16, reflect.Int32:
c.importMap["strconv"] = "strconv" c.importMap["strconv"] = "strconv"
base = "StringToBytes(strconv.FormatInt(int64(" + varname + "), 10))" base = "StringToBytes(strconv.FormatInt(int64(" + varname + "), 10)/*,tmp*/)"
case reflect.Int64: case reflect.Int64:
c.importMap["strconv"] = "strconv" c.importMap["strconv"] = "strconv"
base = "StringToBytes(strconv.FormatInt(" + varname + ", 10))" base = "StringToBytes(strconv.FormatInt(" + varname + ", 10)/*,tmp*/)"
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32:
c.importMap["strconv"] = "strconv" c.importMap["strconv"] = "strconv"
base = "StringToBytes(strconv.FormatUint(uint64(" + varname + "), 10))" base = "StringToBytes(strconv.FormatUint(uint64(" + varname + "), 10)/*,tmp*/)"
case reflect.Uint64: case reflect.Uint64:
c.importMap["strconv"] = "strconv" c.importMap["strconv"] = "strconv"
base = "StringToBytes(strconv.FormatUint(" + varname + ", 10))" base = "StringToBytes(strconv.FormatUint(" + varname + ", 10)/*,tmp*/)"
case reflect.Struct: case reflect.Struct:
// TODO: Avoid clashing with other packages which have structs named Time // TODO: Avoid clashing with other packages which have structs named Time
if val.Type().Name() == "Time" { if val.Type().Name() == "Time" {
base = "StringToBytes(" + varname + ".String())" base = "StringToBytes(" + varname + ".String()/*,tmp*/)"
} else { } else {
if !val.IsValid() { if !val.IsValid() {
panic(assLines + varname + "^\n" + "Invalid value. Maybe, it doesn't exist?") panic(assLines + varname + "^\n" + "Invalid value. Maybe, it doesn't exist?")