Skip to content

Commit

Permalink
feat: reduce allocations (11 to 7 in benchmark) by using a constant k…
Browse files Browse the repository at this point in the history
…ey, see #56
  • Loading branch information
a-h committed Jul 30, 2022
1 parent c54832e commit ff2ad31
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ func (cf ComponentFunc) Render(ctx context.Context, w io.Writer) error {

type childrenContextKey string

var contextKeyChildren = childrenContextKey("children")
const contextKeyChildren = childrenContextKey("children")

func WithChildren(ctx context.Context, children Component) context.Context {
return context.WithValue(ctx, contextKeyChildren, &children)
}
func ClearChildren(ctx context.Context) context.Context {
return context.WithValue(ctx, contextKeyChildren, nil)
}

// NopComponent is a component that doesn't render anything.
var NopComponent = ComponentFunc(func(ctx context.Context, w io.Writer) error { return nil })

Expand All @@ -63,7 +64,7 @@ type ComponentHandler struct {
ErrorHandler func(r *http.Request, err error) http.Handler
}

var componentHandlerErrorMessage = "templ: failed to render template"
const componentHandlerErrorMessage = "templ: failed to render template"

// ServeHTTP implements the http.Handler interface.
func (ch *ComponentHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -146,7 +147,8 @@ func (classes CSSClasses) String() string {
}

var safeClassName = regexp.MustCompile(`^-?[_a-zA-Z]+[_-a-zA-Z0-9]*$`)
var fallbackClassName = ConstantCSSClass("--templ-css-class-safe-name")

const fallbackClassName = ConstantCSSClass("--templ-css-class-safe-name")

// Class returns a sanitized CSS class name.
func Class(name string) CSSClass {
Expand Down Expand Up @@ -197,7 +199,7 @@ func CSSID(name string, css string) string {

type cssContextKey string

var contextKeyRenderedClasses = cssContextKey("renderedClasses")
const contextKeyRenderedClasses = cssContextKey("renderedClasses")

// RenderedCSSClassesFromContext returns a set of the CSS classes that have already been
// rendered to the response.
Expand Down Expand Up @@ -380,7 +382,7 @@ type ComponentScript struct {

type scriptContextKey string

var contextKeyRenderedScripts = scriptContextKey("scripts")
const contextKeyRenderedScripts = scriptContextKey("scripts")

// RenderedScriptsFromContext returns a set of the scripts that have already been
// rendered to the response.
Expand Down

0 comments on commit ff2ad31

Please sign in to comment.