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

Remove JsObject lazy vals #701

Merged
merged 2 commits into from
Jan 28, 2022
Merged

Conversation

htmldoug
Copy link
Contributor

@htmldoug htmldoug commented Jan 20, 2022

Purpose

Reduce memory footprint by removing the (now unnecessary) lazy vals from JsObject.

Background Context

Many of our production OOME heap dumps show JsValue consuming the majority of memory. I've seen scenarios where there are either too many objects (batching, parallelism), unexpectedly large objects (services returning unwanted data), or both. Regardless of other possible mitigations, it would be nice to reduce the memory footprint of JsValue.

In my previous PRs, I migrated most remaining internal usage off of the two lazy vals in JsObject.

Caveats

The only drawbacks I can think of are in situations:

  • if users explicitly stuff a truly mutable.Map into JsObject, in which case, they can restore the memoization of value (which isn't used internally anyway) by wrapping it in an immutable/unmodifiable wrapper.
  • if users are reading fields (no longer used internally) more than once, but it's difficult to think of use cases for this. If desired, override val fields = super.fields.

While these (rare?) situations can be mitigated, the memory footprint of JsObject cannot be fixed in user space.

Benefits

  • Memory footprint of JsObject: 32 bytes => 16 bytes
    • better OOME tolerance
    • reduced GC pressure
  • lazy val initialization check on value is gone which usually results in a ~5% improvement on access

ClassLayout

ClassLayout.parseClass(classOf[JsObject]).toPrintable

Before:

play.api.libs.json.JsObject object internals:
OFF  SZ                   TYPE DESCRIPTION               VALUE
  0   8                        (object header: mark)     N/A
  8   4                        (object header: class)    N/A
 12   1                   byte JsObject.bitmap$0         N/A
 13   3                        (alignment/padding gap)   
 16   4   scala.collection.Seq JsObject.fields           N/A
 20   4   scala.collection.Map JsObject.value            N/A
 24   4   scala.collection.Map JsObject.underlying       N/A
 28   4                        (object alignment gap)    
Instance size: 32 bytes
Space losses: 3 bytes internal + 4 bytes external = 7 bytes total

After:

play.api.libs.json.JsObject object internals:
OFF  SZ                   TYPE DESCRIPTION               VALUE
  0   8                        (object header: mark)     N/A
  8   4                        (object header: class)    N/A
 12   4   scala.collection.Map JsObject.underlying       N/A
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total

if (!found) o ++ JsObject(Seq(this.key -> transform(JsObject.empty)))
else o
val transformed = transform(obj.underlying.getOrElse(key, JsObject.empty))
obj + (key -> transformed)
Copy link
Contributor Author

@htmldoug htmldoug Jan 20, 2022

Choose a reason for hiding this comment

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

Note that our underlying LinkedHashMap correctly preserves order on + for existing keys.

scala> lhm
val res3: java.util.LinkedHashMap[String, Int] = {1=1, 2=2, 3=3}

scala> lhm.put("2", 2222222)
val res4: Int = 2

scala> lhm
val res5: java.util.LinkedHashMap[String, Int] = {1=1, 2=2222222, 3=3}

@htmldoug
Copy link
Contributor Author

@gmethvin, WDYT?

Copy link
Member

@gmethvin gmethvin left a comment

Choose a reason for hiding this comment

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

Yes, I think given the optimizations you've done, it's probably not worth having these lazy vals anymore.

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