spray / spray-json

A lightweight, clean and simple JSON implementation in Scala
Apache License 2.0
969 stars 190 forks source link

Generated jsonFormatN has unexpectedly large sizeHint #340

Open cplesner-humio opened 3 years ago

cplesner-humio commented 3 years ago

The code generated for this line,

https://github.com/spray/spray-json/blob/d0008f56bc07f7b5cd446b83184949155d40a7ff/src/main/boilerplate/spray/json/ProductFormatsInstances.scala.template#L31

is expanded to this for instance for jsonFormat21:

    def write(p: T) = {
      val fields = new collection.mutable.ListBuffer[(String, JsValue)]
      fields.sizeHint(20 * 21)
      fields ++= productElement2Field[P1](fieldName1, p, 0)
      ...

I don't think ListBuffer actually uses the size hint so it's not that bad but if you ever switched to a different kind of collection that did (and actually side note: why not use Map.newBuilder instead?) it would be quite problematic.

jrudolph commented 3 years ago

Agreed, the whole setup is quite weird.