Skip to content

Faster hashing of periods that hold a time structure - #104

Merged
hellemo merged 2 commits into
mainfrom
hash-periods
Oct 16, 2026
Merged

hellemo merged 2 commits into
mainfrom
hash-periods

Conversation

@hellemo

@hellemo hellemo commented Oct 15, 2026

Copy link
Copy Markdown
Member

I'm doing a bit of performance work on building models using TimeStruct, and the repeated hashing on time periods can reduce the model building time up to 7% in some cases if we improve the hashing.

Periods are often dictionary keys (e.g. in JuMP containers indexed by periods).
Non-bits periods such as StrategicPeriod hashed through the generic object hash;
they now hash their identifying fields (numbers, dates, nested periods), leaving
out time structures, profiles and fields of type Any.  Bits periods keep the
default hash.  Equality is unchanged and the hashes are consistent with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hellemo
hellemo requested review from JulStraus and trulsf October 15, 2026 20:53
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
src/TimeStruct.jl 100.00% <ø> (ø)
src/hashing.jl 100.00% <100.00%> (ø)
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@JulStraus JulStraus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I have never worked with the hashes myself so far. So I do not know if what i write here does make sense. However, I have a few questions:

  1. Couldn't a problem occur with us neglecting the internal time structure in hash comparison? You mention it in the test (l. 2762 ff., hashes are equal, but types) but nowhere else. Wouldn't a change to the hashing not then lead to breaking changes? How is the indexing working here in JuMP, based on hash or instance?

  2. Is l.25,

    ex = :(hash($(QuoteNode(nameof(x))), h))

    not adding the hash of the specific type as well?

  3. Did you see speed advantages when you tested it?

@trulsf trulsf left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me.

Regarding 1., I think hashes are only used to find the dictionary bucket and then equality is used for precise lookup. If we had a Dictionary indexed on StrategicPeriod and had elements from a multitude of different time structures, it could be a bit inefficient. But as we usually have indices from the same time structure it should give efficiency gains.

@JulStraus JulStraus left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM based on the explanation

@hellemo
hellemo merged commit aba4115 into main Oct 16, 2026
6 checks passed
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