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
}
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...))
}

View file

@ -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()
}
}

View file

@ -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)
}
}
}

View file

@ -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)
}