vert-x3 / vertx-web

HTTP web applications for Vert.x
Apache License 2.0
1.11k stars 535 forks source link

fix(HttpContext): unnecesery string to buffer json encoding #2669

Closed zekronium closed 4 weeks ago

zekronium commented 4 weeks ago

Motivation:

I noticed that JSON is getting double-encoded and wastefully converted toString then toBuffer

As per the benchmarks in vertx-core it shows that this is actually wasteful:

Benchmark                                          Mode  Cnt  Score    Error  Units
JsonEncodeBenchmark.deepBufferDatabind             avgt   10  0.974 ±  0.025  ms/op
JsonEncodeBenchmark.deepBufferJackson              avgt   10  0.935 ±  0.018  ms/op
JsonEncodeBenchmark.deepStringDatabind             avgt   10  1.258 ±  0.006  ms/op
JsonEncodeBenchmark.deepStringJackson              avgt   10  1.270 ±  0.006  ms/op
JsonEncodeBenchmark.deepStringThenBufferDatabind   avgt   10  1.471 ±  0.015  ms/op
JsonEncodeBenchmark.deepStringThenBufferJackson    avgt   10  1.461 ±  0.008  ms/op
JsonEncodeBenchmark.smallBufferDatabind            avgt   10  0.003 ±  0.001  ms/op
JsonEncodeBenchmark.smallBufferJackson             avgt   10  0.003 ±  0.001  ms/op
JsonEncodeBenchmark.smallStringDatabind            avgt   10  0.003 ±  0.001  ms/op
JsonEncodeBenchmark.smallStringJackson             avgt   10  0.003 ±  0.001  ms/op
JsonEncodeBenchmark.smallStringThenBufferDatabind  avgt   10  0.004 ±  0.001  ms/op
JsonEncodeBenchmark.smallStringThenBufferJackson   avgt   10  0.004 ±  0.001  ms/op
JsonEncodeBenchmark.wideBufferDatabind             avgt   10  0.233 ±  0.002  ms/op
JsonEncodeBenchmark.wideBufferJackson              avgt   10  0.231 ±  0.004  ms/op
JsonEncodeBenchmark.wideStringDatabind             avgt   10  0.328 ±  0.003  ms/op
JsonEncodeBenchmark.wideStringJackson              avgt   10  0.368 ±  0.005  ms/op
JsonEncodeBenchmark.wideStringThenBufferDatabind   avgt   10  0.414 ±  0.033  ms/op
JsonEncodeBenchmark.wideStringThenBufferJackson    avgt   10  0.393 ±  0.009  ms/op
  private void stringThenBuffJackson(JsonObject jsonObject, Blackhole blackhole) throws Exception {
    blackhole.consume(Buffer.buffer(jsonObject.encode()));
  }

  private void stringThenBuffDatabind(JsonObject jsonObject, Blackhole blackhole) throws Exception {
    blackhole.consume(Buffer.buffer(databindCodec.toString(jsonObject)));
  }
vietj commented 4 weeks ago

can you port to master as well ?