Skip to content

Use IndexMap rather than BTreeMap for commodity prices#1040

Merged
alexdewar merged 1 commit intomainfrom
use-indexmap-for-prices
Dec 19, 2025
Merged

Use IndexMap rather than BTreeMap for commodity prices#1040
alexdewar merged 1 commit intomainfrom
use-indexmap-for-prices

Conversation

@alexdewar
Copy link
Copy Markdown
Member

Description

We currently store commodity prices in a BTreeMap. Everywhere else in the code where we need an ordered map, we use IndexMap. While both give consistent ordering when you iterate over them, an IndexMap iterates over items in the same order as they were added (like Python dicts) and a BTreeMap is sorted by the keys.

When I changed commodity prices to use an ordered map in #601, I opted for a BTreeMap on the grounds that we wouldn't always be adding commodities to the map in the same order and having a consistent ordering in different years seemed like a nice property to have. It was a pretty arbitrary choice though. The downside of ordering by keys is that it also sorts by time slice alphabetically, so e.g. winter.afternoon comes before winter.morning. So: commodities possibly ordered slightly better and time slices ordered worse. It doesn't seem worth it.

It doesn't particularly matter, but there aren't really any upsides to using a BTreeMap, so I've changed it to an IndexMap for consistency with the rest of the code. IndexMaps are faster too, although I imagine the performance improvement will be neglible in our case.

This has obviously changed the ordering of the commodity_prices.csv output files, but there shouldn't be any other functional changes.

Type of change

  • Bug fix (non-breaking change to fix an issue)
  • New feature (non-breaking change to add functionality)
  • Refactoring (non-breaking, non-functional change to improve maintainability)
  • Optimization (non-breaking change to speed up the code)
  • Breaking change (whatever its nature)
  • Documentation (improve or add documentation)

Key checklist

  • All tests pass: $ cargo test
  • The documentation builds and looks OK: $ cargo doc

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 19, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 80.72%. Comparing base (b55c6c1) to head (4f58710).
⚠️ Report is 69 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1040   +/-   ##
=======================================
  Coverage   80.71%   80.72%           
=======================================
  Files          52       52           
  Lines        6924     6926    +2     
  Branches     6924     6926    +2     
=======================================
+ Hits         5589     5591    +2     
  Misses       1063     1063           
  Partials      272      272           

☔ 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.

@alexdewar alexdewar marked this pull request as ready for review December 19, 2025 10:26
Copy link
Copy Markdown
Contributor

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 replaces BTreeMap with IndexMap for storing commodity prices to maintain consistency with the rest of the codebase and provide insertion-order iteration instead of alphabetically sorted iteration. The change affects only the internal data structure of the CommodityPrices struct without altering the API or functionality.

Key Changes

  • Changed CommodityPrices internal storage from BTreeMap to IndexMap
  • Updated all related type signatures for iterators
  • Updated imports to use indexmap::IndexMap instead of std::collections::BTreeMap

Reviewed changes

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

File Description
src/simulation/prices.rs Core implementation - replaced BTreeMap with IndexMap, updated imports and iterator type signatures
tests/data/*/commodity_prices.csv Test output files - reordered rows to reflect insertion order instead of alphabetical sorting
tests/data/simple/debug_appraisal_results.csv Test output file - minor floating-point precision differences in some values

The code changes are clean and well-contained. All necessary type signatures have been updated consistently, and the test data changes reflect the expected reordering from alphabetical (BTreeMap) to insertion order (IndexMap).


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

Copy link
Copy Markdown
Collaborator

@tsmbland tsmbland left a comment

Choose a reason for hiding this comment

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

Seems fine to me. I was never really sure why we were using BTreeMap to begin with.

The only downside I can think of is that, since we add prices in batches according to pricing strategy, changing the pricing strategy of one or more commodities will end up changing the order of prices in the output file. But I don't think anyone will care too much about that

Copy link
Copy Markdown
Collaborator

@dalonsoa dalonsoa left a comment

Choose a reason for hiding this comment

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

LGTM, and the explanation makes sense.

@alexdewar alexdewar merged commit bae744e into main Dec 19, 2025
14 checks passed
@alexdewar alexdewar deleted the use-indexmap-for-prices branch December 19, 2025 16:36
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.

4 participants