Browse Source

Avoid memory allocation in ExecuteFuncString if the template has no tags

Aliaksandr Valialkin 9 years ago
parent
commit
d090d65668
1 changed files with 5 additions and 0 deletions
  1. 5 0
      template.go

+ 5 - 0
template.go

@@ -81,6 +81,11 @@ func Execute(template, startTag, endTag string, w io.Writer, m map[string]interf
 // This function is optimized for constantly changing templates.
 // Use Template.ExecuteFuncString for frozen templates.
 func ExecuteFuncString(template, startTag, endTag string, f TagFunc) string {
+	tagsCount := bytes.Count(unsafeString2Bytes(template), unsafeString2Bytes(startTag))
+	if tagsCount == 0 {
+		return template
+	}
+
 	bb := byteBufferPool.Get()
 	if _, err := ExecuteFunc(template, startTag, endTag, bb, f); err != nil {
 		panic(fmt.Sprintf("unexpected error: %s", err))