From ab9dc20c3c9111125032991f4476c7a390ea2010 Mon Sep 17 00:00:00 2001 From: Azareal Date: Wed, 29 Apr 2020 10:26:15 +1000 Subject: [PATCH] optimise away the array on single phrase use sync.Pool to optimise CompressBytesGzip() --- common/files.go | 23 +++++++++++++++------ common/templates/templates.go | 39 ++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/common/files.go b/common/files.go index ba777444..86218753 100644 --- a/common/files.go +++ b/common/files.go @@ -202,6 +202,7 @@ func (list SFileList) JSTmplInit() error { //rep("//var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const "+shortName+"_phrase_arr = tmplPhrases[\""+tmplName+"\"];") rep("//var plist = GetTmplPhrasesBytes("+shortName+"_tmpl_phrase_id)", "const pl=tmplPhrases[\""+tmplName+"\"];") rep(shortName+"_phrase_arr", "pl") + rep(shortName+"_phrase", "pl") rep("tmpl_"+shortName+"_vars", "t_v") rep("var c_v_", "let c_v_") @@ -434,11 +435,20 @@ func (list SFileList) Set(name string, data SFile) { list[name] = data } -func CompressBytesGzip(in []byte) ([]byte, error) { - var buff bytes.Buffer - gz, err := gzip.NewWriterLevel(&buff, gzip.BestCompression) - if err != nil { - return nil, err +var gzipBestCompress sync.Pool + +func CompressBytesGzip(in []byte) (b []byte, err error) { + var buf bytes.Buffer + ii := gzipBestCompress.Get() + var gz *gzip.Writer + if ii == nil { + gz, err = gzip.NewWriterLevel(&buf, gzip.BestCompression) + if err != nil { + return nil, err + } + } else { + gz = ii.(*gzip.Writer) + gz.Reset(&buf) } _, err = gz.Write(in) if err != nil { @@ -448,7 +458,8 @@ func CompressBytesGzip(in []byte) ([]byte, error) { if err != nil { return nil, err } - return buff.Bytes(), nil + gzipBestCompress.Put(gz) + return buf.Bytes(), nil } func CompressBytesBrotli(in []byte) ([]byte, error) { diff --git a/common/templates/templates.go b/common/templates/templates.go index 6d37a197..2884c343 100644 --- a/common/templates/templates.go +++ b/common/templates/templates.go @@ -471,7 +471,11 @@ func (c *CTemplateSet) compile(name, content, expects string, expectsInt interfa } else if !c.config.SkipInitBlock { if len(c.langIndexToName) > 0 { fout += "var " + fname + "_tmpl_phrase_id int\n\n" - fout += "var " + fname + "_phrase_arr [" + strconv.Itoa(len(c.langIndexToName)) + "][]byte\n\n" + if len(c.langIndexToName) > 1 { + fout += "var " + fname + "_phrase_arr [" + strconv.Itoa(len(c.langIndexToName)) + "][]byte\n\n" + } else { + fout += "var " + fname + "_phrase []byte\n\n" + } } fout += "// nolint\nfunc init() {\n" @@ -492,10 +496,17 @@ func (c *CTemplateSet) compile(name, content, expects string, expectsInt interfa } fout += "\t})\n" - fout += ` phrases.AddTmplIndexCallback(func(phraseSet [][]byte) { + if len(c.langIndexToName) > 1 { + fout += ` phrases.AddTmplIndexCallback(func(phraseSet [][]byte) { copy(` + fname + `_phrase_arr[:], phraseSet) }) ` + } else { + fout += ` phrases.AddTmplIndexCallback(func(phraseSet [][]byte) { + ` + fname + `_phrase = phraseSet[0] + }) +` + } } fout += "}\n\n" } @@ -575,7 +586,11 @@ if !ok { fout += "w.Write(" + fr.Body + ")\n" case fr.Type == "lang": //fout += "w.Write(plist[" + strconv.Itoa(fr.Extra.(int)) + "])\n" - fout += "w.Write(" + fname + "_phrase_arr[" + strconv.Itoa(fr.Extra.(int)) + "])\n" + if len(c.langIndexToName) == 1 { + fout += "w.Write(" + fname + "_phrase)\n" + } else { + fout += "w.Write(" + fname + "_phrase_arr[" + strconv.Itoa(fr.Extra.(int)) + "])\n" + } //case fr.Type == "identifier": default: fout += fr.Body @@ -1039,10 +1054,10 @@ func (c *CTemplateSet) compileIdentSwitchN(con CContext, n *parse.CommandNode) ( return out } -func (c *CTemplateSet) dumpSymbol(pos int, node *parse.CommandNode, symbol string) { +func (c *CTemplateSet) dumpSymbol(pos int, n *parse.CommandNode, symbol string) { c.detail("symbol:", symbol) - c.detail("node.Args[pos+1]", node.Args[pos+1]) - c.detail("node.Args[pos+2]", node.Args[pos+2]) + c.detail("n.Args[pos+1]", n.Args[pos+1]) + c.detail("n.Args[pos+2]", n.Args[pos+2]) } func (c *CTemplateSet) compareFunc(con CContext, pos int, n *parse.CommandNode, compare string) (out string) { @@ -1087,8 +1102,8 @@ func (c *CTemplateSet) compareJoin(con CContext, pos int, node *parse.CommandNod } out = left + " " + symbol + " " + right - c.detail("Left operand:", node.Args[pos-1]) - c.detail("Right operand:", node.Args[pos+1]) + c.detail("Left op:", node.Args[pos-1]) + c.detail("Right op:", node.Args[pos+1]) if !funcExists { pos++ } @@ -1671,22 +1686,22 @@ func (c *CTemplateSet) compileVarSub(con CContext, varname string, val reflect.V if c.guestOnly { c.detail("optimising away member branch") if inSlice(userExprs, varname) { - c.detail("positive conditional:", varname) + c.detail("positive condition:", varname) c.addText(con, []byte("false")) return } else if inSlice(negUserExprs, varname) { - c.detail("negative conditional:", varname) + c.detail("negative condition:", varname) c.addText(con, []byte("true")) return } } else if c.memberOnly { c.detail("optimising away guest branch") if (con.RootHolder + ".CurrentUser.Loggedin") == varname { - c.detail("positive conditional:", varname) + c.detail("positive condition:", varname) c.addText(con, []byte("true")) return } else if ("!" + con.RootHolder + ".CurrentUser.Loggedin") == varname { - c.detail("negative conditional:", varname) + c.detail("negative condition:", varname) c.addText(con, []byte("false")) return }