At the moment NetSurf does not share computed styles. Every element on every page has its own unique computed style allocated. Computed styles are quite large. Consider the following page:

It has over twenty thousand element nodes each with its own computed style. But there are less than 40 unique computed styles on the page. This is a massive waste of memory.

Computed Style Sharing

If different elements can reference the same computed styles, we can save a lot of memory.

  • Computed styles will become reference counted.
  • There are two ways to go about sharing computed styles:

    1. Memoising calls to css_get_style()

      • We'll use selection calbacks to find if node has previous sibling with same element name, classes, etc. If so the style for node can be an extra reference to the computed style of the previous sibling.
      • Presentational hint gathering for nodes needs to change such that presentational hints for two nodes can be compared.
      • Only shares styles between siblings. Could be extended to share between cousins, etc.
      • As well as saving memory, this will make selection faster.
        • It will be a big saving in cases where styles can be shared, by bypassing the need to iterate over the applicable selector chains.
        • In cases where the styles can't be shared, it will have a relatively small cost.
    2. Interning computed styles

      • Will have a performance cost.
      • Much greater scope for sharing computed styles than the above.
      • Can even share computed styles between different pages. (NetSurf will be unique in doing that.)
      • Fully compatible with the above, so we can do both.