Skip to content

Commit 515f1d0

Browse files
committed
Update example02.sc to use more efficient reading through memory mapped files
1 parent 6e867e1 commit 515f1d0

File tree

3 files changed

+55
-5
lines changed

3 files changed

+55
-5
lines changed

jsoniter-scala-examples/README.md

+45-2
Original file line numberDiff line numberDiff line change
@@ -125,5 +125,48 @@ Expected output:
125125

126126
## RFC-8259 validation (example02)
127127

128-
An example of a command line application that reads the system input, parses and validates JSON according to the latest
129-
specification and in case of any error prints it to the system error output
128+
An example of a command line application that reads the provided file, parses and validates JSON according to the latest
129+
specification and in case of any error prints it to the system error output.
130+
131+
For faster JSON validation the example uses reading through memory mapped files which are not supported by Scala.js.
132+
133+
### Build uber jar, print its size, and measure its start up time
134+
135+
```sh
136+
scala-cli --power package --assembly example02.sc --force -o example02.jar
137+
ls -l ./example02.jar
138+
time ./example02.jar test.json 2> /dev/null
139+
```
140+
Expected output:
141+
```text
142+
real 0m0.096s
143+
user 0m0.138s
144+
sys 0m0.017s
145+
```
146+
### Build GraalVM native image, print its size, and measure its start up time
147+
148+
```sh
149+
scala-cli --power package --jvm graalvm --native-image example02.sc --force -o example02_graalvm.bin
150+
ls -l ./example02_graalvm.bin
151+
time ./example02_graalvm.bin test.json 2> /dev/null
152+
```
153+
Expected output:
154+
```text
155+
real 0m0.003s
156+
user 0m0.000s
157+
sys 0m0.002s
158+
```
159+
160+
### Build Scala Native image, print its size, and measure its start up time
161+
162+
```sh
163+
scala-cli --power package --native-version 0.4.16 --native example02.sc --force -o example02_native.bin
164+
ls -l ./example02_native.bin
165+
time ./example02_native.bin test.json 2> /dev/null
166+
```
167+
Expected output:
168+
```text
169+
real 0m0.003s
170+
user 0m0.000s
171+
sys 0m0.003s
172+
```

jsoniter-scala-examples/example02.sc

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
//> using dep "com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-core::2.28.2"
22

33
import com.github.plokhotnyuk.jsoniter_scala.core._
4+
import java.io.RandomAccessFile
5+
import java.nio.channels.FileChannel
46

57
val jsonCodec: JsonValueCodec[Unit] = new JsonValueCodec[Unit] {
68
override def decodeValue(in: JsonReader, default: Unit): Unit = decode(in, 1024) // Max depth is 1024
79

810
override def encodeValue(x: Unit, out: JsonWriter): Unit = ???
911

10-
override def nullValue(): Unit = ()
12+
override def nullValue: Unit = ()
1113

1214
private[this] def decode(in: JsonReader, depth: Int): Unit = {
1315
val b = in.nextToken()
@@ -43,10 +45,14 @@ val jsonCodec: JsonValueCodec[Unit] = new JsonValueCodec[Unit] {
4345
}) ()
4446
if (!in.isCurrentToken('}')) in.objectEndOrCommaError()
4547
}
46-
} else in.readNullOrError(nullValue(), "expected JSON value")
48+
} else in.readNullOrError(nullValue, "expected JSON value")
4749
}
4850
}
4951

50-
try readFromStream(System.in)(jsonCodec) catch {
52+
try {
53+
val fileChannel = new RandomAccessFile(args(0), "rw").getChannel
54+
val mappedByteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, fileChannel.size)
55+
readFromByteBuffer(mappedByteBuffer)(jsonCodec)
56+
} catch {
5157
case ex: Throwable => ex.printStackTrace(System.err)
5258
}

jsoniter-scala-examples/test.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"x:123}

0 commit comments

Comments
 (0)