Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve include time of Mojo::Base by extracting monkey_patch #2173

Merged
merged 1 commit into from
May 13, 2024

Conversation

okurz
Copy link
Contributor

@okurz okurz commented Apr 5, 2024

Motivation

Commit 7e9a2ad introduced a "require Mojo::Util" causing a significant chain of further dependencies being pulled in which IMHO should be avoided for the very base module which is in particular being advertised as useful for just enabling static code checks and common import checks.

Summary

This commit moves out the function "monkey_patch" into its own module to break or prevent the circular dependency between Mojo::Base and Mojo::Util.

With that the import of Mojo::Base is more efficient, time perl -e 'use Mojo::Base
on my system reduced from 224±12.08 ms to 52.0±2.3 ms which I consider a considerable improvement for Mojo::Base which is used as a baseclass in many cases.

lib/Mojo/Util.pm Outdated Show resolved Hide resolved
lib/Mojo/Base.pm Outdated Show resolved Hide resolved
@okurz okurz force-pushed the feature/optimize_mojo_base branch from c408463 to 949dbaa Compare April 5, 2024 19:44
@okurz

This comment was marked as resolved.

@Grinnz
Copy link
Contributor

Grinnz commented Apr 5, 2024

  • Addressed CI failure "Undefined subroutine &Mojo::Util::class_to_path" by moving class_to_path to Mojo::Base and adjusting the rest of the repository accordingly, re-exporting from Mojo::Util

This should not be done. This creates a method in all classes using Mojo::Base as a base.

@Grinnz
Copy link
Contributor

Grinnz commented Apr 5, 2024

Additionally, if Mojo::Util re-exports class_to_path no other adjustment should be necessary.

@okurz

This comment was marked as resolved.

@okurz
Copy link
Contributor Author

okurz commented Apr 5, 2024

  • Addressed CI failure "Undefined subroutine &Mojo::Util::class_to_path" by moving class_to_path to Mojo::Base and adjusting the rest of the repository accordingly, re-exporting from Mojo::Util

This should not be done. This creates a method in all classes using Mojo::Base as a base.

done, updated

@Grinnz
Copy link
Contributor

Grinnz commented Apr 5, 2024

BaseUtil seems reasonable and self-explanatory to me, but there may be other opinions. As long as Mojo::Util imports and re-exports the functions from there correctly, there should be no changes needed to other modules.

@okurz
Copy link
Contributor Author

okurz commented Apr 5, 2024

BaseUtil seems reasonable and self-explanatory to me, but there may be other opinions. As long as Mojo::Util imports and re-exports the functions from there correctly, there should be no changes needed to other modules.

Sure, let's see how that looks. Added a commit to combine monkey_patch and class_to_path in Mojo::BaseUtil and reverted changes in other modules to use still the functions as exported from Mojo::Util.

lib/Mojo/BaseUtil.pm Outdated Show resolved Hide resolved
lib/Mojo/BaseUtil.pm Outdated Show resolved Hide resolved
@Grinnz
Copy link
Contributor

Grinnz commented Apr 5, 2024

IMO, t/mojo/util.t should remain unchanged, to ensure there is no change in compatibility for the re-exported functions. t/mojo/base_util.t may be unnecessary if so.

@kraih
Copy link
Member

kraih commented Apr 5, 2024

I'd be interested in some real world benchmarks where this makes a difference.

@poti1
Copy link

poti1 commented May 7, 2024

I'm looking forwards to seeing this hopefully soon in the library :)

@poti1
Copy link

poti1 commented May 7, 2024

By simply copying over monkey_patch and the set_subname declarations from Mojo::Util to my cli tool (and removing use/require Mojo::Util), I saw these results in performance improvement:

BEFORE:
real 0m0.179s
user 0m0.101s
sys 0m0.052s

NOW:
real 0m0.045s
user 0m0.018s
sys 0m0.015s

When running on my phone's termux, the change of those values is really felt.

I compared before and after also with Nut Prof:

BEFORE:
Profile of -e for 198ms (of 205ms), executing 15207 statements and 6061 subroutine calls in 76 source files and 27 string evals.

AFTER:
Profile of -e for 11.7ms (of 11.8ms), executing 144 statements and 57 subroutine calls in 8 source files.

lib/Mojo/BaseUtil.pm Outdated Show resolved Hide resolved
@okurz okurz force-pushed the feature/optimize_mojo_base branch from 1942558 to a8ba9a5 Compare May 8, 2024 10:10
@okurz

This comment was marked as outdated.

@kraih kraih requested review from a team, marcusramberg, kraih, jhthorsen and jberger May 8, 2024 10:27
@okurz okurz force-pushed the feature/optimize_mojo_base branch from a8ba9a5 to b7002c7 Compare May 8, 2024 14:12
@okurz

This comment was marked as outdated.

@kraih
Copy link
Member

kraih commented May 8, 2024

Please squash commits.

@okurz okurz force-pushed the feature/optimize_mojo_base branch from b7002c7 to 597c50b Compare May 8, 2024 17:20
t/pod_coverage.t Outdated Show resolved Hide resolved
@okurz okurz force-pushed the feature/optimize_mojo_base branch 3 times, most recently from 70f54e2 to 9299d31 Compare May 10, 2024 07:32
lib/Mojo/BaseUtil.pm Outdated Show resolved Hide resolved
lib/Mojo/BaseUtil.pm Outdated Show resolved Hide resolved
@okurz okurz force-pushed the feature/optimize_mojo_base branch from 9299d31 to 760a311 Compare May 10, 2024 16:10
lib/Mojo/BaseUtil.pm Outdated Show resolved Hide resolved
@okurz okurz force-pushed the feature/optimize_mojo_base branch from 760a311 to bfe6eb0 Compare May 10, 2024 17:34
@eduardoj
Copy link

Small typo in commit message and in pull request description: strictures shold be structures.

@okurz okurz force-pushed the feature/optimize_mojo_base branch from bfe6eb0 to f670d4f Compare May 10, 2024 18:05
@okurz
Copy link
Contributor Author

okurz commented May 10, 2024

Small typo in commit message and in pull request description: strictures shold be structures.

uh, no. I meant strictures as from https://metacpan.org/pod/strictures but maybe that word only makes sense when using that pod. So rephrased.

Grinnz
Grinnz previously approved these changes May 10, 2024
Copy link
Contributor

@Grinnz Grinnz left a comment

Choose a reason for hiding this comment

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

LGTM. don't know why perltidy test is failing

@okurz okurz force-pushed the feature/optimize_mojo_base branch from f670d4f to b27e4a4 Compare May 10, 2024 19:42
@mergify mergify bot dismissed Grinnz’s stale review May 10, 2024 19:43

Pull request has been modified.

@kraih
Copy link
Member

kraih commented May 11, 2024

LGTM. don't know why perltidy test is failing

New perltidy release. Just rebase the PR on main and it will be fine.

Commit 7e9a2ad introduced a "require Mojo::Util" causing a significant
chain of further dependencies being pulled in which IMHO should be
avoided for the very base module which is in particular being advertised
as useful for just enabling static code checks and common import checks.

This commit moves out the function "monkey_patch" into its own module to
break or prevent the circular dependency between Mojo::Base and
Mojo::Util.

With that the import of Mojo::Base is more efficient,
`time perl -e 'use Mojo::Base`
on my system reduced from 224±12.08 ms to 52.0±2.3 ms which I consider a
considerable improvement for Mojo::Base which is used as a baseclass in
many cases.

Further minor changes included:
* Directly require MonkeyPatch for cleaner subclassing + POD
* Correct use of MonkeyPatch with empty import
* Combine monkey_patch and class_to_path in new Mojo::BaseUtil
@okurz okurz force-pushed the feature/optimize_mojo_base branch from b27e4a4 to 9637ef0 Compare May 11, 2024 16:26
@okurz
Copy link
Contributor Author

okurz commented May 11, 2024

Rebased

@Grinnz
Copy link
Contributor

Grinnz commented May 11, 2024

Thank you for persisting to tidy this up!

@kraih kraih requested review from a team, kraih, jberger and christopherraa May 11, 2024 19:41
@mergify mergify bot merged commit 9c05718 into mojolicious:main May 13, 2024
10 checks passed
@okurz okurz deleted the feature/optimize_mojo_base branch May 13, 2024 14:36
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.

9 participants