Skip to content

Commit

Permalink
Reduce allocations when building values for reflect.TypeOf
Browse files Browse the repository at this point in the history
Use reflect.TypeOf((*T)(nil)) instead of reflect.TypeOf(&T{}).

See related issue on Go stdlib: golang/go#55973
  • Loading branch information
dolmen committed Jun 26, 2023
1 parent 6531eff commit b07de00
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crypter.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func NewEncrypter(enc ContentEncryption, rcpt Recipient, opts *EncrypterOptions)
switch rcpt.Algorithm {
case DIRECT:
// Direct encryption mode must be treated differently
if reflect.TypeOf(rawKey) != reflect.TypeOf([]byte{}) {
if reflect.TypeOf(rawKey) != reflect.TypeOf([]byte(nil)) {
return nil, ErrUnsupportedKeyType
}
if encrypter.cipher.keySize() != len(rawKey.([]byte)) {
Expand All @@ -189,7 +189,7 @@ func NewEncrypter(enc ContentEncryption, rcpt Recipient, opts *EncrypterOptions)
case ECDH_ES:
// ECDH-ES (w/o key wrapping) is similar to DIRECT mode
typeOf := reflect.TypeOf(rawKey)
if typeOf != reflect.TypeOf(&ecdsa.PublicKey{}) {
if typeOf != reflect.TypeOf((*ecdsa.PublicKey)(nil)) {
return nil, ErrUnsupportedKeyType
}
encrypter.keyGenerator = ecKeyGenerator{
Expand Down

0 comments on commit b07de00

Please sign in to comment.