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

LazyList uses 2x memory for evaluated values compared to Stream #13062

Open
lrytz opened this issue Nov 25, 2024 · 1 comment · May be fixed by scala/scala#10937
Open

LazyList uses 2x memory for evaluated values compared to Stream #13062

lrytz opened this issue Nov 25, 2024 · 1 comment · May be fixed by scala/scala#10937

Comments

@lrytz
Copy link
Member

lrytz commented Nov 25, 2024

In LazyList, an evaluated value is stored in the lazy val state: State[A], which contains an instance of final class Cons[A](val head: A, val tail: LazyList[A]) extends State[A].

So for every evaluated value there's an instance of LazyList plus an instance of Cons.

JOL output for a LazyList with 100 forced new AnyRefs:

scala.collection.immutable.LazyList@2d2e5f00d footprint:
     COUNT       AVG       SUM   DESCRIPTION
       100        16      1600   java.lang.Object
       101        24      2424   scala.collection.immutable.LazyList
       100        24      2400   scala.collection.immutable.LazyList$State$Cons
         1        16        16   scala.collection.immutable.LazyList$State$Empty$
       302                6440   (total)

Compare with Stream:

scala.collection.immutable.Stream$Cons@6ee12bacd footprint:
     COUNT       AVG       SUM   DESCRIPTION
       100        16      1600   java.lang.Object
       100        24      2400   scala.collection.immutable.Stream$Cons
         1        16        16   scala.collection.immutable.Stream$Empty$
       201                4016   (total)

24 bytes is the minimal size as we need two fields (head and tail).

Fortunately, LazyList internals are all private, so there's a chance to fix it. Serialization is already custom, so it's hopefully possible to remain unchanged.

@lrytz
Copy link
Member Author

lrytz commented Nov 25, 2024

Draft PR for discussion: scala/scala#10937 @scala/collections

@SethTisue SethTisue added this to the 2.13.17 milestone Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants