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

ArrayBuilder of Unit needs addAll forwarded #10958

Open
wants to merge 3 commits into
base: 2.13.x
Choose a base branch
from

Conversation

som-snytt
Copy link
Contributor

Fixes scala/bug#13068

Follow-up to previous changes for resizing.

One addAll used to forward to the other. That changed just to avoid redundant range checks.

@scala-jenkins scala-jenkins added this to the 2.13.17 milestone Dec 13, 2024
@som-snytt som-snytt marked this pull request as ready for review December 13, 2024 16:52
@som-snytt som-snytt modified the milestones: 2.13.17, 2.13.16 Dec 13, 2024
@SethTisue
Copy link
Member

@scala/collections

@SethTisue SethTisue added the library:collections PRs involving changes to the standard collection library label Dec 13, 2024
@som-snytt
Copy link
Contributor Author

som-snytt commented Dec 13, 2024

Added to current milestone not for pragmatic reasons, as the alternative method works, but just because.

Quick review: did you add a test for the other method?

Edit:

    // expect an exception when trying to grow larger than maximum size by addAll(array)
    assertThrows[Exception](ab.addAll(arr))

There was already a test that it throws...!

@He-Pin
Copy link
Contributor

He-Pin commented Dec 13, 2024

does I'm the problem live in 2.12.x too?

@som-snytt
Copy link
Contributor Author

@He-Pin No, this is 2.13.x with new collections problems.

https://github.com/scala/scala/blob/2.12.x/src/library/scala/collection/mutable/ArrayBuilder.scala

@som-snytt
Copy link
Contributor Author

I need the meme with the fellow with the nice beard:

I don't always throw exceptions, but when I do, I make it a RuntimeException.

@som-snytt
Copy link
Contributor Author

I absolutely did run library/mimaReportBinaryIssues and it did not make a peep.

Therefore, the task as it currently exists is not useful to me locally. If there is some hoop to jump through, it is probably also not useful to me.

val ab: ArrayBuilder[Unit] = ArrayBuilder.make[Unit]
val arr = Array[Unit]((), (), (), (), (), (), (), (), (), (), (), ())
ab.addAll(arr)
assertEquals(arr.length, ab.result().length)
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we assert the elements are all ()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That sounds extreme, but result doesn't use Array.fill. That is an "optimization", but also therefore a potential point of failure. This class "must" be rarely used, so arguably any optimization is not worth it. Look at the time suck due to "optimizing" the range checks. Probably the test should verify the result.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the input, but verify result seems ✅️

Copy link
Contributor Author

Choose a reason for hiding this comment

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

but then I would have to decide whether to nowarn

[warn] .../ArrayBuilderTest.scala:48:37: dubious usage of method == with unit value
[warn]     assertTrue(ab.result().forall(_ == ()))
[warn]                                     ^

Copy link
Contributor

Choose a reason for hiding this comment

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

but then I would have to decide whether to nowarn

[warn] .../ArrayBuilderTest.scala:48:37: dubious usage of method == with unit value
[warn]     assertTrue(ab.result().forall(_ == ()))
[warn]                                     ^

Maybe eq?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh good suggestion. I did add a commit earlier, but didn't push it yet, because I feel bad for Jenkins.

@som-snytt som-snytt changed the title ArrayBuilder of Unit needs missing override ArrayBuilder of Unit needs addAll forwarded Dec 13, 2024
@som-snytt
Copy link
Contributor Author

I restored the forwarding addAll for reasons of binary compat. A few int ops don't matter when you're copying arrays. Also, if you're computing arrays to fold into an array builder, you don't care about performance.

@@ -46,7 +46,7 @@ sealed abstract class ArrayBuilder[T]
protected[this] def resize(size: Int): Unit

/** Add all elements of an array. */
def addAll(xs: Array[_ <: T]): this.type = doAddAll(xs, 0, xs.length)
def addAll(xs: Array[_ <: T]): this.type = addAll(xs, 0, xs.length)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can just forwarding when xs is not empty

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That is a micro-optimization too far.

Copy link
Contributor

@He-Pin He-Pin left a comment

Choose a reason for hiding this comment

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

lgtm

@som-snytt
Copy link
Contributor Author

It was green until I tempted fate with another Travis build:

No output has been received in the last 10m0s

which looks like "ten months".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
library:collections PRs involving changes to the standard collection library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ArrayBuilder.ofUnit.addAll throws java.lang.UnsupportedOperationException
4 participants