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

CU-fh2vcf - Deprecate mapN and fix zip for collections #2324

Merged
merged 11 commits into from Mar 13, 2021

Conversation

danimontoya
Copy link
Contributor

@danimontoya danimontoya commented Mar 11, 2021

This PR updates/fixes some zip implementations and adds tests for it.
It also deprecates mapN for collections, which were cartesian builders. For collections, this easily results in really big lists that make the JVM OOM.

Example of dangerous usage:

val x: List<Int> = listOf(1, 2, 3, 4, 5, 6)

// OOMs
val result = mapN(x, x, x, x, x, x, x, x, x, x, ::Tuple10)

This tries to create a list with 6^10 (60466176) elements and as a result OOMs. As you can see this easily results in bad code since here we're only using a list with 6 elements and it already results in problematic code.

Since this pattern isn't so common and is only safe with low arities we will recommend to simply use flatMap/map chains instead.

@franciscodr
Copy link
Collaborator

Copy link
Member

@nomisRev nomisRev left a comment

Choose a reason for hiding this comment

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

LGTM thanks @danimontoya ❤️

Copy link
Member

@nomisRev nomisRev left a comment

Choose a reason for hiding this comment

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

We need to figure out what to do with zip for collections versus cartesian builders.

Comment on lines +14 to +27
val bb = iterator()
val cc = c.iterator()
val dd = d.iterator()

val size = minOf(
collectionSizeOrDefault(10),
c.collectionSizeOrDefault(10),
d.collectionSizeOrDefault(10)
)
val list = ArrayList<E>(size)
while (bb.hasNext() && cc.hasNext() && dd.hasNext()) {
list.add(transform(bb.next(), cc.next(), dd.next()))
}
return list
Copy link
Member

Choose a reason for hiding this comment

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

This impl was inspired by the Kotlin Std implementation

Copy link
Member

@aballano aballano left a comment

Choose a reason for hiding this comment

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

looks good!

"zip2" {
forAll(Gen.nonEmptyList(Gen.int()), Gen.nonEmptyList(Gen.int())) { a, b ->
val result = a.zip(b)
val expected = a.all.zip(b.all).let(NonEmptyList.Companion::fromListUnsafe)
Copy link
Member

Choose a reason for hiding this comment

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

aren't we testing against our own zip version here?

Copy link
Member

Choose a reason for hiding this comment

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

Yes, but that zip is tested against Kotlin Std zip in IterableTest so it assumes our zip is correct/tested. Which is added in this PR also.

@nomisRev nomisRev self-requested a review March 12, 2021 17:34
@nomisRev nomisRev dismissed their stale review March 12, 2021 17:40

Worked on this PR after requesting changes so dismissing my review.

Copy link
Member

@raulraja raulraja left a comment

Choose a reason for hiding this comment

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

thanks folks!

@nomisRev nomisRev changed the title CU-fh2vcf - Replace mapN with zip for NonEmptyList and NonEmptyListApply CU-fh2vcf - Deprecate mapN and fix zip for collections Mar 12, 2021
Copy link
Member

@i-walker i-walker left a comment

Choose a reason for hiding this comment

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

🙌🏾

@i-walker i-walker merged commit b814719 into master Mar 13, 2021
@rachelcarmena rachelcarmena deleted the CU-fh2vcf-replace-mapN-with-zip branch March 19, 2021 09:59
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.

None yet

6 participants