reduce allocs by re-using string builder

This commit is contained in:
maddalax 2024-09-19 20:40:17 -05:00
parent 93bb1d64ba
commit 2325df1593
4 changed files with 61 additions and 57 deletions

View file

@ -1,43 +1,23 @@
package h package h
type AttributeR struct { import "fmt"
Name string
Value string
}
func NewAttribute(name string, value string) *AttributeR { type AttributeMap map[string]any
return &AttributeR{
Name: name, func (m *AttributeMap) ToMap() map[string]string {
Value: value, result := make(map[string]string)
} for k, v := range *m {
} switch v.(type) {
case AttributeMap:
type TextContent struct { m2 := v.(*AttributeMap).ToMap()
Content string for _, a := range m2 {
} result[k] = a
}
func NewTextContent(content string) *TextContent { case string:
return &TextContent{ result[k] = v.(string)
Content: content, default:
} result[k] = fmt.Sprintf("%v", v)
} }
type RawContent struct {
Content string
}
func NewRawContent(content string) *RawContent {
return &RawContent{
Content: content,
}
}
type ChildList struct {
Children []Ren
}
func NewChildList(children ...Ren) *ChildList {
return &ChildList{
Children: children,
} }
return result
} }

View file

@ -0,0 +1,43 @@
package h
type AttributeR struct {
Name string
Value string
}
type TextContent struct {
Content string
}
type RawContent struct {
Content string
}
type ChildList struct {
Children []Ren
}
func NewAttribute(name string, value string) *AttributeR {
return &AttributeR{
Name: name,
Value: value,
}
}
func NewRawContent(content string) *RawContent {
return &RawContent{
Content: content,
}
}
func NewTextContent(content string) *TextContent {
return &TextContent{
Content: content,
}
}
func NewChildList(children ...Ren) *ChildList {
return &ChildList{
Children: children,
}
}

View file

@ -104,21 +104,3 @@ func (l *LifeCycle) Render(builder *strings.Builder) {
Children(children...).Render(builder) Children(children...).Render(builder)
} }
func (m *AttributeMap) ToMap() map[string]string {
result := make(map[string]string)
for k, v := range *m {
switch v.(type) {
case AttributeMap:
m2 := v.(*AttributeMap).ToMap()
for _, a := range m2 {
result[k] = a
}
case string:
result[k] = v.(string)
default:
result[k] = fmt.Sprintf("%v", v)
}
}
return result
}

View file

@ -70,7 +70,6 @@ func Id(value string) Ren {
return Attribute("id", value) return Attribute("id", value)
} }
type AttributeMap map[string]any
type ClassMap map[string]bool type ClassMap map[string]bool
func Attributes(attrs *AttributeMap) *AttributeMap { func Attributes(attrs *AttributeMap) *AttributeMap {