Skip to content

Commit dc8a5d1

Browse files
committed
encoding/asn1: use reflect.TypeAssert to improve performance
Use "reflect.TypeAssert" can gain some performance improvements: goos: darwin goarch: arm64 pkg: encoding/asn1 cpu: Apple M4 │ old │ new │ │ sec/op │ sec/op vs base │ ObjectIdentifierString-10 51.48n ± 1% 49.72n ± 2% -3.41% (p=0.000 n=10) Marshal-10 7.549µ ± 0% 7.466µ ± 1% -1.10% (p=0.000 n=10) Unmarshal-10 1.808µ ± 0% 1.798µ ± 0% -0.58% (p=0.000 n=10) geomean 889.0n 873.8n -1.70% │ old │ new │ │ B/op │ B/op vs base │ ObjectIdentifierString-10 32.00 ± 0% 32.00 ± 0% ~ (p=1.000 n=10) ¹ Marshal-10 7.336Ki ± 0% 7.336Ki ± 0% ~ (p=1.000 n=10) ¹ Unmarshal-10 432.0 ± 0% 432.0 ± 0% ~ (p=1.000 n=10) ¹ geomean 470.0 470.0 +0.00% ¹ all samples are equal │ old │ new │ │ allocs/op │ allocs/op vs base │ ObjectIdentifierString-10 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Marshal-10 271.0 ± 0% 271.0 ± 0% ~ (p=1.000 n=10) ¹ Unmarshal-10 24.00 ± 0% 24.00 ± 0% ~ (p=1.000 n=10) ¹ geomean 18.67 18.67 +0.00% ¹ all samples are equal Updates #62121
1 parent 4837fbe commit dc8a5d1

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/encoding/asn1/marshal.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -460,17 +460,20 @@ func makeBody(value reflect.Value, params fieldParameters) (e encoder, err error
460460
case flagType:
461461
return bytesEncoder(nil), nil
462462
case timeType:
463-
t := value.Interface().(time.Time)
463+
t, _ := reflect.TypeAssert[time.Time](value)
464464
if params.timeType == TagGeneralizedTime || outsideUTCRange(t) {
465465
return makeGeneralizedTime(t)
466466
}
467467
return makeUTCTime(t)
468468
case bitStringType:
469-
return bitStringEncoder(value.Interface().(BitString)), nil
469+
v, _ := reflect.TypeAssert[BitString](value)
470+
return bitStringEncoder(v), nil
470471
case objectIdentifierType:
471-
return makeObjectIdentifier(value.Interface().(ObjectIdentifier))
472+
v, _ := reflect.TypeAssert[ObjectIdentifier](value)
473+
return makeObjectIdentifier(v)
472474
case bigIntType:
473-
return makeBigInt(value.Interface().(*big.Int))
475+
v, _ := reflect.TypeAssert[*big.Int](value)
476+
return makeBigInt(v)
474477
}
475478

476479
switch v := value; v.Kind() {
@@ -605,7 +608,7 @@ func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
605608
}
606609

607610
if v.Type() == rawValueType {
608-
rv := v.Interface().(RawValue)
611+
rv, _ := reflect.TypeAssert[RawValue](v)
609612
if len(rv.FullBytes) != 0 {
610613
return bytesEncoder(rv.FullBytes), nil
611614
}
@@ -650,7 +653,8 @@ func makeField(v reflect.Value, params fieldParameters) (e encoder, err error) {
650653
tag = params.stringType
651654
}
652655
case TagUTCTime:
653-
if params.timeType == TagGeneralizedTime || outsideUTCRange(v.Interface().(time.Time)) {
656+
t, _ := reflect.TypeAssert[time.Time](v)
657+
if params.timeType == TagGeneralizedTime || outsideUTCRange(t) {
654658
tag = TagGeneralizedTime
655659
}
656660
}

0 commit comments

Comments
 (0)