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

Prefer flatMapIterable(identity()) over flatMap(i -> FluxfromIterable(i)) #279

Merged
merged 4 commits into from
Oct 8, 2022

Conversation

Ptijohn
Copy link
Contributor

@Ptijohn Ptijohn commented Oct 5, 2022

Suggested commit message:

Suggest `Flux#concatMap{,Iterable}` usage in more contexts (#279)

@Ptijohn Ptijohn requested a review from rickie October 5, 2022 14:17
@Ptijohn
Copy link
Contributor Author

Ptijohn commented Oct 5, 2022

Looking at the build failures.

@Ptijohn Ptijohn added new feature blocked This blocks either a release or a downstream project labels Oct 5, 2022
Comment on lines 301 to 302
flux.flatMap(list -> Flux.fromIterable(list), concurrency),
flux.flatMap(Flux::fromIterable, concurrency));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hum, I guess that if concurrency is 1, we want to rewrite that to concatMapIterable to be consistent with rules above 🤔
So maybe I'll add a new entry to FluxConcatMapFromIterable above that rewrites specifically flatMap(..., 1) 🤔
Is there such a thing as priority of rules?
As this actually conflicts with FluxConcatMap...

@Ptijohn
Copy link
Contributor Author

Ptijohn commented Oct 5, 2022

Alright, there's a bit of extra work here than I thought here (see comment above), to make sure to have the proper rewrites in all cases.
Will get back to this tomorrow afternoon 👌

Copy link
Member

@Stephan202 Stephan202 left a comment

Choose a reason for hiding this comment

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

(The over-arching idea here is that each Refaster rule provides a "minimal" improvement, so that we maximize the amount of code cleaned up, without suffering from a combinatorial explosion of different rules.)

Thanks for this contribution @Ptijohn! This one definitely occurs in our code base 😄

I added a commit. Suggested commit message:

Suggest `Flux#concatMap{,Iterable}` usage in more contexts (#279)

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Flux<S> after(Flux<? extends Iterable<S>> flux, int concurrency) {
return flux.flatMapIterable(identity(), concurrency);
Copy link
Member

Choose a reason for hiding this comment

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

Here the second argument is the prefetch value; there is no concurrency here. So i.c.w. your suggestion above, I think we need:

  1. A rewrite for the flatMap(..., 1) case (which is indeed what FluxConcatMap already does, though I see we can add prefetch overrides).
  2. The concatMapIterable check above :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! That makes sense :)
I would have gone this direction today, but well you beat me to it ;)

* Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#concatMap(Function)} with
* {@link Flux#fromIterable(Iterable)}.
*/
static final class FluxConcatMapFromIterable<S> {
Copy link
Member

Choose a reason for hiding this comment

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

Our yet-to-be-codified naming conventions would have this template be named ConcatMapIterableIdentity :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah I was struggling a bit on the naming convention 😇 Learning as I go!

Comment on lines 276 to 277
* Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#concatMap(Function)} with
* {@link Flux#fromIterable(Iterable)}.
Copy link
Member

Choose a reason for hiding this comment

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

Let's say something about avoiding inner subscriptions.

Comment on lines 100 to 101
Flux.just(ImmutableList.of("1")).concatMap(list -> Flux.fromIterable(list)),
Flux.just(ImmutableList.of("1")).concatMap(Flux::fromIterable));
Copy link
Member

Choose a reason for hiding this comment

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

In tests we generally use integers, or the strings "foo", "bar", "baz", etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah yeah fair enough :)

@Ptijohn Ptijohn removed the blocked This blocks either a release or a downstream project label Oct 6, 2022
@Ptijohn Ptijohn added this to the 0.4.0 milestone Oct 7, 2022
@Ptijohn Ptijohn force-pushed the bdiederichs/ANS-1289 branch from a74fed0 to 651905b Compare October 7, 2022 12:52
@Ptijohn
Copy link
Contributor Author

Ptijohn commented Oct 7, 2022

Rebased + resolved conflicts :)
Ready for your eyes @rickie ;)

Copy link
Member

@rickie rickie left a comment

Choose a reason for hiding this comment

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

Added a commit.

Suggested commit message LGTM!

Nice work @Ptijohn , the shiny one is getting closer 😉.

* Prefer {@link Flux#concatMapIterable(Function)} over alternatives that require an additional
* subscription.
*/
static final class ConcatMapIterableIdentity<S> {
Copy link
Member

Choose a reason for hiding this comment

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

We use S when we need two generics, in this case we can simply use <T>.

Copy link
Member

Choose a reason for hiding this comment

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

Ah yes :). (Also something to auto-unify on day 🙈.)

@Stephan202 Stephan202 merged commit 902d4f7 into master Oct 8, 2022
@Stephan202 Stephan202 deleted the bdiederichs/ANS-1289 branch October 8, 2022 09:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants