Skip to content

Conversation

@Arneball
Copy link

Instead of

  private val adapters: Array<JsonAdapter<ArticleElement>> = arrayOf(
      moshi.adapter<ArticleElement>(Body::class.java),
      moshi.adapter<ArticleElement>(ElaborateLink::class.java),
      moshi.adapter<ArticleElement>(H1::class.java),
      moshi.adapter<ArticleElement>(H2::class.java),
      moshi.adapter<ArticleElement>(H3::class.java),
      moshi.adapter<ArticleElement>(Heading::class.java),
      moshi.adapter<ArticleElement>(Image::class.java),
  )

We get:

  private val adapter_ArticleElement_Body: JsonAdapter<ArticleElement> =
      moshi.adapter<ArticleElement>(Body::class.java)

  private val adapter_ArticleElement_ElaborateLink: JsonAdapter<ArticleElement> =
      moshi.adapter<ArticleElement>(ElaborateLink::class.java)

  private val adapter_ArticleElement_H1: JsonAdapter<ArticleElement> =
      moshi.adapter<ArticleElement>(H1::class.java)

  private val adapter_ArticleElement_H2: JsonAdapter<ArticleElement> =
      moshi.adapter<ArticleElement>(H2::class.java)

  private val adapter_ArticleElement_H3: JsonAdapter<ArticleElement> =
      moshi.adapter<ArticleElement>(H3::class.java)

  private val adapter_ArticleElement_Heading: JsonAdapter<ArticleElement> =
      moshi.adapter<ArticleElement>(Heading::class.java)

  private val adapter_ArticleElement_Image: JsonAdapter<ArticleElement> =
      moshi.adapter<ArticleElement>(Image::class.java)

Which means one mutable array less, less indirection, less memory used.

@Arneball Arneball requested a review from ansman as a code owner October 27, 2025 19:14
@ansman
Copy link
Owner

ansman commented Oct 27, 2025

I don't think this will use less memory actually, and if it does it's negligible. Instead of one array with N instance references you have N fields with an instance reference. So all you're saving is the overhead for an array which should be minuscule.
In addition to that, this will increase the byte code size, and be slower since a when is slower than an array lookup. If you can show some benchmarks showing the serialization performance and memory used I would consider merging this but I think the added complexity is not worth it

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.

2 participants