-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add `//msgp:compactfloats` file directive, that will store float64 as float32, if it can be done so losslessly. Boring, but correct replacement of #365
- Loading branch information
Showing
14 changed files
with
231 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package _generated | ||
|
||
//go:generate msgp | ||
|
||
//msgp:compactfloats | ||
|
||
//msgp:ignore F64 | ||
type F64 float64 | ||
|
||
//msgp:replace F64 with:float64 | ||
|
||
type Floats struct { | ||
A float64 | ||
B float32 | ||
Slice []float64 | ||
Map map[string]float64 | ||
F F64 | ||
OE float64 `msg:",omitempty"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package _generated | ||
|
||
import ( | ||
"bytes" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/tinylib/msgp/msgp" | ||
) | ||
|
||
func TestCompactFloats(t *testing.T) { | ||
// Constant that can be represented in f32 without loss | ||
const f32ok = -1e2 | ||
allF32 := Floats{ | ||
A: f32ok, | ||
B: f32ok, | ||
Slice: []float64{f32ok, f32ok}, | ||
Map: map[string]float64{"a": f32ok}, | ||
F: f32ok, | ||
OE: f32ok, | ||
} | ||
asF32 := float32(f32ok) | ||
wantF32 := map[string]any{"A": asF32, "B": asF32, "F": asF32, "Map": map[string]any{"a": asF32}, "OE": asF32, "Slice": []any{asF32, asF32}} | ||
|
||
enc, err := allF32.MarshalMsg(nil) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
i, _, _ := msgp.ReadIntfBytes(enc) | ||
got := i.(map[string]any) | ||
if !reflect.DeepEqual(got, wantF32) { | ||
t.Errorf("want: %v, got: %v (diff may be types)", wantF32, got) | ||
} | ||
|
||
var buf bytes.Buffer | ||
en := msgp.NewWriter(&buf) | ||
allF32.EncodeMsg(en) | ||
en.Flush() | ||
enc = buf.Bytes() | ||
i, _, _ = msgp.ReadIntfBytes(enc) | ||
got = i.(map[string]any) | ||
if !reflect.DeepEqual(got, wantF32) { | ||
t.Errorf("want: %v, got: %v (diff may be types)", wantF32, got) | ||
} | ||
|
||
const f64ok = -10e64 | ||
allF64 := Floats{ | ||
A: f64ok, | ||
B: f32ok, | ||
Slice: []float64{f64ok, f64ok}, | ||
Map: map[string]float64{"a": f64ok}, | ||
F: f64ok, | ||
OE: f64ok, | ||
} | ||
asF64 := float64(f64ok) | ||
wantF64 := map[string]any{"A": asF64, "B": asF32, "F": asF64, "Map": map[string]any{"a": asF64}, "OE": asF64, "Slice": []any{asF64, asF64}} | ||
|
||
enc, err = allF64.MarshalMsg(nil) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
i, _, _ = msgp.ReadIntfBytes(enc) | ||
got = i.(map[string]any) | ||
if !reflect.DeepEqual(got, wantF64) { | ||
t.Errorf("want: %v, got: %v (diff may be types)", wantF64, got) | ||
} | ||
|
||
buf.Reset() | ||
en = msgp.NewWriter(&buf) | ||
allF64.EncodeMsg(en) | ||
en.Flush() | ||
enc = buf.Bytes() | ||
i, _, _ = msgp.ReadIntfBytes(enc) | ||
got = i.(map[string]any) | ||
if !reflect.DeepEqual(got, wantF64) { | ||
t.Errorf("want: %v, got: %v (diff may be types)", wantF64, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.