Skip to content

perf: store example DB as fixtures, 850x faster export#651

Merged
GiggleLiu merged 8 commits intomainfrom
fixture-based-example-db
Mar 15, 2026
Merged

perf: store example DB as fixtures, 850x faster export#651
GiggleLiu merged 8 commits intomainfrom
fixture-based-example-db

Conversation

@GiggleLiu
Copy link
Copy Markdown
Contributor

Summary

  • export_examples took ~34s in debug mode because every build ran BruteForce on 72 examples
  • Pre-computed results are now embedded as JSON fixtures via include_str!, reducing export to ~0.04s (850x speedup)
  • compute_*_db() functions preserved for fixture regeneration and verification tests

Changes

  • Split build_*_db() (fast fixture load) from compute_*_db() (BruteForce/ILP computation)
  • Added src/example_db/fixtures/{models,rules}.json — pre-computed expected results
  • Added examples/regenerate_fixtures.rs binary for updating fixtures when models/rules change
  • Added verify_model_fixtures_match_computed and verify_rule_fixtures_match_computed tests
  • Added make regenerate-fixtures Makefile target

Test plan

  • All 2144 tests pass (cargo test --features "ilp-highs example-db")
  • export_examples runs in 0.04s (was 34s)
  • Verification tests confirm fixtures match freshly computed results
  • make regenerate-fixtures works correctly

🤖 Generated with Claude Code

export_examples took ~34s in debug mode because every build ran
BruteForce on 72 examples. Now pre-computed results are embedded
as JSON fixtures via include_str!, reducing export to ~0.04s.

- Split build_*_db() (fixture load, fast) from compute_*_db() (BruteForce)
- Add fixture files: src/example_db/fixtures/{models,rules}.json
- Add regenerate_fixtures binary for updating fixtures when code changes
- Add verification tests comparing fixtures against freshly computed results
- Add `make regenerate-fixtures` target

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@GiggleLiu GiggleLiu requested a review from Copilot March 15, 2026 12:52
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 97.59926% with 26 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.88%. Comparing base (f81e52f) to head (9dcd95b).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
src/unit_tests/example_db.rs 97.23% 9 Missing ⚠️
src/example_db/mod.rs 78.57% 6 Missing ⚠️
src/rules/test_helpers.rs 97.39% 6 Missing ⚠️
src/example_db/specs.rs 96.19% 4 Missing ⚠️
src/export.rs 96.29% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main     #651    +/-   ##
========================================
  Coverage   96.88%   96.88%            
========================================
  Files         268      269     +1     
  Lines       35563    36036   +473     
========================================
+ Hits        34456    34915   +459     
- Misses       1107     1121    +14     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR speeds up example DB usage by embedding precomputed model/rule example databases as compile-time JSON fixtures, while keeping the original (slow) computation path for regeneration and verification.

Changes:

  • Load example DBs from embedded include_str! fixtures (build_*_db) and keep recomputation via builders (compute_*_db).
  • Add fixture regeneration example binary and fixture verification tests.
  • Update Makefile/Cargo examples to support fixture regeneration and faster example export.

Reviewed changes

Copilot reviewed 6 out of 7 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
src/example_db/mod.rs Splits “build from fixtures” vs “compute from builders” and updates public API accordingly.
src/example_db/fixtures/models.json Adds embedded precomputed model example DB fixture.
src/example_db/fixtures/rules.json Adds embedded precomputed rule example DB fixture.
src/unit_tests/example_db.rs Adds tests to verify fixtures against freshly computed results.
examples/regenerate_fixtures.rs Adds a binary to recompute and write fixture JSON files.
Makefile Adds targets/help text for fixture regeneration and updates example export commands.
Cargo.toml Registers the new regenerate_fixtures example.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread Makefile Outdated
Comment on lines 127 to 130
# Regenerate example DB fixtures from code (runs BruteForce/ILP — slow)
regenerate-fixtures:
cargo run --release --features "example-db" --example regenerate_fixtures

Comment thread examples/regenerate_fixtures.rs Outdated
Comment on lines +24 to +28
let models_json = serde_json::to_string(&model_db).expect("Failed to serialize models");
let rules_json = serde_json::to_string(&rule_db).expect("Failed to serialize rules");

fs::write(&models_path, &models_json).expect("Failed to write models fixture");
fs::write(&rules_path, &rules_json).expect("Failed to write rules fixture");
Comment thread src/example_db/mod.rs Outdated
Comment on lines +66 to +71
pub fn build_model_db() -> Result<ModelDb> {
static MODELS_JSON: &str = include_str!("fixtures/models.json");
let db: ModelDb =
serde_json::from_str(MODELS_JSON).expect("embedded models fixture should parse");
validate_model_uniqueness(&db.models)?;
Ok(db)
Comment thread src/example_db/mod.rs Outdated
Comment on lines +75 to +80
pub fn build_rule_db() -> Result<RuleDb> {
let mut rules = rule_builders::build_rule_examples();
rules.sort_by_key(rule_key);
validate_rule_uniqueness(&rules)?;
Ok(RuleDb {
version: EXAMPLE_DB_VERSION,
rules,
})
static RULES_JSON: &str = include_str!("fixtures/rules.json");
let db: RuleDb =
serde_json::from_str(RULES_JSON).expect("embedded rules fixture should parse");
validate_rule_uniqueness(&db.rules)?;
Ok(db)
Comment on lines +271 to +278
for (loaded_model, computed_model) in loaded.models.iter().zip(computed.models.iter()) {
assert_eq!(
loaded_model, computed_model,
"model fixture mismatch for {} {:?} — regenerate fixtures with: \
cargo run --release --example regenerate_fixtures --features example-db",
loaded_model.problem, loaded_model.variant
);
}
Comment thread src/unit_tests/example_db.rs Outdated
Comment on lines +304 to +323
for (loaded_rule, computed_rule) in loaded.rules.iter().zip(computed.rules.iter()) {
assert_eq!(
loaded_rule.solutions.len(),
computed_rule.solutions.len(),
"solution count mismatch for {} -> {} — regenerate fixtures",
loaded_rule.source.problem, loaded_rule.target.problem
);
// Overhead formulas may differ between debug/release due to
// floating-point path-cost differences in path-based examples.
// Just verify the same set of overhead field names exist.
let loaded_fields: BTreeSet<_> =
loaded_rule.overhead.iter().map(|o| &o.field).collect();
let computed_fields: BTreeSet<_> =
computed_rule.overhead.iter().map(|o| &o.field).collect();
assert_eq!(
loaded_fields, computed_fields,
"overhead fields mismatch for {} -> {} — regenerate fixtures",
loaded_rule.source.problem, loaded_rule.target.problem
);
}
Comment thread Makefile
@echo " examples - Generate example JSON for paper"
@echo " examples - Generate example JSON for paper (from fixtures, fast)"
@echo " regenerate-fixtures - Recompute example DB fixtures (BruteForce/ILP, slow)"
@echo " regenerate-fixtures - Recompute example DB fixtures (BruteForce/ILP, slow)"
GiggleLiu and others added 7 commits March 15, 2026 23:01
- Add negation rule in .gitignore so src/example_db/fixtures/*.json is
  tracked (fixes CI build failure: include_str! could not find the file)
- Replace expect() with proper ProblemError::SerializationError in
  build_example_db() fixture parsing
- Cache computed rule DB in tests via OnceLock to avoid redundant work

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@GiggleLiu GiggleLiu merged commit 0474f09 into main Mar 15, 2026
5 checks passed
@GiggleLiu GiggleLiu deleted the fixture-based-example-db branch April 12, 2026 00:50
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