8000 Fix #1115: Enrich js.WrappedArray with Buffer and BufferLike. by sjrd · Pull Request #1119 · scala-js/scala-js · GitHub
[go: up one dir, main page]

Skip to content

Fix #1115: Enrich js.WrappedArray with Buffer and BufferLike. #1119

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

Merged
merged 5 commits into from
Sep 26, 2014

Conversation

sjrd
Copy link
Member
@sjrd sjrd commented Sep 26, 2014

With these changes, I was able to make the DeltaBlue benchmark run only 2.2x slowlier than the JS version, which means twice as fast as before. I managed this by replacing all Scala collections by js.Arrays, and calling the methods of js.ArrayOps and js.WrappedArray directly on the js.Arrays (which meant virtually no change but to the types).

@sjrd
Copy link
Member Author
sjrd commented Sep 26, 2014

Review by @gzm0
I suppose you'll want tests of the methods I override in js.WrappedArray, but I already create the PR for CI and - if you want - review.

@scala-jenkins
Copy link

Test PASSed.
Refer to this link for build results: https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/590/

@sjrd
Copy link
Member Author
sjrd commented Sep 26, 2014

Updated with the tests.

@scala-jenkins
Copy link

Test FAILed.
Refer to this link for build results: https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/591/

@@ -48,6 +48,13 @@ class Array[A] extends Object {
/** Length of the array. */
def length: Int = native

/** Sets the length of the array.
* If the new length is bigger than the old length, created slots are
* filled with `undefined` (irrespective of the type argument `A`!).
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't like this, but I guess there is nothing we can do... Having js.Array.apply(x: Int) return js.UndefOr is too harsh...

Copy link
Member Author

Choose a reason for hiding this comment

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

We already the problem with new js.Array[A](n) anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

True

@gzm0
Copy link
Contributor
gzm0 commented Sep 26, 2014

That's all

@scala-jenkins
Copy link

Test PASSed.
Refer to this link for build results: https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/592/

This is ok because the result() method of Builder clearly says
that the builder's state is completely undefined after its
invocation.

For reference, scm.ArrayBuffer is also its own Builder.
This basically turns it into a full-blown Scala collection,
whose implementation happens to use a js.Array.

We also mark it @inline, because it gives the BufferLike
interface to js.Array, which js.ArrayOps cannot provide due to
the type parameters of BufferLike being too restrictive.
…rray.

In a sense, this is as if .toJSArray were overridden in js.ArrayOps
and js.WrappedArray, which makes total sense. But since .toJSArray
is a pimp, we need to make this specialization directly in its
implementation.
@sjrd
Copy link
Member Author
sjrd commented Sep 26, 2014

Updated.

@lihaoyi
Copy link
Contributor
lihaoyi commented Sep 26, 2014

Would it be possible to patch the standard library Buffer/etc. to use
WrappedArrays instead by default? I'd worry about optimizing the
benchmark too much for Scala.js, because we'd end up with a very-fast
benchmark that looks nothing like idiomatic Scala. "Idiomatic Scala"
doesn't use js.Arrays for example =P but It'd be cool if I could just use
mutable.Buffer everywhere and get the free performance boost in Scala.js

On Fri, Sep 26, 2014 at 9:53 AM, Sébastien Doeraene <
notifications@github.com> wrote:

Updated.


Reply to this email directly or view it on GitHub
#1119 (comment).

@gzm0
Copy link
Contributor
gzm0 commented Sep 26, 2014

LGTM

@scala-jenkins
Copy link

Test PASSed.
Refer to this link for build results: https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/595/

sjrd added a commit that referenced this pull request Sep 26, 2014
Fix #1115: Enrich js.WrappedArray with Buffer and BufferLike.
@sjrd sjrd merged commit 201d70e into scala-js:master Sep 26, 2014
@sjrd sjrd deleted the js-array-buffer branch September 26, 2014 17:46
@sjrd
Copy link
Member Author
sjrd commented Sep 26, 2014

@lihaoyi Yes, actually, I think we could! It seems we just have to change the implementation of Buffer.newBuilder in this tiny file:
https://github.com/scala/scala/blob/2.11.x/src/library/scala/collection/mutable/Buffer.scala
Fortunately for us, for once something's really well done in this file: the result type of Buffer.newBuilder is explicitly set to Builder[A, Buffer[A]] (and, e.g., to Builder[A, ArrayBuffer[A]]), which our js.WrappedArray conforms to.

Then all you need to do in (cross-compiling) Scala code is use mutable.Buffer.empty[A] to create your optimized buffer (instead of new mutable.ArrayBuffer[A].

@scala-jenkins
Copy link

Test PASSed.
Refer to this link for build results: https://scala-webapps.epfl.ch/jenkins/job/scalajs-pr/596/

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.

4 participants
0