Skip to content

Minor review suggestions #3

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

Open
wants to merge 6 commits into
base: wip/new-vector
Choose a base branch
from
Open

Conversation

lrytz
Copy link

@lrytz lrytz commented Feb 21, 2020

No description provided.

szeiger and others added 6 commits February 17, 2020 17:34
This is complete rewrite of the old scala.collection.immutable.Vector.
The design differs from the old one in one important aspect: Instead
of a single movable finger (the "display" pointers) it has a fixed
finger at each end. These fingers are part of the proper data
structure, not a transient optimization. There are no other ways to
get to the data in the fingers (it is not part of the main data
array), no focus point, and no stabilization. As a side-effect, the
data structure is completely immutable and no extra memory fences are
required. A second major difference lies in the implementation.
Whereas the old Vector uses a single class for collections of all
supported sizes, the new one is split into Vector0 to Vector6 for the
different dimensions of the main data array.

override final def iterator: Iterator[A] =
if(this.isInstanceOf[Vector0.type]) Vector.emptyIterator
if(this eq Vector0) Vector.emptyIterator
Copy link
Owner

Choose a reason for hiding this comment

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

comparing the instance requires getting the instance first, a type check is faster

Choose a reason for hiding this comment

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

Uuuh. That's a nice one!

Comment on lines 123 to 126
override final def length: Int =
if(this.isInstanceOf[BigVector[_]]) this.asInstanceOf[BigVector[_]].length0
else prefix1.length
override final def length: Int = this match {
case value: BigVector[_] => value.length0
case _ => prefix1.length
}
Copy link
Owner

Choose a reason for hiding this comment

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

I didn't measure the impact but the pattern match creates some unnecessary bytecode instructions:

public final int length();
    descriptor: ()I
    Code:
       0: aload_0
       1: instanceof    #485                // class scala/collection/immutable/BigVector
       4: ifeq          15
       7: aload_0
       8: checkcast     #485                // class scala/collection/immutable/BigVector
      11: invokevirtual #488                // Method scala/collection/immutable/BigVector.length0:()I
      14: ireturn
      15: aload_0
      16: invokevirtual #490                // Method prefix1:()[Ljava/lang/Object;
      19: arraylength
      20: ireturn
public final int length();
    descriptor: ()I
    Code:
       0: aload_0
       1: instanceof    #490                // class scala/collection/immutable/BigVector
       4: ifeq          18
       7: aload_0
       8: checkcast     #490                // class scala/collection/immutable/BigVector
      11: invokevirtual #493                // Method scala/collection/immutable/BigVector.length0:()I
      14: istore_1
      15: goto          24
      18: aload_0
      19: invokevirtual #495                // Method prefix1:()[Ljava/lang/Object;
      22: arraylength
      23: istore_1
      24: iload_1
      25: ireturn

Choose a reason for hiding this comment

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

Hehe. Instead of just ireturning, it istore_1s, gotos, iload_1s and then ireturns.

@lrytz
Copy link
Author

lrytz commented Feb 27, 2020

hehe, bytecode shedding 🙂 just pick what seems right to you

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