Bug 260925

Summary: Use of many shadow trees with unique style triggers significant CPU usage and a rise in process size
Product: WebKit Reporter: Kin Blas <jblas>
Component: CSSAssignee: Antti Koivisto <koivisto>
Status: RESOLVED FIXED    
Severity: Normal CC: abucur, koivisto, rniwa, simon.fraser, webkit-bug-importer, zalan
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Mac (Apple Silicon)   
OS: macOS 13   
Bug Depends on: 262891    
Bug Blocks: 154907    
Attachments:
Description Flags
HTML file that illustrates the performance degradation and rise in memory. Press the Test button and watch the process size grow.
none
Video demonstrating what we see when running the test case. none

Description Kin Blas 2023-08-30 14:33:49 PDT
Created attachment 467500 [details]
HTML file that illustrates the performance degradation and rise in memory. Press the Test button and watch the process size grow.

In our web application we've noticed a significant rise in process memory after showing and hiding DOM UI subtrees made up of a large number of custom elements that utilize style tags that refer to CSS custom properties.

In Safari 16.5 as well as WebKit 267464@main, the attached test case can start out consuming about 44 MB but after a few iterations, the process size can jump up to 1.6 GB or more. A GC or something will eventually kick in and the process size will drop back down to the 100 MB range, but then the process size will slowly climb back up again to the 1 GB range. As memory grows, performance also degrades significantly to the point where rendering stops happening, and you'll see occasional flashes of the content being shown.
Comment 1 Kin Blas 2023-08-30 14:59:42 PDT
Created attachment 467501 [details]
Video demonstrating what we see when running the test case.

Attaching a quick video that shows what we see when running the test case.
Comment 2 Radar WebKit Bug Importer 2023-08-30 16:41:38 PDT
<rdar://problem/114731415>
Comment 3 Antti Koivisto 2023-10-09 08:21:21 PDT
The slowness here is caused by bug 262891.
Comment 4 Antti Koivisto 2023-10-09 08:26:34 PDT
The memory growth appears to be a separate issue from that. In trunk it can go over 1GB but at some point it drops down to ~150MB range. I suspect this behavior is GC related.
Comment 5 Antti Koivisto 2023-10-10 03:05:13 PDT
The reason for memory growth is that the style resolver sharing cache in Style::Scope has unlimited size and the test case keeps adding more items there. Low memory handler clears the cache causing memory use to eventually drop.
Comment 6 Antti Koivisto 2023-10-10 03:16:45 PDT
Pull request: https://github.com/WebKit/WebKit/pull/18889
Comment 7 EWS 2023-10-10 08:23:40 PDT
Committed 269142@main (b3c5d420be05): <https://commits.webkit.org/269142@main>

Reviewed commits have been landed. Closing PR #18889 and removing active labels.