Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions docs/validation/direct-subject-selector-invalidation-243.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Direct-subject selector invalidation — 21 September 2026

WebScene issue #243 identified repeated selector-transition planning as a
material owner of unchanged Code OSS workspace startup. Three traced launches
spent 560–587 ms planning and applying 4,205–4,403 selector transitions, while
layout consumed 219–247 ms. Common list-row class mutations repeatedly planned
roughly 800–1,300 rules even though the changed selector feature was on the
selector's final compound and only the mutated element could be affected.

The runtime now sends final-compound, subject-only dependencies directly to the
existing subject cascade. Dependencies that route through an ancestor,
descendant, child, or sibling keep the compiled transition planner. Inherited
properties and custom-property consumers keep their existing propagation.
`WEBSCENE_PROBE_DISABLE_DIRECT_SUBJECT_INVALIDATION=1` restores the old path in
certification builds for an identical-input A/B comparison.

## Product-neutral gate

`WEBSCENE_NATIVE_ENGINE_TEST_FILTER=direct-subject-invalidation` mutates 64
elements across 512 relevant class rules, first with 32 and then with 1,024
unrelated rules. Addition and removal preserve computed width and perform the
exact 128 required subject cascades.

| Path | 32 unrelated | 1,024 unrelated | Plan lookups | Candidate/fallback visits |
| --- | ---: | ---: | ---: | ---: |
| old planner | 71.41 ms | 72.42 ms | 65,664 | 0 / 0 |
| direct subject | 52.83 ms | 40.72 ms | 0 | 0 / 0 |

The descendant/child/adjacent/general-sibling attribute invalidation contract
passes. The broader CSS invalidation matrix preserved identical bounded counters
at 32 and 1,024 unrelated rules through the existing descendant, sibling,
disabled-state, variable, relational, identity, dataset, and structural cases.
Its pre-existing `tree-reactions` case still exceeds its compound-check ceiling;
the untouched baseline also has pre-existing failures before that point. The
unrelated live-form filter fails identically on the untouched baseline because
`:invalid` now includes both the form and its invalid control.

## Unchanged Code OSS gate

The candidate was produced through the AppScene SDK pipeline at clean AppScene
`cd0a02ecd15f0a635bccc242cdd657888db39e25`, WebScene base
`a99eaf23aef5e5525b0a58c5635fa8a431b604f1` plus this patch, and unchanged Code
OSS `645f29cc3176500b4b5762ba887cf2a7f0ffdf2c`. The SDK inventory contained 2,906
files, AppScene passed 31/31 tests, and the macOS compiler/runtime profile passed.

Twenty clean native launches all reconstructed the exact remote root, registered
the stock provider, resolved the exact five entries, populated the five-entry
Explorer model, painted all five rows, and stayed within 64 Explorer DOM nodes.

| Boundary | Clean base | Candidate | Change |
| --- | ---: | ---: | ---: |
| provider resolve p50 | 914.43 ms | 751.61 ms | -17.8% |
| provider resolve p95 | 1,158.98 ms | 762.64 ms | -34.2% |
| first-child paint p50 | 2,164.26 ms | 1,828.19 ms | -15.5% |
| first-child paint p95 | 2,262.03 ms | 1,860.12 ms | -17.8% |
| launches within 2 seconds | 9/20 | 20/20 | +11 passes |

The candidate report is
`/private/tmp/workspace-direct-subject-243-cycles/report.json`, SHA-256
`5387023d909650235e83d1776302bf86beef65d8bd55585ae938972fd7cd5f2f`.
A traced candidate launch reduced selector-transition time from 559.59 ms to
448.29 ms and maximum WebSocket queue delay from 883.27 ms to 544.19 ms while
remaining inside the product paint bound. Its compact report SHA-256 is
`41fd9d5be3a41b99309b52d806890564ef5865f830892a020639b528aa31962a`.
6 changes: 6 additions & 0 deletions experiments/WebScene.NativeEngine.Probe/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1127,6 +1127,12 @@ if(WEBSCENE_NATIVE_ENGINE_ENABLE_V8)
COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_recursive_selector_cache
COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_direct_subject_invalidation
COMMAND webscene_native_engine_tests)
set_tests_properties(webscene_native_direct_subject_invalidation PROPERTIES
ENVIRONMENT "WEBSCENE_NATIVE_ENGINE_TEST_FILTER=direct-subject-invalidation"
LABELS "native;css;invalidation;performance;lifecycle"
TIMEOUT 30)
add_test(NAME webscene_native_indexeddb_contract COMMAND webscene_native_engine_tests)
add_test(NAME webscene_native_terminal_indexeddb_navigation
COMMAND webscene_native_engine_tests)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2301,15 +2301,68 @@
const auto route_targets = [&](const css::css_invalidation_route& route) {
return css_invalidation_route_targets(subject, context_root, route);
};
// Most class mutations in application UI trees affect only the
// mutated element. Re-matching every dependency compound in every
// directly keyed rule first is redundant: one full cascade of the
// subject already determines which of those rules entered or left the
// match set. Keep the transition planner for dependencies that route
// to ancestors, descendants, or siblings, where it identifies other
// subjects that must be recascaded.
std::vector<size_t> routed_rules;
routed_rules.reserve(changed_rules.size());
size_t direct_subject_rules = 0U;
#if defined(WEBSCENE_NATIVE_ENGINE_CERTIFICATION)
const auto direct_subject_fast_path =
std::getenv("WEBSCENE_PROBE_DISABLE_DIRECT_SUBJECT_INVALIDATION")
== nullptr;
#else
constexpr auto direct_subject_fast_path = true;
#endif
for (const auto index : changed_rules) {
if (index >= css_rules.size()) continue;
const auto& rule = css_rules[index];
const auto& compiled = rule.payload->compiled_pseudo_origin.compounds.empty()
? rule.compiled_selector() : rule.payload->compiled_pseudo_origin;
if (compiled.compounds.empty()
|| compiled.compiled_compounds.size() != compiled.compounds.size()
|| rule.payload->invalidation.size() != compiled.compounds.size()) {
routed_rules.push_back(index);
continue;
}
auto has_relevant_dependency = false;
auto subject_only = true;
for (size_t component_index = 0U;
component_index < rule.payload->invalidation.size();
++component_index) {
const auto targets = dependency_scope(
rule.payload->invalidation[component_index]);
if (targets.scope == 0U) continue;
has_relevant_dependency = true;
if (component_index + 1U != compiled.compounds.size()
|| targets.scope != css::invalidation_subject
|| !targets.routes.empty()) {
subject_only = false;
break;
}
}
if (!direct_subject_fast_path
|| !has_relevant_dependency || !subject_only) {
routed_rules.push_back(index);
continue;
}
++direct_subject_rules;
add_request(&subject, rule_change_propagates_to_descendants(rule));
add_custom_property_consumers(subject, rule);
}
struct trigger_transition final {
uint8_t scope{0};
std::vector<dom_node*> anchors;
std::vector<bool> current;
std::vector<bool> previous;
};
std::unordered_map<const compiled_css_compound*, trigger_transition> transitions;
transitions.reserve(changed_rules.size());
for (const auto index : changed_rules) {
transitions.reserve(routed_rules.size());
for (const auto index : routed_rules) {
if (index >= css_rules.size()) continue;
const auto& rule = css_rules[index];
const auto& compiled = rule.payload->compiled_pseudo_origin.compounds.empty()
Expand Down Expand Up @@ -2359,7 +2412,7 @@
matching_scope.reset();
}
std::vector<size_t> fallback_rules;
for (const auto index : changed_rules) {
for (const auto index : routed_rules) {
if (index >= css_rules.size()) continue;
const auto& rule = css_rules[index];
const auto& compiled = rule.payload->compiled_pseudo_origin.compounds.empty()
Expand Down Expand Up @@ -2475,6 +2528,7 @@
});
std::cerr << "WebScene selector transition: plan-ms=" << planned
<< ", apply-ms=" << applied << ", rules=" << changed_rules.size()
<< ", direct=" << direct_subject_rules
<< ", triggers=" << transitions.size()
<< ", fallback=" << fallback_rules.size()
<< ", requests=" << requests.size()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1077,6 +1077,111 @@ void test_relative_sibling_has_scaling()
#endif
}

void test_direct_subject_selector_invalidation_scaling()
{
constexpr auto affected_count = 64;
constexpr auto relevant_rule_count = 512;
#if defined(WEBSCENE_NATIVE_ENGINE_CERTIFICATION)
std::vector<std::array<uint64_t, 4>> observations;
#endif
for (const auto unrelated_count : {32, 1024}) {
auto* engine = webscene_engine_create(0);
require(engine != nullptr, "direct-subject invalidation engine creation failed");
resize(engine, 800, 600, 1);
wait_for_consumed_inputs(engine, 1, "direct-subject invalidation viewport not applied");
auto source = std::string{R"JS((() => {
const style = document.createElement('style');
style.textContent = '.direct-subject { display:block; width:11px }' +
Array.from({length:RELEVANT}, (_, index) =>
`.direct-subject.active { z-index:${index} }`).join('') +
'.direct-subject.active { width:41px }' +
Array.from({length:UNRELATED}, (_, index) =>
`.unrelated-${index}.enabled { height:${index + 1}px }`).join('');
document.body.innerHTML = '<main>' +
'<div class="direct-subject"></div>'.repeat(AFFECTED) + '</main>';
document.head.appendChild(style);
const subjects = Array.from(document.querySelectorAll('.direct-subject'));
globalThis.mutateDirectSubjects = enabled => {
for (const subject of subjects) subject.classList.toggle('active', enabled);
return subjects.every(subject =>
getComputedStyle(subject).width === (enabled ? '41px' : '11px'));
};
return [subjects.length, subjects[0] ? getComputedStyle(subjects[0]).width : null,
mutateDirectSubjects(false)];
})())JS"};
source.replace(source.find("RELEVANT"), 8, std::to_string(relevant_rule_count));
source.replace(source.find("AFFECTED"), 8, std::to_string(affected_count));
source.replace(source.find("UNRELATED"), 9, std::to_string(unrelated_count));
const auto setup = evaluate(
engine, source, "direct-subject-invalidation-setup.js");
require(setup == "[64,\"11px\",true]",
"direct-subject invalidation setup diverged: " + setup);
#if defined(WEBSCENE_NATIVE_ENGINE_CERTIFICATION)
const auto direct_subject_fast_path =
std::getenv("WEBSCENE_PROBE_DISABLE_DIRECT_SUBJECT_INVALIDATION")
== nullptr;
std::this_thread::sleep_for(std::chrono::milliseconds(260));
execute_and_wait_for_wrapper_retention_scene(engine,
"document.body.style.backgroundColor='rgb(1,2,3)'",
"direct-subject-invalidation-baseline.js");
const std::array<std::string_view, 4> names{
"selector-invalidation-plan-lookups",
"selector-invalidation-candidate-visits",
"selector-invalidation-fallback-visits",
"css-cascade-applications"};
std::array<uint64_t, 4> before{};
for (size_t index = 0; index < names.size(); ++index)
before[index] = runtime_counter_diagnostic(engine, names[index]);
#endif
const auto started = std::chrono::steady_clock::now();
require(evaluate(engine, "mutateDirectSubjects(true)",
"direct-subject-invalidation-add.js") == "true",
"direct-subject class addition produced stale computed style");
require(evaluate(engine, "mutateDirectSubjects(false)",
"direct-subject-invalidation-remove.js") == "true",
"direct-subject class removal produced stale computed style");
const auto elapsed = std::chrono::duration<double, std::milli>(
std::chrono::steady_clock::now() - started).count();
require(elapsed < 2000.0,
"direct-subject selector invalidation exceeded two seconds");
std::cout << "direct-subject-invalidation affected=" << affected_count
<< " relevant=" << relevant_rule_count
<< " unrelated=" << unrelated_count
<< " mutation-and-check-ms=" << elapsed;
#if defined(WEBSCENE_NATIVE_ENGINE_CERTIFICATION)
std::this_thread::sleep_for(std::chrono::milliseconds(260));
execute_and_wait_for_wrapper_retention_scene(engine,
"document.body.style.backgroundColor='rgb(3,2,1)'",
"direct-subject-invalidation-result.js");
std::array<uint64_t, 4> delta{};
for (size_t index = 0; index < names.size(); ++index) {
delta[index] = runtime_counter_diagnostic(engine, names[index])
- before[index];
std::cout << ' ' << names[index] << '=' << delta[index];
}
if (direct_subject_fast_path) {
require(delta[0] == 0U && delta[1] == 0U && delta[2] == 0U,
"subject-only class changes entered routed selector planning");
} else {
require(delta[0]
>= static_cast<uint64_t>(
affected_count * 2 * relevant_rule_count),
"disabled direct-subject fast path did not exercise routed planning");
}
require(delta[3] >= static_cast<uint64_t>(affected_count * 2)
&& delta[3] <= static_cast<uint64_t>(affected_count * 2 + 8),
"subject-only class changes cascaded outside the changed subjects");
observations.push_back(delta);
#endif
std::cout << '\n';
webscene_engine_destroy(engine);
}
#if defined(WEBSCENE_NATIVE_ENGINE_CERTIFICATION)
require(observations[0] == observations[1],
"unrelated rules amplified direct-subject selector invalidation work");
#endif
}

void test_compiled_css_invalidation_scaling()
{
test_nested_functional_selector_scaling();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,12 @@ int main()
test_attribute_invalidation_scopes_subject_and_descendant_rules();
return 0;
}
if (selected == "direct-subject-invalidation") {
test_direct_subject_selector_invalidation_scaling();
return 0;
}
if (selected == "css-invalidation-scaling") {
test_direct_subject_selector_invalidation_scaling();
test_compiled_subject_index_scaling();
test_cascade_layer_mutation_scaling();
test_variadic_child_vector_scaling();
Expand Down Expand Up @@ -1422,6 +1427,7 @@ int main()
test_live_form_state_selectors_and_scaling();
test_compiled_subject_index_scaling();
test_cascade_layer_mutation_scaling();
test_direct_subject_selector_invalidation_scaling();
test_compiled_css_invalidation_scaling();
test_character_data_stable_style_scaling();
test_text_topology_css_work_scaling();
Expand Down
Loading