Skip to content

Commit

Permalink
reduce JsSuccess and JsError instance size
Browse files Browse the repository at this point in the history
before

```
play.api.libs.json.JsError object internals:
OFF  SZ                   TYPE DESCRIPTION               VALUE
  0   8                        (object header: mark)     N/A
  8   4                        (object header: class)    N/A
 12   1                boolean JsError.isSuccess         N/A
 13   1                boolean JsError.isError           N/A
 14   2                        (alignment/padding gap)
 16   4   scala.collection.Seq JsError.errors            N/A
 20   4            scala.None$ JsError.asOpt             N/A
Instance size: 24 bytes
Space losses: 2 bytes internal + 0 bytes external = 2 bytes total
```

after

```
play.api.libs.json.JsError object internals:
OFF  SZ                   TYPE DESCRIPTION               VALUE
  0   8                        (object header: mark)     N/A
  8   4                        (object header: class)    N/A
 12   4   scala.collection.Seq JsError.errors            N/A
Instance size: 16 bytes
Space losses: 0 bytes internal + 0 bytes external = 0 bytes total
```
  • Loading branch information
xuwei-k committed Sep 1, 2024
1 parent 02e70c4 commit 13199c3
Showing 1 changed file with 5 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import scala.collection.Seq
case class JsSuccess[T](value: T, path: JsPath = JsPath()) extends JsResult[T] {
def get: T = value

val isSuccess = true
val isError = false
def isSuccess = true
def isError = false

def fold[U](invalid: Seq[(JsPath, Seq[JsonValidationError])] => U, valid: T => U): U = valid(value)

Expand Down Expand Up @@ -60,8 +60,8 @@ case class JsError(errors: Seq[(JsPath, Seq[JsonValidationError])]) extends JsRe
def +:(error: (JsPath, JsonValidationError)): JsError = JsError.merge(JsError(error), this)
def prepend(error: (JsPath, JsonValidationError)): JsError = this.+:(error)

val isSuccess = false
val isError = true
def isSuccess = false
def isError = true

def fold[U](invalid: Seq[(JsPath, Seq[JsonValidationError])] => U, valid: Nothing => U): U = invalid(errors)

Expand All @@ -84,7 +84,7 @@ case class JsError(errors: Seq[(JsPath, Seq[JsonValidationError])]) extends JsRe

def orElse[U >: Nothing](t: => JsResult[U]): JsResult[U] = t

val asOpt = None
def asOpt = None

def asEither: Either[Seq[(JsPath, Seq[JsonValidationError])], Nothing] = Left(errors)

Expand Down

0 comments on commit 13199c3

Please sign in to comment.