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

Unnecessary boxing of AnyVal values when invoking getClass #10770

Open
carymrobbins opened this issue Mar 12, 2018 · 1 comment
Open

Unnecessary boxing of AnyVal values when invoking getClass #10770

carymrobbins opened this issue Mar 12, 2018 · 1 comment

Comments

@carymrobbins
Copy link

Maybe I'm missing something, but it seems invoking getClass does unnecessary boxing when applied to AnyVal values.

https://gist.github.com/carymrobbins/ec94e043e1fb1ce20501f1808672b9f9

To summarize -

1.getClass returns the int primitive class, but actually does some boxing when looking at the bytecode. I'm not sure why this would be necessary.

1.asInstanceOf[Int with Tag].getClass returns the boxed java.lang.Integer class, even though removing the .getClass returns a primitive int value.

Because of this magic boxing performed in the bytecode, it makes it hard to inspect things at runtime to figure out if we're really dealing with a primitive or boxed value (particularly needed in test suites). Maybe there's a better way to get around that (welcoming suggestions), however, the superfluous boxing seems like something that should be removed.

@joroKr21
Copy link
Member

But it seems like even 1.asInstanceOf[Int with Tag] is boxing and then immediately unboxing (making trait Tag extends Any doesn't change that):

0: iconst_1
1: invokestatic  #30                 // Method scala/runtime/BoxesRunTime.boxToInteger:(I)Ljava/lang/Integer;
4: invokestatic  #51                 // Method scala/runtime/BoxesRunTime.unboxToInt:(Ljava/lang/Object;)I
7: ireturn

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

No branches or pull requests

2 participants