diff options
| author | Brad Fitzpatrick <bradfitz@tailscale.com> | 2024-07-07 13:30:47 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@tailscale.com> | 2024-07-07 13:34:11 -0700 |
| commit | 1f845922bf3d5141226940ae766f5c1f72dc4cbd (patch) | |
| tree | 1d7f33e4d7cf900d37f46171b35fb553d03d0dea /types | |
| parent | d2fef01206cd7a96d684f9c69ba9e767de824ab4 (diff) | |
| download | tailscale-bradfitz/json2.tar.xz tailscale-bradfitz/json2.zip | |
types/views: add MarshalJSONV2 to Slicebradfitz/json2
It helps, but not all the way.
│ before │ after │
│ B/op │ B/op vs base │
JSONMarshalView/v1_slice-8 64.00 ± 0% 64.00 ± ∞ ¹ ~ (p=1.000 n=10+1) ²
JSONMarshalView/v1_view-8 136.0 ± 0% 136.0 ± ∞ ¹ ~ (p=1.000 n=10+1) ²
JSONMarshalView/v2_slice-8 64.00 ± 0% 64.00 ± ∞ ¹ ~ (p=1.000 n=10+1) ²
JSONMarshalView/v2_view-8 136.0 ± 0% 112.0 ± ∞ ¹ -17.65% (n=10+1)
Updates tailscale/corp#14379
Updates tailscale/corp#21429 (new benchmark)
Change-Id: I891dd6a4db1a995ae1be14f8c5085629bb30b8da
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Diffstat (limited to 'types')
| -rw-r--r-- | types/views/views.go | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/types/views/views.go b/types/views/views.go index 42758966f..fa0e45adb 100644 --- a/types/views/views.go +++ b/types/views/views.go @@ -13,6 +13,9 @@ import ( "maps" "slices" + jsonexp "github.com/go-json-experiment/json" + jsontext "github.com/go-json-experiment/json/jsontext" + "go4.org/mem" ) @@ -225,6 +228,13 @@ func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.ж) } +var _ jsonexp.MarshalerV2 = Slice[string]{nil} + +// MarshalJSON implements github.com/go-json-experiment/json.MarshalerV2. +func (v Slice[T]) MarshalJSONV2(e *jsontext.Encoder, opts jsonexp.Options) error { + return jsonexp.MarshalEncode(e, v.ж, opts) +} + // UnmarshalJSON implements json.Unmarshaler. func (v *Slice[T]) UnmarshalJSON(b []byte) error { return unmarshalSliceFromJSON(b, &v.ж) |
