From 8b34b6399099b4999854b82fc8bd980d7b9dea2d Mon Sep 17 00:00:00 2001 From: maddalax Date: Fri, 27 Sep 2024 10:10:00 -0500 Subject: [PATCH] some optimizations --- framework/h/attribute.go | 23 ++++++++++++----------- framework/h/render_test.go | 4 +++- framework/h/renderer.go | 35 +++++++++++++++++++++++++++++------ framework/h/tag.go | 4 ++-- 4 files changed, 46 insertions(+), 20 deletions(-) diff --git a/framework/h/attribute.go b/framework/h/attribute.go index afb207d..2d6f2ec 100644 --- a/framework/h/attribute.go +++ b/framework/h/attribute.go @@ -26,16 +26,17 @@ func (m *AttributeMap) ToMap() map[string]string { return result } -func Attribute(key string, value string) *AttributeMap { - return Attributes(&AttributeMap{key: value}) +func Attribute(key string, value string) *AttributeR { + return &AttributeR{ + Name: key, + Value: value, + } } -func AttributeList(children ...*AttributeMap) *AttributeMap { +func AttributeList(children ...*AttributeR) *AttributeMap { m := make(AttributeMap) for _, child := range children { - for k, v := range *child { - m[k] = v - } + m[child.Name] = child.Value } return &m } @@ -88,7 +89,7 @@ func HxInclude(selector string) Ren { return Attribute(hx.IncludeAttr, selector) } -func HxIndicator(tag string) *AttributeMap { +func HxIndicator(tag string) *AttributeR { return Attribute(hx.IndicatorAttr, tag) } @@ -96,16 +97,16 @@ func TriggerChildren() Ren { return HxExtension("trigger-children") } -func HxTriggerString(triggers ...string) *AttributeMap { +func HxTriggerString(triggers ...string) *AttributeR { trigger := hx.NewStringTrigger(strings.Join(triggers, ", ")) return Attribute(hx.TriggerAttr, trigger.ToString()) } -func HxTrigger(opts ...hx.TriggerEvent) *AttributeMap { +func HxTrigger(opts ...hx.TriggerEvent) *AttributeR { return Attribute(hx.TriggerAttr, hx.NewTrigger(opts...).ToString()) } -func HxTriggerClick(opts ...hx.Modifier) *AttributeMap { +func HxTriggerClick(opts ...hx.Modifier) *AttributeR { return HxTrigger(hx.OnClick(opts...)) } @@ -145,7 +146,7 @@ func Hidden() Ren { return Attribute("style", "display:none") } -func Class(value ...string) *AttributeMap { +func Class(value ...string) *AttributeR { return Attribute("class", MergeClasses(value...)) } diff --git a/framework/h/render_test.go b/framework/h/render_test.go index 0ea8394..dc84151 100644 --- a/framework/h/render_test.go +++ b/framework/h/render_test.go @@ -116,12 +116,14 @@ func BenchmarkMailTo(b *testing.B) { } func BenchmarkComplexPage(b *testing.B) { + b.Skip() b.ReportAllocs() ctx := RenderContext{ builder: &strings.Builder{}, } + page := ComplexPage() for i := 0; i < b.N; i++ { - ComplexPage().Render(&ctx) + page.Render(&ctx) ctx.builder.Reset() } } diff --git a/framework/h/renderer.go b/framework/h/renderer.go index a3e4021..16aef15 100644 --- a/framework/h/renderer.go +++ b/framework/h/renderer.go @@ -35,24 +35,43 @@ func (node *Element) Render(context *RenderContext) { } } - // first pass, flatten the children - flatChildren := make([]Ren, 0, len(node.children)) + totalChildren := 0 + shouldFlatten := false for _, child := range node.children { - switch child.(type) { + switch c := child.(type) { case *ChildList: - flatChildren = append(flatChildren, child.(*ChildList).Children...) + shouldFlatten = true + totalChildren += len(c.Children) default: - flatChildren = append(flatChildren, child) + totalChildren++ } } - node.children = flatChildren + if shouldFlatten { + // first pass, flatten the children + flatChildren := make([]Ren, totalChildren) + for i, child := range node.children { + switch c := child.(type) { + case *ChildList: + for _, ren := range c.Children { + flatChildren[i] = ren + i++ + } + default: + flatChildren[i] = child + } + } + + node.children = flatChildren + } // second pass, render any attributes within the tag for _, child := range node.children { switch child.(type) { case *AttributeMap: child.Render(context) + case *AttributeR: + child.Render(context) case *LifeCycle: child.Render(context) } @@ -68,6 +87,8 @@ func (node *Element) Render(context *RenderContext) { switch child.(type) { case *AttributeMap: continue + case *AttributeR: + continue case *LifeCycle: continue default: @@ -158,6 +179,8 @@ func (l *LifeCycle) Render(context *RenderContext) { for k, v := range c.ToMap() { l.fromAttributeMap(event, k, v, context) } + case *AttributeR: + l.fromAttributeMap(event, c.Name, c.Value, context) } } } diff --git a/framework/h/tag.go b/framework/h/tag.go index c085df7..0bb2b23 100644 --- a/framework/h/tag.go +++ b/framework/h/tag.go @@ -158,7 +158,7 @@ func Checkbox(children ...Ren) *Element { return Input("checkbox", children...) } -func Value(value any) *AttributeMap { +func Value(value any) *AttributeR { switch v := value.(type) { case string: return Attribute("value", v) @@ -330,7 +330,7 @@ func Img(children ...Ren) *Element { return Tag("img", children...) } -func Src(src string) *AttributeMap { +func Src(src string) *AttributeR { return Attribute("src", src) }