Skip to content

Commit b78fa5d

Browse files
authored
More efficient derivation of codecs for classes without required fields when checking of field duplication is turned off with Scala 3
1 parent 66a18cd commit b78fa5d

File tree

1 file changed

+15
-12
lines changed
  • jsoniter-scala-macros/shared/src/main/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros

1 file changed

+15
-12
lines changed

jsoniter-scala-macros/shared/src/main/scala-3/com/github/plokhotnyuk/jsoniter_scala/macros/JsonCodecMaker.scala

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,8 +1875,9 @@ object JsonCodecMaker {
18751875
val paramVarNum = fields.size
18761876
val lastParamVarIndex = Math.max(0, (paramVarNum - 1) >> 5)
18771877
val lastParamVarBits = -1 >>> -paramVarNum
1878-
val paramVars = (0 to lastParamVarIndex)
1879-
.map(i => ValDef(symbol("p" + i, TypeRepr.of[Int], Flags.Mutable), Some(Literal(IntConstant {
1878+
val paramVars =
1879+
if (required.isEmpty && !cfg.checkFieldDuplication) Nil
1880+
else (0 to lastParamVarIndex).map(i => ValDef(symbol("p" + i, TypeRepr.of[Int], Flags.Mutable), Some(Literal(IntConstant {
18801881
if (i == lastParamVarIndex) lastParamVarBits
18811882
else -1
18821883
}))))
@@ -1951,17 +1952,19 @@ object JsonCodecMaker {
19511952
case '[ft] =>
19521953
val tmpVar = Ref(tmpVars(fieldInfo.symbol.name).symbol)
19531954
val readVal = genReadVal(fTpe :: types, tmpVar.asExprOf[ft], fieldInfo.isStringified, false, in).asTerm
1954-
val n = Ref(paramVars(fieldInfo.nonTransientFieldIndex >> 5).symbol).asExprOf[Int]
1955-
val m = Expr(1 << fieldInfo.nonTransientFieldIndex)
1956-
val nm = Expr(~(1 << fieldInfo.nonTransientFieldIndex))
19571955
Block(List({
1958-
if (cfg.checkFieldDuplication) {
1959-
'{
1960-
if (($n & $m) != 0) ${Assign(n.asTerm, '{ $n ^ $m }.asTerm).asExprOf[Unit]}
1961-
else $in.duplicatedKeyError($l)
1962-
}.asTerm
1963-
} else if (required(fieldInfo.mappedName)) Assign(n.asTerm, '{ $n & $nm }.asTerm)
1964-
else '{ }.asTerm
1956+
val isRequired = required(fieldInfo.mappedName)
1957+
if (isRequired || cfg.checkFieldDuplication) {
1958+
val n = Ref(paramVars(fieldInfo.nonTransientFieldIndex >> 5).symbol).asExprOf[Int]
1959+
val m = Expr(1 << fieldInfo.nonTransientFieldIndex)
1960+
val nm = Expr(~(1 << fieldInfo.nonTransientFieldIndex))
1961+
if (cfg.checkFieldDuplication) {
1962+
'{
1963+
if (($n & $m) != 0) ${Assign(n.asTerm, '{ $n ^ $m }.asTerm).asExprOf[Unit]}
1964+
else $in.duplicatedKeyError($l)
1965+
}.asTerm
1966+
} else Assign(n.asTerm, '{ $n & $nm }.asTerm)
1967+
} else '{ }.asTerm
19651968
}), Assign(tmpVar, readVal)).asExprOf[Unit]
19661969
}
19671970
'{

0 commit comments

Comments
 (0)