Repository navigation
refactor(cost): one OutputFacts per node, one costing per Stage 3 execution - #1520
Merged
Merged
Conversation
…odeResourceUsageAndCost
…ache and LCM helpers
…sult, reuse cost_reading in at_rate
zzylol
force-pushed
the
refactor/cost-one-node-usage-shape
branch
from
November 26, 2026 04:23
be5f7f2 to
b398c43
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
NodeResourceUsageAndCostrestated 14 fields ofProducerOutput, so each new output fact went in three places. Stage 3 also calledcost_nodea second time per execution, with the same arguments, only to read two byte counts. The exact and rounded raw-row closures, the answer-cache amount, and the LCM were each written more than once.OutputFactsstruct now sits in bothProducerOutputandNodeResourceUsageAndCost. Stage 3'scost_readingreturns the kept-result and output bytes with the quantity, soat_ratecosts each execution once.raw_rows_backis now the exact count rounded.answers_kept(returningAnswersKept) andleast_common_multipleare shared. The result-cache and answer-cache settings are read fromSelectionInputsinstead of being copied intoExecutionCosts. EachResidentReadcarries the one window count its quantity charges.node_resource_usage_and_costcall per execution instead of two.Review guide
main. Not stacked.crates/stage3-plan-selection/src/cost.rs(QuantityAtRate,cost_reading, theat_ratemap, and the kept-bytes block that replaces the second costing loop). Thencrates/cost/src/node/usage/mod.rs(OutputFacts,producer_output, and theraw_rows_exact/raw_rows_backclosures).NodeMemoryAndDisk::disk_read_bytes_per_secondis reworded. It said the field was 0 "until reads of windows not in the window cache are costed ([Design] Stored views' disk and reads: describe the demo's storage, add file slack, cache bound and cold reads to the existing cost terms #1469)". That has since landed, so it now says the node alone has 0 andplan_costadds the windows a cached view's readers read while they are not in the window cache.x.output/x.sources/ … access on a producer or usage becomesx.output_facts.…, andoutputinside it is nowstatistics(usage.output_facts.statistics.bytes). That coversusage/mod.rs,plan/cost.rs, and Stage 3'slogical_output. The 31ProducerOutput { .. }literals inusage/tests.rswere nested by a script. Inmilp/model/tests.rs, 18SelectionInputsliterals gainkept_query_time_results: None, answer_cache: None.raw_rows_back = round(raw_rows_back_exact)must equal the oldmax(min(round(rate·t), held), 1). It does:heldis an integer androundis monotone, soround(max(min(x, h), 1)) = max(min(round x, h), 1)forx ≥ 0.per_resident_windowis nowper_kept_byte × bytes_per_window. That is the same product, and both areNonefor the same quantities.milp/lightly. This was needed because the settings andresident_at_mostwere read there:milp/kept_results.rs,dominance.rs,upper_bound.rsandretention.rschange one or two lines each.milp/model/mod.rsgains two fields.milp/model/cluster/mod.rschanges one line: it readsanswer_cachefromselection, so the MILP has one path to that setting.Behavior: before → after
The same workload, e.g.
crates/integration-tests/tests/storage_costs.rswith an answer cache and kept query-time results, gives the same selected plan, per-node costs, resident windows and answer bytes before and after. Only the work done to cost it changes: before, Stage 3 costed each execution twice (at_rate, then again for the kept bytes); after, it costs each once. This serves design and maintenance: an output fact is declared once, and one costing per execution keeps the two passes from drifting.Interfaces
Files at the PR head: https://github.com/ProjectASAP/ASAPPlanner/tree/be5f7f2b615284f9c72b2d8822363982ac91359b/crates (
cost/src/node/usage/mod.rs,cost/src/plan/kept_results.rs,types/src/workload/query.rs,stage3-plan-selection/src/cost.rs).crates/cost/src/node/usage/mod.rs,crates/cost/src/lib.rsquery_time_computation_short_of_source_rowsis now private and no longer re-exported from the crate root. Its only caller is in the same file, and Stage 3's doc link now points atAnalyticalCostError::FewerSourceRowsThanItsRange. The plan report's JSON is unchanged: neither struct is serialized.crates/cost/src/plan/kept_results.rssteps_computed_per_requestis now private: its only non-test caller iscomputed_rangein the same file.crates/types/src/workload/query.rs(re-exported fromasap_types::workload)Stage 3, crate-private (
crates/stage3-plan-selection/src/{cost.rs, resident_windows.rs, milp/model/mod.rs})pub(crate) struct ExecutionCosts { /* ...the other fields are unchanged... */ - pub(crate) resident_at_most: bool, - pub(crate) kept_query_time_results: Option<KeptQueryTimeResults>, - pub(crate) answer_cache: Option<AnswerCache>, } pub(crate) struct ResidentRead { pub(crate) execution: usize, pub(crate) reader: Reader, - pub(crate) mean_windows: f64, - pub(crate) most_windows: u64, + /// The windows it keeps resident: the mean, or the most for peak memory. + pub(crate) resident_windows: f64, pub(crate) bytes_read_from_disk_per_second: f64, pub(crate) most_bytes_read_from_disk_per_run: f64, } pub(crate) fn resident_reads( physical: &PhysicalASAPMemoCandidates, selectable: &SelectableExecutions, entries: &[QueryWorkloadEntry], (cache, answer_cache): (ViewWindowCache, Option<AnswerCache>), cached: &HashMap<PhysicalCandidateId, CachedViewWindows>, - need_ms_per_edge: &[Option<u64>], - retained_ms_of_read: impl Fn(PhysicalCandidateId, u64) -> u64, + /// Each read's need and the retention it keeps (both were read from it). + costs: &ExecutionCosts, + /// Whether each read carries its most resident windows (peak memory), + /// not its mean. + most_windows: bool, ) -> Vec<ResidentRead>; pub(crate) struct SelectionInputs<'a> { pub(crate) physical: &'a PhysicalASAPMemoCandidates, pub(crate) entries: &'a [QueryWorkloadEntry], pub(crate) selectable: &'a SelectableExecutions, pub(crate) costs: &'a ExecutionCosts, + /// The data plane's result cache; `None` without one. + pub(crate) kept_query_time_results: Option<KeptQueryTimeResults>, + /// The data plane's answer cache; `None` without one. + pub(crate) answer_cache: Option<AnswerCache>, }Deleted
asap_cost::query_time_computation_short_of_source_rows(re-export; now private)usage/mod.rs.pubonkept_results::steps_computed_per_requestcomputed_range, in the same file.ExecutionCosts::{kept_query_time_results, answer_cache}SelectionInputs, the MILP's inputs, which the cluster path now also readsanswer_cachefrom.ExecutionCosts::resident_at_most,ResidentRead::{mean_windows, most_windows}resident_windows).ProducerOutput/NodeResourceUsageAndCostOutputFacts.Usage:
node_resource_usage_and_cost(node, producers, …)returnsusage. A consumer's input isusage.producer_output(&node.payload, &node.output_schema), which now clones oneOutputFacts. Stage 3'sexecution_quantitiescosts each execution once (cost_reading→QuantityAtRate { own, raw_data, per_second, kept_result_bytes, output_bytes }). The MILP then reads the cache settings fromSelectionInputs::{kept_query_time_results, answer_cache}, e.g.kept_results_quantity→answers_kept(longest_retention_ms, answers, …).held_bytes. Exercised end to end bystorage_costs::,multi_level_windows::and the planner-cli snapshot suites.Algorithm changes
None. The cost model and selection are unchanged; only duplicated computation was removed.
Decisions and open questions
OutputFacts, and the field holding it isoutput_facts, as the review suggested. Its rows-and-bytes field isstatistics(after its type,EdgeStatistics), so accesses readusage.output_facts.statistics.bytesand not…output_facts.output….Derefwas rejected because it would hide the nesting.cost_readingreturnsQuantityAtRate, a private struct, not a tuple, so its call sites name what they take andat_ratereuses it as is.resident_readskeeps amost_windows: bool. Onlyexecution_quantitiesknows the quantity being costed, and passingQuantitywould carry the same one bit. Its retention inputs are now one&ExecutionCosts, so it needs notoo_many_argumentsallow.SelectionInputscarries only the two cache settings the MILP reads, not the wholeDataPlaneExecution.plan_costand Stage 3 (review D7), and onereader_period_mshelper (part of D5). Both build or readWindowRead::Cover, which a separate cover PR is changing.lowest_mean_latency.rs's ownassert_close(T1). This belongs to the MILP cleanup PR.Verification
Run at the PR head with no snapshot regeneration; the test run changed no file in the tree (snapshots and goldens identical).
cargo fmt --all --checkcargo clippy -p asap-types -p asap-cost -p asap-stage3-plan-selection -p asap-integration-tests --all-targets -- -D warningscargo test -p asap-types -p asap-cost --libcargo test -p asap-stage3-plan-selection --libcargo test -p asap-integration-tests --test integration(all modules:storage_costs,deployment_exclusions,lowest_mean_latency,default_cost_plan_ranking,multi_level_windows, ...)cargo test -p asap-planner-cli(corpus snapshots, goldens, sketch plan snapshots,lowest_mean_latency_exactness)Follow-ups
WindowReadbuilder shared byplan_cost::window_cache_chargesand Stage 3'sresident_reads, plusreader_period_ms(review D5/D7).Readerlookups), D8, D10, D11, D13, O3, X3.