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

lazy vals on Either should probably be defs #797

Closed
scabug opened this issue Apr 23, 2008 · 9 comments
Closed

lazy vals on Either should probably be defs #797

scabug opened this issue Apr 23, 2008 · 9 comments

Comments

@scabug
Copy link

scabug commented Apr 23, 2008

If you look at scala.Either, it has a lot of lazy vals:
http://www.scala-lang.org/docu/files/api/scala/Either.html

These should probably all be defs. The results are extremely lightweight, and the allocation is probably comparably expensive to the laziness overhead so the fact that there are lazy vals just increases the memory overhead per instance of Either unneccessarily.

@scabug
Copy link
Author

scabug commented Apr 23, 2008

Imported From: https://issues.scala-lang.org/browse/SI-797?orig=1
Reporter: @DRMacIver

@scabug
Copy link
Author

scabug commented Apr 23, 2008

@DRMacIver said:
The problem is worse than I thought actually. A single instance of Either can use up unbounded space.

The problem is that x holds onto x.swap which holds onto x.swap.swap which...

You get the idea.

I'm more and more convinced that making any of these lazy vals is useless and actively dangerous.

@scabug
Copy link
Author

scabug commented Apr 23, 2008

@mcdirmid said:
Another case for stable defs.

@scabug
Copy link
Author

scabug commented Apr 24, 2008

Geoffrey Alan Washburn (washburn) said:
Stable defs would be a useful thing to have, but it is not clear whether we could come up with a set of restrictions and a way of testing them that works well. I've made a note to think about it more later.

In trunk, and in the 2.7.1 branch, I've converted most of the uses of lazy val to def. I left isLeft and isRight as think that they are unlikely to hold onto an unbounded amount of memory. If someone can prove me wrong, let me know and I'll change them as well.

@scabug
Copy link
Author

scabug commented Apr 24, 2008

@DRMacIver said:
isLeft and isRight are unlikely to hold onto an unbounded amount of memory, but they don't serve a useful purpose either and continue to bloat the memory footprint of Either instances. It's not like it's an expensive computation. You could make it even less so by making them abstract and override them in Left and Right.

While I'm making suggestions, wouldn't it be better if Either were a sealed abstract class so you could get pattern match warnings?

@scabug
Copy link
Author

scabug commented Apr 24, 2008

@ijuma said:
I noticed the same thing as DRMacIver but actually tested this to have some numbers to back me up. Making isLeft and isRight a def is actually faster, even if by a very small amount (JDK6u4), so there's no reason to make them lazy vals.

This is not unexpected because lazy vals involve a volatile read for thread-safety reasons (if I am not mistaken) and instanceof checks are intrinsic operations in recent JVMs.

@scabug
Copy link
Author

scabug commented Apr 24, 2008

Geoffrey Alan Washburn (washburn) said:
I've made all these changes. Given that Left and Right are final, I cannot see any sensible way someone could be using Either as a mixin.

Further suggestions are welcome, but put them in new tickets to make them easier to keep track of separately.

@scabug
Copy link
Author

scabug commented Apr 24, 2008

@ijuma said:
Thanks!

@scabug
Copy link
Author

scabug commented Jan 14, 2009

@odersky said:
Milestone next_bugfix deleted

@scabug scabug closed this as completed May 18, 2011
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

1 participant