Skip to content

Commit 2d37d64

Browse files
committed
More efficient writing of small (less than an hour) java.time.Duration values
1 parent 612d622 commit 2d37d64

File tree

3 files changed

+18
-6
lines changed
  • jsoniter-scala-core

3 files changed

+18
-6
lines changed

jsoniter-scala-core/js/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,8 +1771,12 @@ final class JsonWriter private[jsoniter_scala](
17711771
val effectiveTotalSecs =
17721772
if (totalSecs < 0) (-nano >> 31) - totalSecs
17731773
else totalSecs
1774-
val hours = effectiveTotalSecs / 3600
1775-
val secsOfHour = (effectiveTotalSecs - hours * 3600).toInt
1774+
var hours = 0L
1775+
var secsOfHour = effectiveTotalSecs.toInt
1776+
if (effectiveTotalSecs >= 3600) {
1777+
hours = effectiveTotalSecs / 3600
1778+
secsOfHour = (effectiveTotalSecs - (hours << 12) + (hours << 9) - (hours << 4)).toInt // (effectiveTotalSecs - hours * 3600).toInt
1779+
}
17761780
val minutes = secsOfHour * 17477 >> 20 // divide a small positive int by 60
17771781
val seconds = secsOfHour - minutes * 60
17781782
val ds = digits

jsoniter-scala-core/jvm/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,8 +1598,12 @@ final class JsonWriter private[jsoniter_scala](
15981598
val effectiveTotalSecs =
15991599
if (totalSecs < 0) (-nano >> 31) - totalSecs
16001600
else totalSecs
1601-
val hours = Math.multiplyHigh(effectiveTotalSecs >> 4, 655884233731895169L) >> 3 // divide a positive long by 3600
1602-
val secsOfHour = (effectiveTotalSecs - hours * 3600).toInt
1601+
var hours = 0L
1602+
var secsOfHour = effectiveTotalSecs.toInt
1603+
if (effectiveTotalSecs >= 3600) {
1604+
hours = Math.multiplyHigh(effectiveTotalSecs >> 4, 655884233731895169L) >> 3 // divide a positive long by 3600
1605+
secsOfHour = (effectiveTotalSecs - hours * 3600).toInt
1606+
}
16031607
val minutes = secsOfHour * 17477 >> 20 // divide a small positive int by 60
16041608
val seconds = secsOfHour - minutes * 60
16051609
val ds = digits

jsoniter-scala-core/native/src/main/scala/com/github/plokhotnyuk/jsoniter_scala/core/JsonWriter.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1598,8 +1598,12 @@ final class JsonWriter private[jsoniter_scala](
15981598
val effectiveTotalSecs =
15991599
if (totalSecs < 0) (-nano >> 31) - totalSecs
16001600
else totalSecs
1601-
val hours = NativeMath.multiplyHigh(effectiveTotalSecs >> 4, 655884233731895169L) >> 3 // divide a positive long by 3600
1602-
val secsOfHour = (effectiveTotalSecs - hours * 3600).toInt
1601+
var hours = 0L
1602+
var secsOfHour = effectiveTotalSecs.toInt
1603+
if (effectiveTotalSecs >= 3600) {
1604+
hours = NativeMath.multiplyHigh(effectiveTotalSecs >> 4, 655884233731895169L) >> 3 // divide a positive long by 3600
1605+
secsOfHour = (effectiveTotalSecs - hours * 3600).toInt
1606+
}
16031607
val minutes = secsOfHour * 17477 >> 20 // divide a small positive int by 60
16041608
val seconds = secsOfHour - minutes * 60
16051609
val ds = digits

0 commit comments

Comments
 (0)