Skip to content
Myr

c.String() performance improvements

Before adoption: this commit came in with the history the project adopted, so no landing records it.

``` benchmark old ns/op new ns/op delta BenchmarkOneRouteString 448 310 -30.80% benchmark old allocs new allocs delta BenchmarkOneRouteString 1 0 -100.00% benchmark old bytes new bytes delta BenchmarkOneRouteString 48 0 -100.00% ```

authored by Manu Mtz-Almeida · committed by Manu Mtz-Almeida · parent 7186200 · Browse files · 2 files+10 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+10 added−8 removed

  • other:+10 added−8 removed

2 files+10 added−8 removed

modified:render/text.go+8 added-4 removed
16 unmodified lines
1718var plainContentType = []string{"text/plain; charset=utf-8"}1920func (r String) Render(w http.ResponseWriter) error {21 WriteString(w, r.Format, r.Data)22 return nil23}2425func WriteString(w http.ResponseWriter, format string, data []interface{}) {26 header := w.Header()27 if _, exist := header["Content-Type"]; !exist {28 header["Content-Type"] = plainContentType29 }25 if len(r.Data) > 0 {26 fmt.Fprintf(w, r.Format, r.Data...)30 if len(data) > 0 {31 fmt.Fprintf(w, format, data...)32 } else {28 io.WriteString(w, r.Format)33 io.WriteString(w, format)34 }30 return nil35}
modified:context.go+2 added-4 removed
318 unmodified lines
319}320321// Writes the given string into the response body.322func (c *Context) String(code int, format string, values ...interface{}) {323 c.Render(code, render.String{324 Format: format,325 Data: values},326 )323 c.writermem.WriteHeader(code)324 render.WriteString(c.Writer, format, values)325}326327// Returns a HTTP redirect to the specific location.328func (c *Context) Redirect(code int, location string) {
126 unmodified lines