From 5f8e022c665e9e5da8e4f2f1159c86eb0cc9a11b Mon Sep 17 00:00:00 2001 From: pdaures Date: Mon, 30 May 2016 20:04:52 +0200 Subject: [PATCH 1/2] replaced regex with faster impl for float parsing --- fix_float.go | 11 ++++++++--- fix_float_test.go | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/fix_float.go b/fix_float.go index 4c19730bd..e99f730d5 100644 --- a/fix_float.go +++ b/fix_float.go @@ -2,7 +2,6 @@ package quickfix import ( "fmt" - "regexp" "strconv" ) @@ -22,8 +21,10 @@ func (f *FIXFloat) Read(bytes []byte) error { } //strconv allows values like "+100.00", which is not allowed for FIX float types - if valid, _ := regexp.MatchString("^[0-9.-]+$", string(bytes)); !valid { - return fmt.Errorf("invalid value %v", string(bytes)) + for _, b := range bytes { + if b != '.' && b != '-' && !isDecimal(b) { + return fmt.Errorf("invalid value %v", string(bytes)) + } } *f = FIXFloat(val) @@ -34,3 +35,7 @@ func (f *FIXFloat) Read(bytes []byte) error { func (f FIXFloat) Write() []byte { return []byte(strconv.FormatFloat(float64(f), 'f', -1, 64)) } + +func isDecimal(b byte) bool { + return '0' <= b && b <= '9' +} diff --git a/fix_float_test.go b/fix_float_test.go index ee85746ca..efe55a163 100644 --- a/fix_float_test.go +++ b/fix_float_test.go @@ -46,3 +46,11 @@ func TestFloatRead(t *testing.T) { } } } + +func BenchmarkFloatRead(b *testing.B) { + val := []byte("15.1234") + for i := 0; i < b.N; i++ { + var field FIXFloat + field.Read(val) + } +} From 7598932468941a8e324a712be4b815b5cf3ba030 Mon Sep 17 00:00:00 2001 From: pdaures Date: Mon, 30 May 2016 20:22:44 +0200 Subject: [PATCH 2/2] added test cases for fix_float --- fix_float_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/fix_float_test.go b/fix_float_test.go index efe55a163..8cb1d1f4c 100644 --- a/fix_float_test.go +++ b/fix_float_test.go @@ -29,7 +29,12 @@ func TestFloatRead(t *testing.T) { expectError bool }{ {[]byte("15"), 15.0, false}, + {[]byte("99.9"), 99.9, false}, + {[]byte("0.00"), 0.0, false}, + {[]byte("-99.9"), -99.9, false}, + {[]byte("-99.9.9"), 0.0, true}, {[]byte("blah"), 0.0, true}, + {[]byte("1.a1"), 0.0, true}, {[]byte("+200.00"), 0.0, true}, }