Skip to content

perf: improve instances in AdjoinRoot#7435

Closed
ChrisHughes24 wants to merge 5 commits intomasterfrom
AdjoinRootPerfCH
Closed

perf: improve instances in AdjoinRoot#7435
ChrisHughes24 wants to merge 5 commits intomasterfrom
AdjoinRootPerfCH

Conversation

@ChrisHughes24
Copy link
Copy Markdown
Member

@ChrisHughes24 ChrisHughes24 commented Sep 29, 2023


Open in Gitpod

@ChrisHughes24 ChrisHughes24 added WIP Work in progress awaiting-author A reviewer has asked the author a question or requested changes. labels Sep 29, 2023
@ChrisHughes24
Copy link
Copy Markdown
Member Author

!bench

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 6b14acb.
There were significant changes against commit 042888f:

  Benchmark                     Metric         Change
  ===================================================
+ ~Mathlib.FieldTheory.Normal   instructions    -7.1%

@ChrisHughes24 ChrisHughes24 added blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) and removed awaiting-author A reviewer has asked the author a question or requested changes. labels Sep 30, 2023
@ChrisHughes24 ChrisHughes24 added awaiting-review and removed WIP Work in progress blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) labels Oct 4, 2023
@ghost ghost added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 4, 2023
@ChrisHughes24 ChrisHughes24 removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 4, 2023
Copy link
Copy Markdown
Contributor

@mattrobball mattrobball left a comment

Choose a reason for hiding this comment

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

These seem to be upstream issues. Fixing those would probably improve things more broadly.

instance [CommSemiring S] [Algebra S R] : Algebra S (AdjoinRoot f) :=
Ideal.Quotient.algebra S
--TODO: add reference to library note in PR #7432
{ Ideal.Quotient.algebra S with
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The underlying problem seems to be that Ideal.Quotient.algebra buries SMul under RingHom.comp which (I'm guessing) is more expensive to unify if you just want the smul.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This statement is not correct. The two signatures are

@Algebra.mk S (AdjoinRoot f) inst_1 CommSemiring.toSemiring (Submodule.Quotient.instSMul' (span {f})) Algebra.toRingHom  _ _ : Algebra S (AdjoinRoot f)

and, previously,

@Quotient.algebra S R[X] inst_1 commRing algebraOfAlgebra (span {f}) : Algebra S (R[X] ⧸ span {f})

You have to unfold algebraOfAlgebra to get access SMul previously and no longer have to do that.

But Algebra.toRingHom buries RingHom under another layer with change. It might be good to include

  toRingHom := RingHom.comp _ _ 

which avoids this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't see why this would help because Algebra is the only class that has a toRingHom field. It may help, I guess if people are really exploiting this def-eq everywhere, but that would be bad style anyway.

instance [DistribSMul S R] [IsScalarTower S R R] : DistribSMul S (AdjoinRoot f) :=
Submodule.Quotient.distribSMul' _
--TODO: add reference to library note in PR #7432
{ Submodule.Quotient.distribSMul' _ with
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

distribSMul' uses Function.Surjective which is probably the underlying problem

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I don't think so. For example, the change in #6803 to the group instance made a difference even though the underlying instance on Con was defined nicely. Maybe defining them nicely all the way back also makes a difference.

Copy link
Copy Markdown
Contributor

@mattrobball mattrobball left a comment

Choose a reason for hiding this comment

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

See if #7459 helps with the DistribMul instance.

instance [CommSemiring S] [Algebra S R] : Algebra S (AdjoinRoot f) :=
Ideal.Quotient.algebra S
--TODO: add reference to library note in PR #7432
{ Ideal.Quotient.algebra S with
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This statement is not correct. The two signatures are

@Algebra.mk S (AdjoinRoot f) inst_1 CommSemiring.toSemiring (Submodule.Quotient.instSMul' (span {f})) Algebra.toRingHom  _ _ : Algebra S (AdjoinRoot f)

and, previously,

@Quotient.algebra S R[X] inst_1 commRing algebraOfAlgebra (span {f}) : Algebra S (R[X] ⧸ span {f})

You have to unfold algebraOfAlgebra to get access SMul previously and no longer have to do that.

But Algebra.toRingHom buries RingHom under another layer with change. It might be good to include

  toRingHom := RingHom.comp _ _ 

which avoids this.

@ChrisHughes24
Copy link
Copy Markdown
Member Author

!bench

@leanprover-bot
Copy link
Copy Markdown
Collaborator

Here are the benchmark results for commit 907cc35.
There were no significant changes against commit 37bce19.

@mattrobball
Copy link
Copy Markdown
Contributor

FieldTheory.Normal is still -2%

@ChrisHughes24 ChrisHughes24 added awaiting-author A reviewer has asked the author a question or requested changes. and removed awaiting-review labels Oct 25, 2023
@ghost ghost added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Nov 1, 2023
@YaelDillies YaelDillies deleted the AdjoinRootPerfCH branch August 12, 2025 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-author A reviewer has asked the author a question or requested changes. merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants