-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #646 from gnieh/json/render
Backporting #641 to main branch
- Loading branch information
Showing
6 changed files
with
425 additions
and
173 deletions.
There are no files selected for viewing
85 changes: 85 additions & 0 deletions
85
benchmarks/src/main/scala/fs2/data/benchmarks/PrinterBenchmarks.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package fs2.data.benchmarks | ||
|
||
import cats.effect.SyncIO | ||
import cats.effect.IO | ||
import fs2.data.json.Token | ||
import fs2.data.json.circe.* | ||
import fs2.{Fallible, Stream} | ||
import io.circe.Json | ||
import org.openjdk.jmh.annotations.* | ||
import org.openjdk.jmh.infra.Blackhole | ||
import cats.effect.unsafe.implicits.global | ||
|
||
import java.util.concurrent.TimeUnit | ||
|
||
@OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
@BenchmarkMode(Array(Mode.AverageTime)) | ||
@State(org.openjdk.jmh.annotations.Scope.Benchmark) | ||
@Fork(value = 1) | ||
@Warmup(iterations = 15, time = 5) | ||
@Measurement(iterations = 10, time = 5) | ||
class PrinterBenchmarks { | ||
|
||
val intArrayStream = | ||
Stream.emits( | ||
Token.StartArray :: | ||
(List | ||
.range(0, 1000000) | ||
.map(i => Token.NumberValue(i.toString())) :+ Token.EndArray)) | ||
|
||
val objectStream = | ||
Stream.emits( | ||
Token.StartObject :: | ||
(List | ||
.range(0, 1000000) | ||
.flatMap(i => List(Token.Key(s"key:$i"), Token.NumberValue(i.toString()))) :+ Token.EndObject)) | ||
|
||
@Benchmark | ||
def intArrayCompact(bh: Blackhole) = | ||
bh.consume( | ||
intArrayStream | ||
.through(fs2.data.json.render.compact) | ||
.compile | ||
.drain) | ||
|
||
@Benchmark | ||
def objectCompact(bh: Blackhole) = | ||
bh.consume( | ||
objectStream | ||
.through(fs2.data.json.render.compact) | ||
.compile | ||
.drain) | ||
|
||
@Benchmark | ||
def intArrayPretty(bh: Blackhole) = | ||
bh.consume( | ||
intArrayStream | ||
.through(fs2.data.json.render.prettyPrint()) | ||
.compile | ||
.drain) | ||
|
||
@Benchmark | ||
def objectPretty(bh: Blackhole) = | ||
bh.consume( | ||
objectStream | ||
.through(fs2.data.json.render.prettyPrint()) | ||
.compile | ||
.drain) | ||
|
||
@Benchmark | ||
def intArrayPrettyLegacy(bh: Blackhole) = | ||
bh.consume( | ||
intArrayStream | ||
.through(fs2.data.json.render.pretty()) | ||
.compile | ||
.drain) | ||
|
||
@Benchmark | ||
def objectPrettyLegacy(bh: Blackhole) = | ||
bh.consume( | ||
objectStream | ||
.through(fs2.data.json.render.pretty()) | ||
.compile | ||
.drain) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
text/shared/src/main/scala/fs2/data/text/render/internal/NonEmptyIntList.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2024 fs2-data Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package fs2.data.text.render.internal | ||
|
||
private sealed trait NonEmptyIntList { | ||
def head: Int | ||
def ::(i: Int): NonEmptyIntList = | ||
More(i, this) | ||
def incHead: NonEmptyIntList | ||
def decHead: NonEmptyIntList | ||
def pop: NonEmptyIntList | ||
} | ||
private final case class One(head: Int) extends NonEmptyIntList { | ||
override def incHead: NonEmptyIntList = One(head + 1) | ||
override def decHead: NonEmptyIntList = One(head - 1) | ||
override lazy val pop: NonEmptyIntList = One(0) | ||
} | ||
private final case class More(head: Int, tail: NonEmptyIntList) extends NonEmptyIntList { | ||
override def incHead: NonEmptyIntList = More(head + 1, tail) | ||
override def decHead: NonEmptyIntList = More(head - 1, tail) | ||
override def pop: NonEmptyIntList = tail | ||
} |
Oops, something went wrong.