summaryrefslogtreecommitdiff
path: root/libcss_style_sharing.mdwn
blob: 1c8a6355767eece7c69cc7438cc0faeb785778a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[[!meta title="LibCSS Style Sharing"]]
[[!meta author="Tlsa"]]
[[!meta date="2014-01-03T15:21:20Z"]]


[[!toc]] 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:

-   <http://git.netsurf-browser.org/netsurf.git/tree/render/layout.c>

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.