some optimizations

This commit is contained in:
maddalax 2024-09-27 10:10:00 -05:00
parent f01ec9da2e
commit 8b34b63990
4 changed files with 46 additions and 20 deletions

View file

@ -26,16 +26,17 @@ func (m *AttributeMap) ToMap() map[string]string {
return result return result
} }
func Attribute(key string, value string) *AttributeMap { func Attribute(key string, value string) *AttributeR {
return Attributes(&AttributeMap{key: value}) return &AttributeR{
Name: key,
Value: value,
}
} }
func AttributeList(children ...*AttributeMap) *AttributeMap { func AttributeList(children ...*AttributeR) *AttributeMap {
m := make(AttributeMap) m := make(AttributeMap)
for _, child := range children { for _, child := range children {
for k, v := range *child { m[child.Name] = child.Value
m[k] = v
}
} }
return &m return &m
} }
@ -88,7 +89,7 @@ func HxInclude(selector string) Ren {
return Attribute(hx.IncludeAttr, selector) return Attribute(hx.IncludeAttr, selector)
} }
func HxIndicator(tag string) *AttributeMap { func HxIndicator(tag string) *AttributeR {
return Attribute(hx.IndicatorAttr, tag) return Attribute(hx.IndicatorAttr, tag)
} }
@ -96,16 +97,16 @@ func TriggerChildren() Ren {
return HxExtension("trigger-children") return HxExtension("trigger-children")
} }
func HxTriggerString(triggers ...string) *AttributeMap { func HxTriggerString(triggers ...string) *AttributeR {
trigger := hx.NewStringTrigger(strings.Join(triggers, ", ")) trigger := hx.NewStringTrigger(strings.Join(triggers, ", "))
return Attribute(hx.TriggerAttr, trigger.ToString()) 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()) 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...)) return HxTrigger(hx.OnClick(opts...))
} }
@ -145,7 +146,7 @@ func Hidden() Ren {
return Attribute("style", "display:none") return Attribute("style", "display:none")
} }
func Class(value ...string) *AttributeMap { func Class(value ...string) *AttributeR {
return Attribute("class", MergeClasses(value...)) return Attribute("class", MergeClasses(value...))
} }

View file

@ -116,12 +116,14 @@ func BenchmarkMailTo(b *testing.B) {
} }
func BenchmarkComplexPage(b *testing.B) { func BenchmarkComplexPage(b *testing.B) {
b.Skip()
b.ReportAllocs() b.ReportAllocs()
ctx := RenderContext{ ctx := RenderContext{
builder: &strings.Builder{}, builder: &strings.Builder{},
} }
page := ComplexPage()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
ComplexPage().Render(&ctx) page.Render(&ctx)
ctx.builder.Reset() ctx.builder.Reset()
} }
} }

View file

@ -35,24 +35,43 @@ func (node *Element) Render(context *RenderContext) {
} }
} }
// first pass, flatten the children totalChildren := 0
flatChildren := make([]Ren, 0, len(node.children)) shouldFlatten := false
for _, child := range node.children { for _, child := range node.children {
switch child.(type) { switch c := child.(type) {
case *ChildList: case *ChildList:
flatChildren = append(flatChildren, child.(*ChildList).Children...) shouldFlatten = true
totalChildren += len(c.Children)
default: default:
flatChildren = append(flatChildren, child) totalChildren++
}
}
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 node.children = flatChildren
}
// second pass, render any attributes within the tag // second pass, render any attributes within the tag
for _, child := range node.children { for _, child := range node.children {
switch child.(type) { switch child.(type) {
case *AttributeMap: case *AttributeMap:
child.Render(context) child.Render(context)
case *AttributeR:
child.Render(context)
case *LifeCycle: case *LifeCycle:
child.Render(context) child.Render(context)
} }
@ -68,6 +87,8 @@ func (node *Element) Render(context *RenderContext) {
switch child.(type) { switch child.(type) {
case *AttributeMap: case *AttributeMap:
continue continue
case *AttributeR:
continue
case *LifeCycle: case *LifeCycle:
continue continue
default: default:
@ -158,6 +179,8 @@ func (l *LifeCycle) Render(context *RenderContext) {
for k, v := range c.ToMap() { for k, v := range c.ToMap() {
l.fromAttributeMap(event, k, v, context) l.fromAttributeMap(event, k, v, context)
} }
case *AttributeR:
l.fromAttributeMap(event, c.Name, c.Value, context)
} }
} }
} }

View file

@ -158,7 +158,7 @@ func Checkbox(children ...Ren) *Element {
return Input("checkbox", children...) return Input("checkbox", children...)
} }
func Value(value any) *AttributeMap { func Value(value any) *AttributeR {
switch v := value.(type) { switch v := value.(type) {
case string: case string:
return Attribute("value", v) return Attribute("value", v)
@ -330,7 +330,7 @@ func Img(children ...Ren) *Element {
return Tag("img", children...) return Tag("img", children...)
} }
func Src(src string) *AttributeMap { func Src(src string) *AttributeR {
return Attribute("src", src) return Attribute("src", src)
} }