inefficient memory usage of Custom Pages that can Signal a renders
JoshSweaterGuy opened this issue · comments
Joshua Davis commented
When a custom page is rendered , it is necessary to keep its shallow children in memory due to needing to know which conditionals change.
So to more efficiently do this, only store the hash values of the shallow conditionals as a string.
Conditionals
...
var body: some Page {
Div {
if toggle {
H1("My Title")
}
if toggle2 {
H2("subtitle")
if toggle3 {
H3("Third Head")
}
}
}
}
...
previously the entire body page was store but now could get away with only storing
// 0 is a false conditional , 1 is true conditional, -1 if false no else clause, else if is a bit more complex but still unique
// toggle = false, toggle2 = false, toggle3 = ANYTHING
"(0,0)"
// toggle = false, toggle2 = true, toggle3 = false
"(0(1,0))"
// toggle = true, toggle2 = true, toggle3 = true
"(1(1,1))"
For-Loop
...
var people: [String] = ["Jeff", "Josh", "Todd", "Emma", "Jessica", "Tom"]
var body: some Page {
Div {
for person in people {
H2(person)
}
}
}
...
by default the id for a for loop is the .count amount of elements
"([6])"
...
var people: [String] = ["Jeff", "Josh", "Todd", "Emma", "Jessica", "Tom"]
var body: some Page {
Div {
for person in people {
H2(person)
.key(person)
}
}
}
...
use .key to create a custom key and the new id gets a bit more complicated
"([Jeff,Josh,Todd,Emma,Jessica,Tom])"
Complex Example
...
var people: [String] = ["Jeff", "Josh", "Todd", "Emma", "Jessica", "Tom"]
var body: some Page {
Div {
if toggle {
H1("My Title")
}
for person in people {
H2(person)
}
if toggle2 {
if toggle3 {
H2("subtitle")
}
if toggle4 {
for person in people {
H2(person)
.key(person[0])
}
}
}
}
}
...
// toggle = true, toggle2 = true, toggle3 = false, toggle4 = true
"(1,[6],(1,0(1,[J,J,T,E,J,T])))"
Problems
what happens in the case below the id would stay the same for a for loop but it should change
var people: [String] = ["Jeff,", "Josh"]
...
var people: [String] = ["Jeff", ",Josh"]
Consider hashing in base32 so there are no commas?