Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inefficient memory usage of Custom Pages that can Signal a renders #22

Open
JoshSweaterGuy opened this issue Jul 15, 2024 · 0 comments
Open

Comments

@JoshSweaterGuy
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant