Skip to content

Cache compiled Stencil templates and move rendering off main thread#1232

Open
dfsm wants to merge 2 commits intomainfrom
stencil-render
Open

Cache compiled Stencil templates and move rendering off main thread#1232
dfsm wants to merge 2 commits intomainfrom
stencil-render

Conversation

@dfsm
Copy link
Copy Markdown
Contributor

@dfsm dfsm commented Mar 27, 2026

BundleResourceLoader now caches parsed Template objects in an NSCache, eliminating redundant disk I/O and lexer passes on every render cycle.
Template rendering in renderPosts() is moved to a detached task to avoid blocking the main thread during page loads.

BundleResourceLoader now caches parsed Template objects in an NSCache, eliminating redundant disk I/O and lexer passes on every render cycle.
Template rendering in renderPosts() is moved to a detached task so scroll gestures are no longer blocked by template work.
@dfsm
Copy link
Copy Markdown
Contributor Author

dfsm commented Mar 27, 2026

This PR and the HTMLReader PR are related. Both came out of some Time Profiler testing using Instruments.

Before and After for both changes:

Before vs After (recursive sample counts)

Function Before After
HTMLSelector initWithString: 18 0 (gone)
SelectorFunctionForString 17 0 (gone)
Lexer.init present 0 (gone)
Template.init present 0 (gone)
BundleResourceLoader.loadTemplate present 0 (gone)

Template re-parsing and selector re-compilation are completely eliminated from the hot path.

Copy link
Copy Markdown
Member

@nolanw nolanw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

Do you want to bring the cached selectors over to this PR too? I’m good either way. Feel free to merge if not :) also the cached selectors could certainly be a category on HTMSelector/HTMLElement if that’s the nicest way.

@dfsm
Copy link
Copy Markdown
Contributor Author

dfsm commented Mar 27, 2026

Looks good!

Do you want to bring the cached selectors over to this PR too? I’m good either way. Feel free to merge if not :) also the cached selectors could certainly be a category on HTMSelector/HTMLElement if that’s the nicest way.

Ah yep, no worries. I will bring cached selectors over to this PR so that the issue doesn't keep getting flagged in future instrument runs. i'm trying to automate some testing and instruments stuff

thanks for the quick review.
I'll add another comment to request another review after pushing those changes to this branch

…che, and update all call sites to use matchingParsedSelector: instead of matchingSelector: so parsed selectors are reused across repeated scraping operations.
@dfsm
Copy link
Copy Markdown
Contributor Author

dfsm commented Mar 28, 2026

Hello, I've added the cached selectors now

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

Successfully merging this pull request may close these issues.

2 participants