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

Inline lambdas #423

Merged
merged 1 commit into from
Mar 10, 2025
Merged

Inline lambdas #423

merged 1 commit into from
Mar 10, 2025

Conversation

byroot
Copy link
Contributor

@byroot byroot commented Mar 6, 2025

I couldn't find a reason for this pattern to call a method to get a similar proc every time.

It does incur an extra method call, and a Proc object allocation.

In addition, in the case of collection_converter, the relatively complex code can be replaced by a simpler and faster map.

Map is preferable over shifting objects into a new Array, because it allows Ruby to directly allocate an Array of the right size rather than to have to potentially resize it multiple times.

This isn't a big gain, but I think it makes the code easier to read anyway.

Before:

Calculating -------------------------------------
                alba    801.321 (± 0.6%) i/s    (1.25 ms/i) -      4.080k in   5.091760s
Calculating -------------------------------------
                alba   826.321k memsize (     0.000  retained)
                         9.908k objects (     0.000  retained)
                         6.000  strings (     0.000  retained)

After:

Calculating -------------------------------------
                alba    830.039 (± 0.8%) i/s    (1.20 ms/i) -      4.182k in   5.038669s
Calculating -------------------------------------
                alba   818.241k memsize (     0.000  retained)
                         9.807k objects (     0.000  retained)
                         6.000  strings (     0.000  retained)

Copy link

codecov bot commented Mar 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.89%. Comparing base (ad700cd) to head (a222da9).
Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #423      +/-   ##
==========================================
- Coverage   99.66%   97.89%   -1.78%     
==========================================
  Files          14       14              
  Lines         603      618      +15     
  Branches      156      160       +4     
==========================================
+ Hits          601      605       +4     
- Misses          2       13      +11     

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

@okuramasafumi
Copy link
Owner

@byroot Great work, thank you!
converter and collection_converter were meant to be "hooks" for modifying behaviors of Alba, but there should be a better way.
And I'm afraid removing methods is backward incompatible, so we should deprecate them at first and then removing them in the next major version (soon).

@byroot
Copy link
Contributor Author

byroot commented Mar 7, 2025

converter and collection_converter were meant to be "hooks" for modifying behaviors of Alba

I see, that's what I suspected, but I couldn't see any tested behavior, so I couldn't be sure. I'll see about deprecating them without losing performance.

@byroot
Copy link
Contributor Author

byroot commented Mar 7, 2025

On that note, I don't know how important it is (I'm trying to optimize alba more for curiosity), but it offer quite a few of these "hooks", with multiple ways to do the same thing, and that makes a pretty big optimization I have in mind challenging.

But I think I'll try it anyway once I get some time (probably next week).

@okuramasafumi
Copy link
Owner

okuramasafumi commented Mar 7, 2025

@byroot Right, so I'm looking for mentioning them in README with previous versions of Alba. If the mention was deleted so long ago, we can decide it's safe to remove them without deprecating (they are private methods after all).
README mentioned it a while back, so it's safe to deprecate it first.

@trevorturk
Copy link
Collaborator

I'm curious what else would be helpful to deprecate, perhaps we can soft-deprecate with a warning to see if any users are really impacted and figure out how to help them work around issues.

I'm happy to help with that effort. It's lovely for Alba to have a lot of this flexibility, but at least I've found I don't need much beyond the basics.

I'm so excited to see Alba getting this attention even if just an exercise in curiosity! Any speedups here will really help for my use case which is mostly reading and writing JSON via the newly improved JSON gem and Alba! 🙌

I couldn't find a reason for this pattern to call a method to get
a similar proc every time.

It does incur an extra method call, and a Proc object allocation.

In addition, in the case of `collection_converter`, the relatively
complex code can be replaced by a simpler and faster `map`.

Map is preferable over shifting objects into a new Array, because
it allows Ruby to directly allocate an Array of the right size
rather than to have to potentially resize it multiple times.

This isn't a big gain, but I think it makes the code easier to
read anyway.

Before:

```
Calculating -------------------------------------
                alba    801.321 (± 0.6%) i/s    (1.25 ms/i) -      4.080k in   5.091760s
Calculating -------------------------------------
                alba   826.321k memsize (     0.000  retained)
                         9.908k objects (     0.000  retained)
                         6.000  strings (     0.000  retained)
```

After:

```
Calculating -------------------------------------
                alba    830.039 (± 0.8%) i/s    (1.20 ms/i) -      4.182k in   5.038669s
Calculating -------------------------------------
                alba   818.241k memsize (     0.000  retained)
                         9.807k objects (     0.000  retained)
                         6.000  strings (     0.000  retained)
```
@byroot
Copy link
Contributor Author

byroot commented Mar 7, 2025

I pushed a new version that emits a warning if to define one of these two methods, and fallback to the old code.

If you don't though, it continues to use the fast path.

I'm curious what else would be helpful to deprecate

I'm kinda planning (no promises) to prototype a branch that disregard backward compat to see how much it would be worth.

@okuramasafumi
Copy link
Owner

Awesome, thank you!
(And I learned warn has uplevel option, which is useful in this case. Actually there are Alba::Deprecation.warn method which basically does the same, but without warning option, which is worse than just using Kernel.warn like this)

@byroot
Copy link
Contributor Author

byroot commented Mar 8, 2025

#424

@okuramasafumi
Copy link
Owner

@byroot Can I merge this PR, or should I wait for #424 to get ready?
I'd like to release a new version and include this change in it.

@byroot
Copy link
Contributor Author

byroot commented Mar 9, 2025

Can I merge this PR, or should I wait for #424 to get ready?

Yes you can.

For #424 there are a few decisions about what you are willing to deprecate or not, that only you can take. And there is no rush anyway.

@okuramasafumi
Copy link
Owner

@byroot Thanks, I'll merge this PR.
#425 Here's a dedicated discussion about this topic, so let's talk about it!

@okuramasafumi okuramasafumi merged commit 834ac9b into okuramasafumi:main Mar 10, 2025
68 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.

3 participants