Skip to content

Refactoring of the parsing of simple units, moving short-circuit up#17012

Closed
mhvk wants to merge 4 commits intoastropy:mainfrom
mhvk:units-simple-unit-refactoring
Closed

Refactoring of the parsing of simple units, moving short-circuit up#17012
mhvk wants to merge 4 commits intoastropy:mainfrom
mhvk:units-simple-unit-refactoring

Conversation

@mhvk
Copy link
Copy Markdown
Contributor

@mhvk mhvk commented Sep 14, 2024

This is an alternative to #17004 that does not rely on private methods, and removes the short-circuiting from the unit parser. It builds on #17011, since removing the short-circuit from the unit parser brought out quite a few bugs. (EDIT: #17011 is merged, so only 3 commits left.) But it turns out that for FITS at least, some short-circuiting has to be done, since it weirdly defines a unit "deg C", which would in normal parsing become "degrees Coulomb" (and which we really do not want in a composite unit!).

Note that while the last commit is labelled performance, this is only relative to the case where the short circuit is not present at all. Relative to main, performance benefits are modest.

  • By checking this box, the PR author has requested that maintainers do NOT use the "Squash and Merge" button. Maintainers should respect this when possible; however, the final decision is at the discretion of the maintainer that merges the PR.

@mhvk mhvk added this to the v7.0.0 milestone Sep 14, 2024
@mhvk mhvk requested a review from eerovaher September 14, 2024 21:39
@github-actions github-actions bot added the units label Sep 14, 2024
@github-actions
Copy link
Copy Markdown
Contributor

Thank you for your contribution to Astropy! 🌌 This checklist is meant to remind the package maintainers who will review this pull request of some common things to look for.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see instructions for rebase and squash.
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the "Extra CI" label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the "no-changelog-entry-needed" label. If this is a manual backport, use the "skip-changelog-checks" label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate "backport-X.Y.x" label(s) before merge.

@mhvk mhvk added the benchmark Run benchmarks for a PR label Sep 15, 2024
@mhvk
Copy link
Copy Markdown
Contributor Author

mhvk commented Sep 15, 2024

Benchmarks are a bit weird: simple units are faster, as expected, but quantity initialization has slowed down? Will try to rerun, especially given the large variance, but for the record:

| Change   | Before [b266a404]    | After [b02b73a0]    |   Ratio | Benchmark (Parameter)             |
|----------|----------------------|---------------------|---------|-----------------------------------|
| -        | 2.46±0.01μs          | 1.85±0.01μs         |    0.75 | units.time_very_simple_unit_parse |
| -        | 123±70μs             | 48.6±0.2μs          |    0.39 | units.time_quantity_times_unit    |
| Change   | Before [b266a404]    | After [b02b73a0]    |   Ratio | Benchmark (Parameter)                |
|----------|----------------------|---------------------|---------|--------------------------------------|
| +        | 38.6±0.2μs           | 126±90μs            |    3.25 | units.time_quantity_creation         |
| +        | 97.9±1μs             | 234±100μs           |    2.39 | units.time_quantity_init_array       |
| +        | 168±1μs              | 309±100μs           |    1.84 | units.time_quantity_array_conversion |

EDIT: OK, the rerun of the benchmarks only shows the expected improvement (plus a still flaky run on main):

| Change   | Before [b266a404]    | After [b02b73a0]    |   Ratio | Benchmark (Parameter)             |
|----------|----------------------|---------------------|---------|-----------------------------------|
| -        | 2.46±0μs             | 1.89±0.02μs         |    0.77 | units.time_very_simple_unit_parse |
| -        | 139±80μs             | 47.5±0.1μs          |    0.34 | units.time_quantity_times_unit    |

@mhvk mhvk force-pushed the units-simple-unit-refactoring branch from 0cf509c to 7cba61b Compare September 29, 2024 23:14
Copy link
Copy Markdown
Member

@eerovaher eerovaher left a comment

Choose a reason for hiding this comment

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

This is an alternative to #17004 that does not rely on private methods

The difference between public and private API is that in private API we are allowed to make breaking changes whenever we want. This means downstream developers should avoid using our private API, but it does not mean that we ourselves should be forbidden from using it. I don't think using private API in #17004 is a problem, especially given that it is using private API from units.

But it turns out that for FITS at least, some short-circuiting has to be done...

Special cases like FITS in this pull request should be avoided if possible because they make the code more difficult to understand and to maintain. There are no such special cases in #17004.

@mhvk
Copy link
Copy Markdown
Contributor Author

mhvk commented Sep 30, 2024

Hmm, looking at your PR again, I think it is really equally simple (after the other changes made), and does cover the FITS case. So, let me close this one in favour of #17004.

@mhvk mhvk closed this Sep 30, 2024
@mhvk mhvk deleted the units-simple-unit-refactoring branch February 17, 2025 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants