Skip to content

Commit 8febefe

Browse files
committed
types/wgkey: use value receiver with MarshalJSON
Pointer receivers used with MarshalJSON are code rakes. golang/go#22967 dominikh/go-tools#911 I just stepped on one, and it hurt. Turn it over. While we're here, optimize the code a bit. name old time/op new time/op delta MarshalJSON-8 184ns ± 0% 44ns ± 0% -76.03% (p=0.000 n=20+19) name old alloc/op new alloc/op delta MarshalJSON-8 184B ± 0% 80B ± 0% -56.52% (p=0.000 n=20+20) name old allocs/op new allocs/op delta MarshalJSON-8 4.00 ± 0% 1.00 ± 0% -75.00% (p=0.000 n=20+20) Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
1 parent da07ab4 commit 8febefe

File tree

2 files changed

+42
-4
lines changed

2 files changed

+42
-4
lines changed

types/wgkey/key.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,11 @@ func (k *Key) IsZero() bool {
9191
}
9292

9393
func (k Key) MarshalJSON() ([]byte, error) {
94-
buf := new(bytes.Buffer)
95-
fmt.Fprintf(buf, `"%x"`, k[:])
96-
return buf.Bytes(), nil
94+
buf := make([]byte, 2+len(k)*2)
95+
buf[0] = '"'
96+
hex.Encode(buf[1:], k[:])
97+
buf[len(buf)-1] = '"'
98+
return buf, nil
9799
}
98100

99101
func (k *Key) UnmarshalJSON(b []byte) error {

types/wgkey/key_test.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package wgkey
66

77
import (
88
"bytes"
9+
"encoding/json"
910
"testing"
1011
)
1112

@@ -20,7 +21,7 @@ func TestKeyBasics(t *testing.T) {
2021
t.Fatal(err)
2122
}
2223

23-
t.Run("JSON round-trip", func(t *testing.T) {
24+
t.Run("JSON round-trip (pointer)", func(t *testing.T) {
2425
// should preserve the keys
2526
k2 := new(Key)
2627
if err := k2.UnmarshalJSON(b); err != nil {
@@ -55,6 +56,27 @@ func TestKeyBasics(t *testing.T) {
5556
t.Fatalf("base64-encoded keys match: %s, %s", b1, b2)
5657
}
5758
})
59+
60+
t.Run("JSON round-trip (value)", func(t *testing.T) {
61+
type T struct {
62+
K Key
63+
}
64+
v := T{K: *k1}
65+
b, err := json.Marshal(v)
66+
if err != nil {
67+
t.Fatal(err)
68+
}
69+
var u T
70+
if err := json.Unmarshal(b, &u); err != nil {
71+
t.Fatal(err)
72+
}
73+
if !bytes.Equal(v.K[:], u.K[:]) {
74+
t.Fatalf("v.K %v != u.K %v", v.K[:], u.K[:])
75+
}
76+
if b1, b2 := v.K.String(), u.K.String(); b1 != b2 {
77+
t.Fatalf("base64-encoded keys do not match: %s, %s", b1, b2)
78+
}
79+
})
5880
}
5981
func TestPrivateKeyBasics(t *testing.T) {
6082
pri, err := NewPrivate()
@@ -109,3 +131,17 @@ func TestPrivateKeyBasics(t *testing.T) {
109131
}
110132
})
111133
}
134+
135+
var sink []byte
136+
137+
func BenchmarkMarshalJSON(b *testing.B) {
138+
b.ReportAllocs()
139+
var k Key
140+
for i := 0; i < b.N; i++ {
141+
var err error
142+
sink, err = k.MarshalJSON()
143+
if err != nil {
144+
b.Fatal(err)
145+
}
146+
}
147+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy