Title: perf(core): cache media query matches between device changes by edusperoni · Pull Request #11512 · NativeScript/NativeScript · GitHub
Open Graph Title: perf(core): cache media query matches between device changes by edusperoni · Pull Request #11512 · NativeScript/NativeScript
X Title: perf(core): cache media query matches between device changes by edusperoni · Pull Request #11512 · NativeScript/NativeScript
Description: PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this PR is for. To avoid wasting your time, it's best to open a suggestion issue first and wait for approval before working on it. You have signed the CLA. All existing tests are passing: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/DevelopmentWorkflow.md#running-unit-tests-application. Tests for the changes are included - https://github.com/NativeScript/NativeScript/blob/master/tools/notes/WritingUnitTests.md. What is the current behavior? Every CSS state change on a view (a class added or removed, a pseudo-class toggled) re-collects selector candidates, and for each @media selector scope in the stylesheet that means calling checkIfMediaQueryMatches. That function reads Screen.mainScreen.widthPixels / heightPixels, the orientation and the system appearance through native getters, and re-parses the media query string with regexes, on every call, before it even looks at which features the query uses. collectCandidates only remembers queries that matched, in an array created fresh per view, so a query that does not match is re-evaluated for every scope that uses it, on every view, on every style pass. A query that uses a feature core does not evaluate at all (for example the @media (color-gamut: p3) blocks that Tailwind 4 and postcss-preset-env emit as fallbacks) is treated the same way: it returns false, so it is never cached, and it is re-checked forever even though it can never become true. In a profile of a screen with ~500 labels whose classes change together, with 52 such media scopes in the stylesheet, ~80% of the main-thread time of each interaction was inside checkIfMediaQueryMatches (native screen/appearance getters plus parseQuery); the actual selector matching was under 2%. What is the new behavior? Two commits: 1. Cache match results between device changes media-query-list: match results are cached per query string. The cache is cleared on the application orientationChanged and systemAppearanceChanged events (the same events MediaQueryList already uses for change notifications), so results stay correct across rotations and dark/light switches. onDeviceChange also clears the cache before re-evaluating the registered lists, so listener order cannot leave a list reading a stale result. css-mediaquery: matchQuery memoizes the parsed form of each query string. parseQuery itself is unchanged and still throws on invalid input, which is never cached. application/helpers-common: toggleApplicationEventListeners now queues listeners requested before the platform installs its handler and replays them in setToggleApplicationEventListenersCallback. This guarantees the cache invalidation hook is registered even when a media query is evaluated before application.ios / application.android finishes loading, and also fixes the pre-existing case where a MediaQueryList created that early never received change events. 2. Drop unevaluable media scopes and read device state lazily css-mediaquery: new isEvaluableMediaQuery(query) returns whether a query parses and uses only features the runtime evaluates (width, height, device-width, device-height, orientation, prefers-color-scheme, with min-/max- prefixes). style-scope: _populateRules skips an @media block (and anything nested in it) when its query is not evaluable, with one trace warning in the MediaQuery category. Such a block could never match, so this changes no rendering; it only stops the block from being re-checked on every style pass. @supports blocks were already dropped this way. media-query-list: checkIfMediaQueryMatches checks evaluability first and returns false for an unevaluable query without touching native state. For evaluable queries the values object uses lazy getters, so a width query no longer reads orientation or appearance, an orientation query no longer resolves the screen, and the screen is resolved once per evaluation (shared by width and device-width) rather than once per feature. Unit tests cover: cache reuse and invalidation on device change, MediaQueryList notifications staying in sync with the cache, the pending-listener replay, isEvaluableMediaQuery accept/reject cases, zero device reads for unevaluable queries, feature-driven reads for evaluable ones, and scope dropping (top-level and nested) in _populateRules. Not covered: a change in Screen.mainScreen metrics that is not accompanied by an orientation or appearance event (an external display connecting, for example). Previously such a change was picked up on the next style pass; now it is picked up on the next orientation or appearance change.
Open Graph Description: PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this...
X Description: PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this...
Opengraph URL: https://github.com/NativeScript/NativeScript/pull/11512
X: @github
Domain: github.com
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /:user_id/:repository/pull/:id/files(.:format) |
| route-controller | pull_requests |
| route-action | files |
| fetch-nonce | v2:5b9b5423-1f19-cc49-4ba1-95dbd02643ca |
| current-catalog-service-hash | ae870bc5e265a340912cde392f23dad3671a0a881730ffdadd82f2f57d81641b |
| request-id | D6E4:10D9F8:17007F8:1E3DCD7:6B01304E |
| html-safe-nonce | 54373fc0b689a4170868ede530fe7c4690b762ae39c6cead970e349484ce79d4 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENkU0OjEwRDlGODoxNzAwN0Y4OjFFM0RDRDc6NkIwMTMwNEUiLCJ2aXNpdG9yX2lkIjoiODc2MDkzODM0NzMxODI5MjU1OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ad7c711a71a544480c5b903adc6007ee1f97ad9b64ee10252bf98cfd81de1446 |
| hovercard-subject-tag | pull_request:4776694118 |
| github-keyboard-shortcuts | repository,pull-request-list,pull-request-conversation,pull-request-files-changed,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/NativeScript/NativeScript/pull/11512/files |
| twitter:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| twitter:card | summary_large_image |
| og:image | https://avatars.githubusercontent.com/u/4061767?s=400&v=4 |
| og:image:alt | PR Checklist The PR title follows our guidelines: https://github.com/NativeScript/NativeScript/blob/master/tools/notes/CONTRIBUTING.md#commit-messages. There is an issue for the bug/feature this... |
| og:site_name | GitHub |
| og:type | object |
| hostname | github.com |
| expected-hostname | github.com |
| None | cc5dfdd32f62ebd3223cab3af3aa2b268d3605218237326a48a99d1efd89495c |
| turbo-cache-control | no-preview |
| diff-view | unified |
| go-import | github.com/NativeScript/NativeScript git https://github.com/NativeScript/NativeScript.git |
| octolytics-dimension-user_id | 7392261 |
| octolytics-dimension-user_login | NativeScript |
| octolytics-dimension-repository_id | 31492490 |
| octolytics-dimension-repository_nwo | NativeScript/NativeScript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 31492490 |
| octolytics-dimension-repository_network_root_nwo | NativeScript/NativeScript |
| turbo-body-classes | logged-out env-production page-responsive full-width |
| disable-turbo | true |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 8c77c9e8439031ad54c75a250694842892f693e1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width