From a9dad532aeba656a59131f3e3e2365b3d272989e Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Mon, 11 May 2015 01:02:17 +0200 Subject: [PATCH] Performance improvement in renders --- render/html.go | 6 +++--- render/json.go | 18 +++++++++++------- render/render.go | 23 +---------------------- render/render_test.go | 6 ------ render/text.go | 2 +- render/xml.go | 2 +- 6 files changed, 17 insertions(+), 40 deletions(-) diff --git a/render/html.go b/render/html.go index 139a8ae..d7a0b89 100644 --- a/render/html.go +++ b/render/html.go @@ -21,14 +21,14 @@ type ( ) func (html HTMLRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { - WriteHeader(w, code, "text/html") + writeHeader(w, code, "text/html; charset=utf-8") file := data[0].(string) args := data[1] return html.Template.ExecuteTemplate(w, file, args) } func (r *HTMLDebugRender) Render(w http.ResponseWriter, code int, data ...interface{}) error { - WriteHeader(w, code, "text/html") + writeHeader(w, code, "text/html; charset=utf-8") file := data[0].(string) obj := data[1] @@ -57,7 +57,7 @@ func (_ htmlPlainRender) Render(w http.ResponseWriter, code int, data ...interfa } func WriteHTMLString(w http.ResponseWriter, code int, format string, values []interface{}) { - WriteHeader(w, code, "text/html") + writeHeader(w, code, "text/html; charset=utf-8") if len(values) > 0 { fmt.Fprintf(w, format, values...) } else { diff --git a/render/json.go b/render/json.go index a6bab24..5cd1fe7 100644 --- a/render/json.go +++ b/render/json.go @@ -16,16 +16,20 @@ func (_ jsonRender) Render(w http.ResponseWriter, code int, data ...interface{}) } func (_ indentedJSON) Render(w http.ResponseWriter, code int, data ...interface{}) error { - WriteHeader(w, code, "application/json") - jsonData, err := json.MarshalIndent(data[0], "", " ") + return WriteIndentedJSON(w, code, data[0]) +} + +func WriteJSON(w http.ResponseWriter, code int, data interface{}) error { + writeHeader(w, code, "application/json; charset=utf-8") + return json.NewEncoder(w).Encode(data) +} + +func WriteIndentedJSON(w http.ResponseWriter, code int, data interface{}) error { + writeHeader(w, code, "application/json; charset=utf-8") + jsonData, err := json.MarshalIndent(data, "", " ") if err != nil { return err } _, err = w.Write(jsonData) return err } - -func WriteJSON(w http.ResponseWriter, code int, data interface{}) error { - WriteHeader(w, code, "application/json") - return json.NewEncoder(w).Encode(data) -} diff --git a/render/render.go b/render/render.go index 694f400..e80958b 100644 --- a/render/render.go +++ b/render/render.go @@ -22,28 +22,7 @@ var ( _ Render = &HTMLDebugRender{} ) -func WriteHeader(w http.ResponseWriter, code int, contentType string) { - contentType = joinStrings(contentType, "; charset=utf-8") +func writeHeader(w http.ResponseWriter, code int, contentType string) { w.Header().Set("Content-Type", contentType) w.WriteHeader(code) } - -func joinStrings(a ...string) string { - if len(a) == 0 { - return "" - } - if len(a) == 1 { - return a[0] - } - n := 0 - for i := 0; i < len(a); i++ { - n += len(a[i]) - } - - b := make([]byte, n) - n = 0 - for _, s := range a { - n += copy(b[n:], s) - } - return string(b) -} diff --git a/render/render_test.go b/render/render_test.go index 0ffcf4d..3ecca0e 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -145,9 +145,3 @@ func TestRenderHTMLTemplate(t *testing.T) { assert.Equal(t, w.Body.String(), "Hello alexandernyquist") assert.Equal(t, w.Header().Get("Content-Type"), "text/html; charset=utf-8") } - -func TestRenderJoinStrings(t *testing.T) { - assert.Equal(t, joinStrings("a", "BB", "c"), "aBBc") - assert.Equal(t, joinStrings("a", "", "c"), "ac") - assert.Equal(t, joinStrings("text/html", "; charset=utf-8"), "text/html; charset=utf-8") -} diff --git a/render/text.go b/render/text.go index bfcfc6f..efd5201 100644 --- a/render/text.go +++ b/render/text.go @@ -15,7 +15,7 @@ func (_ plainTextRender) Render(w http.ResponseWriter, code int, data ...interfa } func WritePlainText(w http.ResponseWriter, code int, format string, values []interface{}) { - WriteHeader(w, code, "text/plain") + writeHeader(w, code, "text/plain; charset=utf-8") // we assume w.Write can not fail, is that right? if len(values) > 0 { fmt.Fprintf(w, format, values...) diff --git a/render/xml.go b/render/xml.go index 3792d5f..8ebe302 100644 --- a/render/xml.go +++ b/render/xml.go @@ -12,6 +12,6 @@ func (_ xmlRender) Render(w http.ResponseWriter, code int, data ...interface{}) } func WriteXML(w http.ResponseWriter, code int, data interface{}) error { - WriteHeader(w, code, "application/xml") + writeHeader(w, code, "application/xml; charset=utf-8") return xml.NewEncoder(w).Encode(data) }