├── README.md ├── struct.go ├── .gitignore ├── defer_test.go ├── memory_test.go ├── ring_test.go ├── map_test.go ├── channel_test.go ├── struct_gen_test.go ├── reflection_test.go ├── ring_padded.go ├── struct_gen.go ├── LICENSE └── struct_ffjson.go /README.md: -------------------------------------------------------------------------------- 1 | # go-benchmarks 2 | 3 | Benchmarks used in [So You Wanna Go Fast?](http://bravenewgeek.com/so-you-wanna-go-fast/). 4 | -------------------------------------------------------------------------------- /struct.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | type Iface interface { 4 | Foo() 5 | } 6 | 7 | type Struct struct { 8 | Field1 string 9 | Field2 int 10 | Field3 []string 11 | Field4 uint64 12 | Field5 string 13 | Field6 string 14 | Field7 []byte 15 | } 16 | 17 | func (s *Struct) Foo() {} 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files, Static and Dynamic libs (Shared Objects) 2 | *.o 3 | *.a 4 | *.so 5 | 6 | # Folders 7 | _obj 8 | _test 9 | 10 | # Architecture specific extensions/prefixes 11 | *.[568vq] 12 | [568vq].out 13 | 14 | *.cgo1.go 15 | *.cgo2.c 16 | _cgo_defun.c 17 | _cgo_gotypes.go 18 | _cgo_export.* 19 | 20 | _testmain.go 21 | 22 | *.exe 23 | *.test 24 | *.prof 25 | -------------------------------------------------------------------------------- /defer_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "sync" 5 | "testing" 6 | ) 7 | 8 | func BenchmarkMutexDeferUnlock(b *testing.B) { 9 | var mu sync.Mutex 10 | for i := 0; i < b.N; i++ { 11 | func() { 12 | mu.Lock() 13 | defer mu.Unlock() 14 | }() 15 | } 16 | } 17 | 18 | func BenchmarkMutexUnlock(b *testing.B) { 19 | var mu sync.Mutex 20 | for i := 0; i < b.N; i++ { 21 | func() { 22 | mu.Lock() 23 | mu.Unlock() 24 | }() 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /memory_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "sync" 5 | "testing" 6 | ) 7 | 8 | func BenchmarkAllocateHeap(b *testing.B) { 9 | for i := 0; i < b.N; i++ { 10 | func() *Struct { 11 | return new(Struct) 12 | }() 13 | } 14 | } 15 | 16 | func BenchmarkAllocateStack(b *testing.B) { 17 | for i := 0; i < b.N; i++ { 18 | func() Struct { 19 | return Struct{} 20 | }() 21 | } 22 | } 23 | 24 | func BenchmarkPassByReference(b *testing.B) { 25 | f := func(s *Struct) {} 26 | s := new(Struct) 27 | b.ResetTimer() 28 | 29 | for i := 0; i < b.N; i++ { 30 | f(s) 31 | } 32 | } 33 | 34 | func BenchmarkPassByValue(b *testing.B) { 35 | f := func(s Struct) {} 36 | s := Struct{} 37 | b.ResetTimer() 38 | 39 | for i := 0; i < b.N; i++ { 40 | f(s) 41 | } 42 | } 43 | 44 | func BenchmarkConcurrentStructAllocate(b *testing.B) { 45 | var wg sync.WaitGroup 46 | wg.Add(10) 47 | b.ResetTimer() 48 | 49 | for i := 0; i < 10; i++ { 50 | go func() { 51 | for j := 0; j < b.N; j++ { 52 | _ = func() *Struct { 53 | return new(Struct) 54 | }() 55 | } 56 | wg.Done() 57 | }() 58 | } 59 | 60 | wg.Wait() 61 | } 62 | 63 | func BenchmarkConcurrentStructPool(b *testing.B) { 64 | pool := &sync.Pool{New: func() interface{} { return new(Struct) }} 65 | var wg sync.WaitGroup 66 | wg.Add(10) 67 | b.ResetTimer() 68 | 69 | for i := 0; i < 10; i++ { 70 | go func() { 71 | for j := 0; j < b.N; j++ { 72 | s := func() *Struct { 73 | return pool.Get().(*Struct) 74 | }() 75 | pool.Put(s) 76 | } 77 | wg.Done() 78 | }() 79 | } 80 | 81 | wg.Wait() 82 | } 83 | -------------------------------------------------------------------------------- /ring_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "sync" 5 | "testing" 6 | 7 | "github.com/Workiva/go-datastructures/queue" 8 | ) 9 | 10 | func BenchmarkRingBufferSPSC(b *testing.B) { 11 | q := queue.NewRingBuffer(uint64(b.N)) 12 | b.ResetTimer() 13 | 14 | go func() { 15 | for i := 0; i < b.N; i++ { 16 | q.Put(i) 17 | } 18 | }() 19 | 20 | for i := 0; i < b.N; i++ { 21 | q.Get() 22 | } 23 | } 24 | 25 | func BenchmarkRingBufferPaddedSPSC(b *testing.B) { 26 | q := NewRingBufferPadded(uint64(b.N)) 27 | b.ResetTimer() 28 | 29 | go func() { 30 | for i := 0; i < b.N; i++ { 31 | q.Put(i) 32 | } 33 | }() 34 | 35 | for i := 0; i < b.N; i++ { 36 | q.Get() 37 | } 38 | } 39 | 40 | func BenchmarkRingBufferMPMC(b *testing.B) { 41 | q := queue.NewRingBuffer(uint64(b.N * 100)) 42 | var wg sync.WaitGroup 43 | wg.Add(100) 44 | b.ResetTimer() 45 | 46 | for i := 0; i < 100; i++ { 47 | go func() { 48 | for i := 0; i < b.N; i++ { 49 | q.Put(i) 50 | } 51 | }() 52 | } 53 | 54 | for i := 0; i < 100; i++ { 55 | go func() { 56 | for i := 0; i < b.N; i++ { 57 | q.Get() 58 | } 59 | wg.Done() 60 | }() 61 | } 62 | 63 | wg.Wait() 64 | } 65 | 66 | func BenchmarkRingBufferPaddedMPMC(b *testing.B) { 67 | q := NewRingBufferPadded(uint64(b.N * 100)) 68 | var wg sync.WaitGroup 69 | wg.Add(100) 70 | b.ResetTimer() 71 | 72 | for i := 0; i < 100; i++ { 73 | go func() { 74 | for i := 0; i < b.N; i++ { 75 | q.Put(i) 76 | } 77 | }() 78 | } 79 | 80 | for i := 0; i < 100; i++ { 81 | go func() { 82 | for i := 0; i < b.N; i++ { 83 | q.Get() 84 | } 85 | wg.Done() 86 | }() 87 | } 88 | 89 | wg.Wait() 90 | } 91 | -------------------------------------------------------------------------------- /map_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "strconv" 5 | "sync" 6 | "testing" 7 | 8 | "github.com/Workiva/go-datastructures/trie/ctrie" 9 | ) 10 | 11 | func snapshotMap(m map[string]struct{}, mu *sync.Mutex) map[string]struct{} { 12 | mu.Lock() 13 | snapshot := make(map[string]struct{}, len(m)) 14 | for k, v := range m { 15 | snapshot[k] = v 16 | } 17 | mu.Unlock() 18 | return snapshot 19 | } 20 | 21 | func BenchmarkSnapshotMap(b *testing.B) { 22 | m := make(map[string]struct{}) 23 | for i := 0; i < b.N; i++ { 24 | m[strconv.Itoa(i)] = struct{}{} 25 | } 26 | b.ResetTimer() 27 | 28 | for i := 0; i < b.N; i++ { 29 | snapshot := make(map[string]struct{}, b.N) 30 | for k, v := range m { 31 | snapshot[k] = v 32 | } 33 | } 34 | } 35 | 36 | func BenchmarkSnapshotCtrie(b *testing.B) { 37 | m := ctrie.New(nil) 38 | for i := 0; i < b.N; i++ { 39 | m.Insert([]byte(strconv.Itoa(i)), struct{}{}) 40 | } 41 | b.ResetTimer() 42 | 43 | for i := 0; i < b.N; i++ { 44 | m.Snapshot() 45 | } 46 | } 47 | 48 | func BenchmarkConcurrentSnapshotMap(b *testing.B) { 49 | m := make(map[string]struct{}) 50 | for i := 0; i < b.N; i++ { 51 | m[strconv.Itoa(i)] = struct{}{} 52 | } 53 | mu := &sync.Mutex{} 54 | b.ResetTimer() 55 | 56 | for i := 0; i < b.N; i++ { 57 | var wg sync.WaitGroup 58 | wg.Add(100) 59 | for j := 0; j < 100; j++ { 60 | go func() { 61 | snapshotMap(m, mu) 62 | wg.Done() 63 | }() 64 | } 65 | wg.Wait() 66 | } 67 | } 68 | 69 | func BenchmarkConcurrentSnapshotCtrie(b *testing.B) { 70 | m := ctrie.New(nil) 71 | for i := 0; i < b.N; i++ { 72 | m.Insert([]byte(strconv.Itoa(i)), struct{}{}) 73 | } 74 | b.ResetTimer() 75 | 76 | for i := 0; i < b.N; i++ { 77 | var wg sync.WaitGroup 78 | wg.Add(100) 79 | for j := 0; j < 100; j++ { 80 | go func() { 81 | m.Snapshot() 82 | wg.Done() 83 | }() 84 | } 85 | wg.Wait() 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /channel_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "sync" 5 | "testing" 6 | 7 | "github.com/Workiva/go-datastructures/queue" 8 | ) 9 | 10 | func BenchmarkChannel(b *testing.B) { 11 | ch := make(chan interface{}, 1) 12 | 13 | b.ResetTimer() 14 | go func() { 15 | for i := 0; i < b.N; i++ { 16 | <-ch 17 | } 18 | }() 19 | 20 | for i := 0; i < b.N; i++ { 21 | ch <- `a` 22 | } 23 | } 24 | 25 | func BenchmarkRingBuffer(b *testing.B) { 26 | q := queue.NewRingBuffer(1) 27 | 28 | b.ResetTimer() 29 | go func() { 30 | for i := 0; i < b.N; i++ { 31 | q.Get() 32 | } 33 | }() 34 | 35 | for i := 0; i < b.N; i++ { 36 | q.Put(`a`) 37 | } 38 | } 39 | 40 | func BenchmarkChannelReadContention(b *testing.B) { 41 | ch := make(chan interface{}, 100) 42 | var wg sync.WaitGroup 43 | wg.Add(1000) 44 | b.ResetTimer() 45 | 46 | go func() { 47 | for i := 0; i < b.N; i++ { 48 | ch <- `a` 49 | } 50 | }() 51 | 52 | for i := 0; i < 1000; i++ { 53 | go func() { 54 | for i := 0; i < b.N/1000; i++ { 55 | <-ch 56 | } 57 | wg.Done() 58 | }() 59 | } 60 | 61 | wg.Wait() 62 | } 63 | 64 | func BenchmarkRingBufferReadContention(b *testing.B) { 65 | q := queue.NewRingBuffer(100) 66 | var wg sync.WaitGroup 67 | wg.Add(1000) 68 | b.ResetTimer() 69 | 70 | go func() { 71 | for i := 0; i < b.N; i++ { 72 | q.Put(`a`) 73 | } 74 | }() 75 | 76 | for i := 0; i < 1000; i++ { 77 | go func() { 78 | for i := 0; i < b.N/1000; i++ { 79 | q.Get() 80 | } 81 | wg.Done() 82 | }() 83 | } 84 | 85 | wg.Wait() 86 | } 87 | 88 | func BenchmarkChannelContention(b *testing.B) { 89 | ch := make(chan interface{}, 100) 90 | var wg sync.WaitGroup 91 | wg.Add(1000) 92 | b.ResetTimer() 93 | 94 | for i := 0; i < 1000; i++ { 95 | go func() { 96 | for i := 0; i < b.N; i++ { 97 | ch <- `a` 98 | } 99 | }() 100 | } 101 | 102 | for i := 0; i < 1000; i++ { 103 | go func() { 104 | for i := 0; i < b.N; i++ { 105 | <-ch 106 | } 107 | wg.Done() 108 | }() 109 | } 110 | 111 | wg.Wait() 112 | } 113 | 114 | func BenchmarkRingBufferContention(b *testing.B) { 115 | q := queue.NewRingBuffer(100) 116 | var wg sync.WaitGroup 117 | wg.Add(1000) 118 | b.ResetTimer() 119 | 120 | for i := 0; i < 1000; i++ { 121 | go func() { 122 | for i := 0; i < b.N; i++ { 123 | q.Put(`a`) 124 | } 125 | }() 126 | } 127 | 128 | for i := 0; i < 1000; i++ { 129 | go func() { 130 | for i := 0; i < b.N; i++ { 131 | q.Get() 132 | } 133 | wg.Done() 134 | }() 135 | } 136 | 137 | wg.Wait() 138 | } 139 | -------------------------------------------------------------------------------- /struct_gen_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | // NOTE: THIS FILE WAS PRODUCED BY THE 4 | // MSGP CODE GENERATION TOOL (github.com/tinylib/msgp) 5 | // DO NOT EDIT 6 | 7 | import ( 8 | "bytes" 9 | "testing" 10 | 11 | "github.com/tinylib/msgp/msgp" 12 | ) 13 | 14 | func TestMarshalUnmarshalStruct(t *testing.T) { 15 | v := Struct{} 16 | bts, err := v.MarshalMsg(nil) 17 | if err != nil { 18 | t.Fatal(err) 19 | } 20 | left, err := v.UnmarshalMsg(bts) 21 | if err != nil { 22 | t.Fatal(err) 23 | } 24 | if len(left) > 0 { 25 | t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left) 26 | } 27 | 28 | left, err = msgp.Skip(bts) 29 | if err != nil { 30 | t.Fatal(err) 31 | } 32 | if len(left) > 0 { 33 | t.Errorf("%d bytes left over after Skip(): %q", len(left), left) 34 | } 35 | } 36 | 37 | func BenchmarkMarshalMsgStruct(b *testing.B) { 38 | v := Struct{} 39 | b.ReportAllocs() 40 | b.ResetTimer() 41 | for i := 0; i < b.N; i++ { 42 | v.MarshalMsg(nil) 43 | } 44 | } 45 | 46 | func BenchmarkAppendMsgStruct(b *testing.B) { 47 | v := Struct{} 48 | bts := make([]byte, 0, v.Msgsize()) 49 | bts, _ = v.MarshalMsg(bts[0:0]) 50 | b.SetBytes(int64(len(bts))) 51 | b.ReportAllocs() 52 | b.ResetTimer() 53 | for i := 0; i < b.N; i++ { 54 | bts, _ = v.MarshalMsg(bts[0:0]) 55 | } 56 | } 57 | 58 | func BenchmarkUnmarshalStruct(b *testing.B) { 59 | v := Struct{} 60 | bts, _ := v.MarshalMsg(nil) 61 | b.ReportAllocs() 62 | b.SetBytes(int64(len(bts))) 63 | b.ResetTimer() 64 | for i := 0; i < b.N; i++ { 65 | _, err := v.UnmarshalMsg(bts) 66 | if err != nil { 67 | b.Fatal(err) 68 | } 69 | } 70 | } 71 | 72 | func TestEncodeDecodeStruct(t *testing.T) { 73 | v := Struct{} 74 | var buf bytes.Buffer 75 | msgp.Encode(&buf, &v) 76 | 77 | m := v.Msgsize() 78 | if buf.Len() > m { 79 | t.Logf("WARNING: Msgsize() for %v is inaccurate", v) 80 | } 81 | 82 | vn := Struct{} 83 | err := msgp.Decode(&buf, &vn) 84 | if err != nil { 85 | t.Error(err) 86 | } 87 | 88 | buf.Reset() 89 | msgp.Encode(&buf, &v) 90 | err = msgp.NewReader(&buf).Skip() 91 | if err != nil { 92 | t.Error(err) 93 | } 94 | } 95 | 96 | func BenchmarkEncodeStruct(b *testing.B) { 97 | v := Struct{} 98 | var buf bytes.Buffer 99 | msgp.Encode(&buf, &v) 100 | b.SetBytes(int64(buf.Len())) 101 | en := msgp.NewWriter(msgp.Nowhere) 102 | b.ReportAllocs() 103 | b.ResetTimer() 104 | for i := 0; i < b.N; i++ { 105 | v.EncodeMsg(en) 106 | } 107 | en.Flush() 108 | } 109 | 110 | func BenchmarkDecodeStruct(b *testing.B) { 111 | v := Struct{} 112 | var buf bytes.Buffer 113 | msgp.Encode(&buf, &v) 114 | b.SetBytes(int64(buf.Len())) 115 | rd := msgp.NewEndlessReader(buf.Bytes(), b) 116 | dc := msgp.NewReader(rd) 117 | b.ReportAllocs() 118 | b.ResetTimer() 119 | for i := 0; i < b.N; i++ { 120 | err := v.DecodeMsg(dc) 121 | if err != nil { 122 | b.Fatal(err) 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /reflection_test.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | import ( 4 | "encoding/json" 5 | "sort" 6 | "testing" 7 | ) 8 | 9 | func BenchmarkJSONReflectionMarshal(b *testing.B) { 10 | s := &Struct{ 11 | Field1: "foo", 12 | Field2: 42, 13 | Field3: make([]string, 10), 14 | Field4: 100, 15 | Field5: "bar", 16 | Field6: "baz", 17 | } 18 | b.ResetTimer() 19 | 20 | for i := 0; i < b.N; i++ { 21 | json.Marshal(s) 22 | } 23 | } 24 | 25 | func BenchmarkJSONMarshal(b *testing.B) { 26 | s := &Struct{ 27 | Field1: "foo", 28 | Field2: 42, 29 | Field3: make([]string, 10), 30 | Field4: 100, 31 | Field5: "bar", 32 | Field6: "baz", 33 | } 34 | b.ResetTimer() 35 | 36 | for i := 0; i < b.N; i++ { 37 | s.MarshalJSON() 38 | } 39 | } 40 | 41 | func BenchmarkMsgpackMarshal(b *testing.B) { 42 | s := &Struct{ 43 | Field1: "foo", 44 | Field2: 42, 45 | Field3: make([]string, 10), 46 | Field4: 100, 47 | Field5: "bar", 48 | Field6: "baz", 49 | } 50 | var data []byte 51 | b.ResetTimer() 52 | 53 | for i := 0; i < b.N; i++ { 54 | s.MarshalMsg(data) 55 | } 56 | } 57 | 58 | func BenchmarkJSONReflectionUnmarshal(b *testing.B) { 59 | s := &Struct{ 60 | Field1: "foo", 61 | Field2: 42, 62 | Field3: make([]string, 10), 63 | Field4: 100, 64 | Field5: "bar", 65 | Field6: "baz", 66 | } 67 | data, _ := json.Marshal(s) 68 | b.ResetTimer() 69 | 70 | for i := 0; i < b.N; i++ { 71 | json.Unmarshal(data, &s) 72 | } 73 | } 74 | 75 | func BenchmarkJSONUnmarshal(b *testing.B) { 76 | s := &Struct{ 77 | Field1: "foo", 78 | Field2: 42, 79 | Field3: make([]string, 10), 80 | Field4: 100, 81 | Field5: "bar", 82 | Field6: "baz", 83 | } 84 | data, _ := json.Marshal(s) 85 | b.ResetTimer() 86 | 87 | for i := 0; i < b.N; i++ { 88 | s.UnmarshalJSON(data) 89 | } 90 | } 91 | 92 | func BenchmarkMsgpackUnmarshal(b *testing.B) { 93 | s := &Struct{ 94 | Field1: "foo", 95 | Field2: 42, 96 | Field3: make([]string, 10), 97 | Field4: 100, 98 | Field5: "bar", 99 | Field6: "baz", 100 | } 101 | data, _ := json.Marshal(s) 102 | b.ResetTimer() 103 | 104 | for i := 0; i < b.N; i++ { 105 | s.UnmarshalMsg(data) 106 | } 107 | } 108 | 109 | func BenchmarkJSONReflectionMarshalIface(b *testing.B) { 110 | var s Iface = &Struct{ 111 | Field1: "foo", 112 | Field2: 42, 113 | Field3: make([]string, 10), 114 | Field4: 100, 115 | Field5: "bar", 116 | Field6: "baz", 117 | } 118 | b.ResetTimer() 119 | 120 | for i := 0; i < b.N; i++ { 121 | json.Marshal(s) 122 | } 123 | } 124 | 125 | func BenchmarkJSONReflectionUnmarshalIface(b *testing.B) { 126 | var s Iface = &Struct{ 127 | Field1: "foo", 128 | Field2: 42, 129 | Field3: make([]string, 10), 130 | Field4: 100, 131 | Field5: "bar", 132 | Field6: "baz", 133 | } 134 | data, _ := json.Marshal(s) 135 | b.ResetTimer() 136 | 137 | for i := 0; i < b.N; i++ { 138 | json.Unmarshal(data, &s) 139 | } 140 | } 141 | 142 | func BenchmarkStructMethodCall(b *testing.B) { 143 | s := &Struct{} 144 | for i := 0; i < b.N; i++ { 145 | s.Foo() 146 | } 147 | } 148 | 149 | func BenchmarkIfaceMethodCall(b *testing.B) { 150 | var s Iface = &Struct{} 151 | for i := 0; i < b.N; i++ { 152 | s.Foo() 153 | } 154 | } 155 | 156 | type SortableIface interface { 157 | Number() int 158 | } 159 | 160 | type Sortable struct { 161 | number int 162 | } 163 | 164 | func (s Sortable) Number() int { 165 | return s.number 166 | } 167 | 168 | type SortableIfaceByNumber []SortableIface 169 | 170 | func (a SortableIfaceByNumber) Len() int { return len(a) } 171 | func (a SortableIfaceByNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 172 | func (a SortableIfaceByNumber) Less(i, j int) bool { return a[i].Number() < a[j].Number() } 173 | 174 | type SortableByNumber []Sortable 175 | 176 | func (a SortableByNumber) Len() int { return len(a) } 177 | func (a SortableByNumber) Swap(i, j int) { a[i], a[j] = a[j], a[i] } 178 | func (a SortableByNumber) Less(i, j int) bool { return a[i].Number() < a[j].Number() } 179 | 180 | func BenchmarkSortStruct(b *testing.B) { 181 | s := make(SortableByNumber, 1000000) 182 | for i := 0; i < 1000000; i++ { 183 | s[i] = Sortable{i} 184 | } 185 | b.ResetTimer() 186 | 187 | for i := 0; i < b.N; i++ { 188 | sort.Sort(s) 189 | } 190 | } 191 | 192 | func BenchmarkSortIface(b *testing.B) { 193 | s := make(SortableIfaceByNumber, 1000000) 194 | for i := 0; i < 1000000; i++ { 195 | s[i] = Sortable{i} 196 | } 197 | b.ResetTimer() 198 | 199 | for i := 0; i < b.N; i++ { 200 | sort.Sort(s) 201 | } 202 | } 203 | -------------------------------------------------------------------------------- /ring_padded.go: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright 2014 Workiva, LLC 3 | 4 | Licensed under the Apache License, Version 2.0 (the "License"); 5 | you may not use this file except in compliance with the License. 6 | You may obtain a copy of the License at 7 | 8 | http://www.apache.org/licenses/LICENSE-2.0 9 | 10 | Unless required by applicable law or agreed to in writing, software 11 | distributed under the License is distributed on an "AS IS" BASIS, 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | See the License for the specific language governing permissions and 14 | limitations under the License. 15 | */ 16 | 17 | package test 18 | 19 | import ( 20 | "runtime" 21 | "sync/atomic" 22 | 23 | "github.com/Workiva/go-datastructures/queue" 24 | ) 25 | 26 | // roundUp takes a uint64 greater than 0 and rounds it up to the next 27 | // power of 2. 28 | func roundUp(v uint64) uint64 { 29 | v-- 30 | v |= v >> 1 31 | v |= v >> 2 32 | v |= v >> 4 33 | v |= v >> 8 34 | v |= v >> 16 35 | v |= v >> 32 36 | v++ 37 | return v 38 | } 39 | 40 | type node struct { 41 | position uint64 42 | data interface{} 43 | } 44 | 45 | type nodes []*node 46 | 47 | // RingBufferPadded is a MPMC buffer that achieves threadsafety with CAS 48 | // operations only. A put on full or get on empty call will block until an 49 | // item is put or retrieved. Calling Dispose on the RingBufferPadded will 50 | // unblock any blocked threads with an error. This buffer is similar to the 51 | // buffer described here: 52 | // http://www.1024cores.net/home/lock-free-algorithms/queues/bounded-mpmc-queue 53 | // with some minor additions. 54 | type RingBufferPadded struct { 55 | _padding0 [8]uint64 56 | queue uint64 57 | _padding1 [8]uint64 58 | dequeue uint64 59 | _padding2 [8]uint64 60 | mask, disposed uint64 61 | _padding3 [8]uint64 62 | nodes nodes 63 | } 64 | 65 | func (rb *RingBufferPadded) init(size uint64) { 66 | size = roundUp(size) 67 | rb.nodes = make(nodes, size) 68 | for i := uint64(0); i < size; i++ { 69 | rb.nodes[i] = &node{position: i} 70 | } 71 | rb.mask = size - 1 // so we don't have to do this with every put/get operation 72 | } 73 | 74 | // Put adds the provided item to the queue. If the queue is full, this 75 | // call will block until an item is added to the queue or Dispose is called 76 | // on the queue. An error will be returned if the queue is disposed. 77 | func (rb *RingBufferPadded) Put(item interface{}) error { 78 | _, err := rb.put(item, false) 79 | return err 80 | } 81 | 82 | // Offer adds the provided item to the queue if there is space. If the queue 83 | // is full, this call will return false. An error will be returned if the 84 | // queue is disposed. 85 | func (rb *RingBufferPadded) Offer(item interface{}) (bool, error) { 86 | return rb.put(item, true) 87 | } 88 | 89 | func (rb *RingBufferPadded) put(item interface{}, offer bool) (bool, error) { 90 | var n *node 91 | pos := atomic.LoadUint64(&rb.queue) 92 | i := 0 93 | L: 94 | for { 95 | if atomic.LoadUint64(&rb.disposed) == 1 { 96 | return false, queue.ErrDisposed 97 | } 98 | 99 | n = rb.nodes[pos&rb.mask] 100 | seq := atomic.LoadUint64(&n.position) 101 | switch dif := seq - pos; { 102 | case dif == 0: 103 | if atomic.CompareAndSwapUint64(&rb.queue, pos, pos+1) { 104 | break L 105 | } 106 | case dif < 0: 107 | panic(`Ring buffer in a compromised state during a put operation.`) 108 | default: 109 | pos = atomic.LoadUint64(&rb.queue) 110 | } 111 | 112 | if offer { 113 | return false, nil 114 | } 115 | 116 | if i == 10000 { 117 | runtime.Gosched() // free up the cpu before the next iteration 118 | i = 0 119 | } else { 120 | i++ 121 | } 122 | } 123 | 124 | n.data = item 125 | atomic.StoreUint64(&n.position, pos+1) 126 | return true, nil 127 | } 128 | 129 | // Get will return the next item in the queue. This call will block 130 | // if the queue is empty. This call will unblock when an item is added 131 | // to the queue or Dispose is called on the queue. An error will be returned 132 | // if the queue is disposed. 133 | func (rb *RingBufferPadded) Get() (interface{}, error) { 134 | var n *node 135 | pos := atomic.LoadUint64(&rb.dequeue) 136 | i := 0 137 | L: 138 | for { 139 | if atomic.LoadUint64(&rb.disposed) == 1 { 140 | return nil, queue.ErrDisposed 141 | } 142 | 143 | n = rb.nodes[pos&rb.mask] 144 | seq := atomic.LoadUint64(&n.position) 145 | switch dif := seq - (pos + 1); { 146 | case dif == 0: 147 | if atomic.CompareAndSwapUint64(&rb.dequeue, pos, pos+1) { 148 | break L 149 | } 150 | case dif < 0: 151 | panic(`Ring buffer in compromised state during a get operation.`) 152 | default: 153 | pos = atomic.LoadUint64(&rb.dequeue) 154 | } 155 | 156 | if i == 10000 { 157 | runtime.Gosched() // free up the cpu before the next iteration 158 | i = 0 159 | } else { 160 | i++ 161 | } 162 | } 163 | data := n.data 164 | n.data = nil 165 | atomic.StoreUint64(&n.position, pos+rb.mask+1) 166 | return data, nil 167 | } 168 | 169 | // Len returns the number of items in the queue. 170 | func (rb *RingBufferPadded) Len() uint64 { 171 | return atomic.LoadUint64(&rb.queue) - atomic.LoadUint64(&rb.dequeue) 172 | } 173 | 174 | // Cap returns the capacity of this ring buffer. 175 | func (rb *RingBufferPadded) Cap() uint64 { 176 | return uint64(len(rb.nodes)) 177 | } 178 | 179 | // Dispose will dispose of this queue and free any blocked threads 180 | // in the Put and/or Get methods. Calling those methods on a disposed 181 | // queue will return an error. 182 | func (rb *RingBufferPadded) Dispose() { 183 | atomic.CompareAndSwapUint64(&rb.disposed, 0, 1) 184 | } 185 | 186 | // IsDisposed will return a bool indicating if this queue has been 187 | // disposed. 188 | func (rb *RingBufferPadded) IsDisposed() bool { 189 | return atomic.LoadUint64(&rb.disposed) == 1 190 | } 191 | 192 | // NewRingBufferPadded will allocate, initialize, and return a ring buffer 193 | // with the specified size. 194 | func NewRingBufferPadded(size uint64) *RingBufferPadded { 195 | rb := &RingBufferPadded{} 196 | rb.init(size) 197 | return rb 198 | } 199 | -------------------------------------------------------------------------------- /struct_gen.go: -------------------------------------------------------------------------------- 1 | package test 2 | 3 | // NOTE: THIS FILE WAS PRODUCED BY THE 4 | // MSGP CODE GENERATION TOOL (github.com/tinylib/msgp) 5 | // DO NOT EDIT 6 | 7 | import ( 8 | "github.com/tinylib/msgp/msgp" 9 | ) 10 | 11 | // DecodeMsg implements msgp.Decodable 12 | func (z *Struct) DecodeMsg(dc *msgp.Reader) (err error) { 13 | var field []byte 14 | _ = field 15 | var isz uint32 16 | isz, err = dc.ReadMapHeader() 17 | if err != nil { 18 | return 19 | } 20 | for isz > 0 { 21 | isz-- 22 | field, err = dc.ReadMapKeyPtr() 23 | if err != nil { 24 | return 25 | } 26 | switch msgp.UnsafeString(field) { 27 | case "Field1": 28 | z.Field1, err = dc.ReadString() 29 | if err != nil { 30 | return 31 | } 32 | case "Field2": 33 | z.Field2, err = dc.ReadInt() 34 | if err != nil { 35 | return 36 | } 37 | case "Field3": 38 | var xsz uint32 39 | xsz, err = dc.ReadArrayHeader() 40 | if err != nil { 41 | return 42 | } 43 | if cap(z.Field3) >= int(xsz) { 44 | z.Field3 = z.Field3[:xsz] 45 | } else { 46 | z.Field3 = make([]string, xsz) 47 | } 48 | for xvk := range z.Field3 { 49 | z.Field3[xvk], err = dc.ReadString() 50 | if err != nil { 51 | return 52 | } 53 | } 54 | case "Field4": 55 | z.Field4, err = dc.ReadUint64() 56 | if err != nil { 57 | return 58 | } 59 | case "Field5": 60 | z.Field5, err = dc.ReadString() 61 | if err != nil { 62 | return 63 | } 64 | case "Field6": 65 | z.Field6, err = dc.ReadString() 66 | if err != nil { 67 | return 68 | } 69 | default: 70 | err = dc.Skip() 71 | if err != nil { 72 | return 73 | } 74 | } 75 | } 76 | return 77 | } 78 | 79 | // EncodeMsg implements msgp.Encodable 80 | func (z *Struct) EncodeMsg(en *msgp.Writer) (err error) { 81 | // map header, size 6 82 | // write "Field1" 83 | err = en.Append(0x86, 0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x31) 84 | if err != nil { 85 | return err 86 | } 87 | err = en.WriteString(z.Field1) 88 | if err != nil { 89 | return 90 | } 91 | // write "Field2" 92 | err = en.Append(0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x32) 93 | if err != nil { 94 | return err 95 | } 96 | err = en.WriteInt(z.Field2) 97 | if err != nil { 98 | return 99 | } 100 | // write "Field3" 101 | err = en.Append(0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x33) 102 | if err != nil { 103 | return err 104 | } 105 | err = en.WriteArrayHeader(uint32(len(z.Field3))) 106 | if err != nil { 107 | return 108 | } 109 | for xvk := range z.Field3 { 110 | err = en.WriteString(z.Field3[xvk]) 111 | if err != nil { 112 | return 113 | } 114 | } 115 | // write "Field4" 116 | err = en.Append(0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x34) 117 | if err != nil { 118 | return err 119 | } 120 | err = en.WriteUint64(z.Field4) 121 | if err != nil { 122 | return 123 | } 124 | // write "Field5" 125 | err = en.Append(0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x35) 126 | if err != nil { 127 | return err 128 | } 129 | err = en.WriteString(z.Field5) 130 | if err != nil { 131 | return 132 | } 133 | // write "Field6" 134 | err = en.Append(0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x36) 135 | if err != nil { 136 | return err 137 | } 138 | err = en.WriteString(z.Field6) 139 | if err != nil { 140 | return 141 | } 142 | return 143 | } 144 | 145 | // MarshalMsg implements msgp.Marshaler 146 | func (z *Struct) MarshalMsg(b []byte) (o []byte, err error) { 147 | o = msgp.Require(b, z.Msgsize()) 148 | // map header, size 6 149 | // string "Field1" 150 | o = append(o, 0x86, 0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x31) 151 | o = msgp.AppendString(o, z.Field1) 152 | // string "Field2" 153 | o = append(o, 0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x32) 154 | o = msgp.AppendInt(o, z.Field2) 155 | // string "Field3" 156 | o = append(o, 0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x33) 157 | o = msgp.AppendArrayHeader(o, uint32(len(z.Field3))) 158 | for xvk := range z.Field3 { 159 | o = msgp.AppendString(o, z.Field3[xvk]) 160 | } 161 | // string "Field4" 162 | o = append(o, 0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x34) 163 | o = msgp.AppendUint64(o, z.Field4) 164 | // string "Field5" 165 | o = append(o, 0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x35) 166 | o = msgp.AppendString(o, z.Field5) 167 | // string "Field6" 168 | o = append(o, 0xa6, 0x46, 0x69, 0x65, 0x6c, 0x64, 0x36) 169 | o = msgp.AppendString(o, z.Field6) 170 | return 171 | } 172 | 173 | // UnmarshalMsg implements msgp.Unmarshaler 174 | func (z *Struct) UnmarshalMsg(bts []byte) (o []byte, err error) { 175 | var field []byte 176 | _ = field 177 | var isz uint32 178 | isz, bts, err = msgp.ReadMapHeaderBytes(bts) 179 | if err != nil { 180 | return 181 | } 182 | for isz > 0 { 183 | isz-- 184 | field, bts, err = msgp.ReadMapKeyZC(bts) 185 | if err != nil { 186 | return 187 | } 188 | switch msgp.UnsafeString(field) { 189 | case "Field1": 190 | z.Field1, bts, err = msgp.ReadStringBytes(bts) 191 | if err != nil { 192 | return 193 | } 194 | case "Field2": 195 | z.Field2, bts, err = msgp.ReadIntBytes(bts) 196 | if err != nil { 197 | return 198 | } 199 | case "Field3": 200 | var xsz uint32 201 | xsz, bts, err = msgp.ReadArrayHeaderBytes(bts) 202 | if err != nil { 203 | return 204 | } 205 | if cap(z.Field3) >= int(xsz) { 206 | z.Field3 = z.Field3[:xsz] 207 | } else { 208 | z.Field3 = make([]string, xsz) 209 | } 210 | for xvk := range z.Field3 { 211 | z.Field3[xvk], bts, err = msgp.ReadStringBytes(bts) 212 | if err != nil { 213 | return 214 | } 215 | } 216 | case "Field4": 217 | z.Field4, bts, err = msgp.ReadUint64Bytes(bts) 218 | if err != nil { 219 | return 220 | } 221 | case "Field5": 222 | z.Field5, bts, err = msgp.ReadStringBytes(bts) 223 | if err != nil { 224 | return 225 | } 226 | case "Field6": 227 | z.Field6, bts, err = msgp.ReadStringBytes(bts) 228 | if err != nil { 229 | return 230 | } 231 | default: 232 | bts, err = msgp.Skip(bts) 233 | if err != nil { 234 | return 235 | } 236 | } 237 | } 238 | o = bts 239 | return 240 | } 241 | 242 | func (z *Struct) Msgsize() (s int) { 243 | s = 1 + 7 + msgp.StringPrefixSize + len(z.Field1) + 7 + msgp.IntSize + 7 + msgp.ArrayHeaderSize 244 | for xvk := range z.Field3 { 245 | s += msgp.StringPrefixSize + len(z.Field3[xvk]) 246 | } 247 | s += 7 + msgp.Uint64Size + 7 + msgp.StringPrefixSize + len(z.Field5) + 7 + msgp.StringPrefixSize + len(z.Field6) 248 | return 249 | } 250 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /struct_ffjson.go: -------------------------------------------------------------------------------- 1 | // DO NOT EDIT! 2 | // Code generated by ffjson 3 | // source: struct.go 4 | // DO NOT EDIT! 5 | 6 | package test 7 | 8 | import ( 9 | "bytes" 10 | "fmt" 11 | fflib "github.com/pquerna/ffjson/fflib/v1" 12 | ) 13 | 14 | func (mj *Struct) MarshalJSON() ([]byte, error) { 15 | var buf fflib.Buffer 16 | if mj == nil { 17 | buf.WriteString("null") 18 | return buf.Bytes(), nil 19 | } 20 | err := mj.MarshalJSONBuf(&buf) 21 | if err != nil { 22 | return nil, err 23 | } 24 | return buf.Bytes(), nil 25 | } 26 | func (mj *Struct) MarshalJSONBuf(buf fflib.EncodingBuffer) error { 27 | if mj == nil { 28 | buf.WriteString("null") 29 | return nil 30 | } 31 | var err error 32 | var obj []byte 33 | _ = obj 34 | _ = err 35 | buf.WriteString(`{"Field1":`) 36 | fflib.WriteJsonString(buf, string(mj.Field1)) 37 | buf.WriteString(`,"Field2":`) 38 | fflib.FormatBits2(buf, uint64(mj.Field2), 10, mj.Field2 < 0) 39 | buf.WriteString(`,"Field3":`) 40 | if mj.Field3 != nil { 41 | buf.WriteString(`[`) 42 | for i, v := range mj.Field3 { 43 | if i != 0 { 44 | buf.WriteString(`,`) 45 | } 46 | fflib.WriteJsonString(buf, string(v)) 47 | } 48 | buf.WriteString(`]`) 49 | } else { 50 | buf.WriteString(`null`) 51 | } 52 | buf.WriteString(`,"Field4":`) 53 | fflib.FormatBits2(buf, uint64(mj.Field4), 10, false) 54 | buf.WriteString(`,"Field5":`) 55 | fflib.WriteJsonString(buf, string(mj.Field5)) 56 | buf.WriteString(`,"Field6":`) 57 | fflib.WriteJsonString(buf, string(mj.Field6)) 58 | buf.WriteByte('}') 59 | return nil 60 | } 61 | 62 | const ( 63 | ffj_t_Structbase = iota 64 | ffj_t_Structno_such_key 65 | 66 | ffj_t_Struct_Field1 67 | 68 | ffj_t_Struct_Field2 69 | 70 | ffj_t_Struct_Field3 71 | 72 | ffj_t_Struct_Field4 73 | 74 | ffj_t_Struct_Field5 75 | 76 | ffj_t_Struct_Field6 77 | ) 78 | 79 | var ffj_key_Struct_Field1 = []byte("Field1") 80 | 81 | var ffj_key_Struct_Field2 = []byte("Field2") 82 | 83 | var ffj_key_Struct_Field3 = []byte("Field3") 84 | 85 | var ffj_key_Struct_Field4 = []byte("Field4") 86 | 87 | var ffj_key_Struct_Field5 = []byte("Field5") 88 | 89 | var ffj_key_Struct_Field6 = []byte("Field6") 90 | 91 | func (uj *Struct) UnmarshalJSON(input []byte) error { 92 | fs := fflib.NewFFLexer(input) 93 | return uj.UnmarshalJSONFFLexer(fs, fflib.FFParse_map_start) 94 | } 95 | 96 | func (uj *Struct) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error { 97 | var err error = nil 98 | currentKey := ffj_t_Structbase 99 | _ = currentKey 100 | tok := fflib.FFTok_init 101 | wantedTok := fflib.FFTok_init 102 | 103 | mainparse: 104 | for { 105 | tok = fs.Scan() 106 | // println(fmt.Sprintf("debug: tok: %v state: %v", tok, state)) 107 | if tok == fflib.FFTok_error { 108 | goto tokerror 109 | } 110 | 111 | switch state { 112 | 113 | case fflib.FFParse_map_start: 114 | if tok != fflib.FFTok_left_bracket { 115 | wantedTok = fflib.FFTok_left_bracket 116 | goto wrongtokenerror 117 | } 118 | state = fflib.FFParse_want_key 119 | continue 120 | 121 | case fflib.FFParse_after_value: 122 | if tok == fflib.FFTok_comma { 123 | state = fflib.FFParse_want_key 124 | } else if tok == fflib.FFTok_right_bracket { 125 | goto done 126 | } else { 127 | wantedTok = fflib.FFTok_comma 128 | goto wrongtokenerror 129 | } 130 | 131 | case fflib.FFParse_want_key: 132 | // json {} ended. goto exit. woo. 133 | if tok == fflib.FFTok_right_bracket { 134 | goto done 135 | } 136 | if tok != fflib.FFTok_string { 137 | wantedTok = fflib.FFTok_string 138 | goto wrongtokenerror 139 | } 140 | 141 | kn := fs.Output.Bytes() 142 | if len(kn) <= 0 { 143 | // "" case. hrm. 144 | currentKey = ffj_t_Structno_such_key 145 | state = fflib.FFParse_want_colon 146 | goto mainparse 147 | } else { 148 | switch kn[0] { 149 | 150 | case 'F': 151 | 152 | if bytes.Equal(ffj_key_Struct_Field1, kn) { 153 | currentKey = ffj_t_Struct_Field1 154 | state = fflib.FFParse_want_colon 155 | goto mainparse 156 | 157 | } else if bytes.Equal(ffj_key_Struct_Field2, kn) { 158 | currentKey = ffj_t_Struct_Field2 159 | state = fflib.FFParse_want_colon 160 | goto mainparse 161 | 162 | } else if bytes.Equal(ffj_key_Struct_Field3, kn) { 163 | currentKey = ffj_t_Struct_Field3 164 | state = fflib.FFParse_want_colon 165 | goto mainparse 166 | 167 | } else if bytes.Equal(ffj_key_Struct_Field4, kn) { 168 | currentKey = ffj_t_Struct_Field4 169 | state = fflib.FFParse_want_colon 170 | goto mainparse 171 | 172 | } else if bytes.Equal(ffj_key_Struct_Field5, kn) { 173 | currentKey = ffj_t_Struct_Field5 174 | state = fflib.FFParse_want_colon 175 | goto mainparse 176 | 177 | } else if bytes.Equal(ffj_key_Struct_Field6, kn) { 178 | currentKey = ffj_t_Struct_Field6 179 | state = fflib.FFParse_want_colon 180 | goto mainparse 181 | } 182 | 183 | } 184 | 185 | if fflib.AsciiEqualFold(ffj_key_Struct_Field6, kn) { 186 | currentKey = ffj_t_Struct_Field6 187 | state = fflib.FFParse_want_colon 188 | goto mainparse 189 | } 190 | 191 | if fflib.AsciiEqualFold(ffj_key_Struct_Field5, kn) { 192 | currentKey = ffj_t_Struct_Field5 193 | state = fflib.FFParse_want_colon 194 | goto mainparse 195 | } 196 | 197 | if fflib.AsciiEqualFold(ffj_key_Struct_Field4, kn) { 198 | currentKey = ffj_t_Struct_Field4 199 | state = fflib.FFParse_want_colon 200 | goto mainparse 201 | } 202 | 203 | if fflib.AsciiEqualFold(ffj_key_Struct_Field3, kn) { 204 | currentKey = ffj_t_Struct_Field3 205 | state = fflib.FFParse_want_colon 206 | goto mainparse 207 | } 208 | 209 | if fflib.AsciiEqualFold(ffj_key_Struct_Field2, kn) { 210 | currentKey = ffj_t_Struct_Field2 211 | state = fflib.FFParse_want_colon 212 | goto mainparse 213 | } 214 | 215 | if fflib.AsciiEqualFold(ffj_key_Struct_Field1, kn) { 216 | currentKey = ffj_t_Struct_Field1 217 | state = fflib.FFParse_want_colon 218 | goto mainparse 219 | } 220 | 221 | currentKey = ffj_t_Structno_such_key 222 | state = fflib.FFParse_want_colon 223 | goto mainparse 224 | } 225 | 226 | case fflib.FFParse_want_colon: 227 | if tok != fflib.FFTok_colon { 228 | wantedTok = fflib.FFTok_colon 229 | goto wrongtokenerror 230 | } 231 | state = fflib.FFParse_want_value 232 | continue 233 | case fflib.FFParse_want_value: 234 | 235 | if tok == fflib.FFTok_left_brace || tok == fflib.FFTok_left_bracket || tok == fflib.FFTok_integer || tok == fflib.FFTok_double || tok == fflib.FFTok_string || tok == fflib.FFTok_bool || tok == fflib.FFTok_null { 236 | switch currentKey { 237 | 238 | case ffj_t_Struct_Field1: 239 | goto handle_Field1 240 | 241 | case ffj_t_Struct_Field2: 242 | goto handle_Field2 243 | 244 | case ffj_t_Struct_Field3: 245 | goto handle_Field3 246 | 247 | case ffj_t_Struct_Field4: 248 | goto handle_Field4 249 | 250 | case ffj_t_Struct_Field5: 251 | goto handle_Field5 252 | 253 | case ffj_t_Struct_Field6: 254 | goto handle_Field6 255 | 256 | case ffj_t_Structno_such_key: 257 | err = fs.SkipField(tok) 258 | if err != nil { 259 | return fs.WrapErr(err) 260 | } 261 | state = fflib.FFParse_after_value 262 | goto mainparse 263 | } 264 | } else { 265 | goto wantedvalue 266 | } 267 | } 268 | } 269 | 270 | handle_Field1: 271 | 272 | /* handler: uj.Field1 type=string kind=string quoted=false*/ 273 | 274 | { 275 | 276 | { 277 | if tok != fflib.FFTok_string && tok != fflib.FFTok_null { 278 | return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok)) 279 | } 280 | } 281 | 282 | if tok == fflib.FFTok_null { 283 | 284 | } else { 285 | 286 | outBuf := fs.Output.Bytes() 287 | 288 | uj.Field1 = string(string(outBuf)) 289 | 290 | } 291 | } 292 | 293 | state = fflib.FFParse_after_value 294 | goto mainparse 295 | 296 | handle_Field2: 297 | 298 | /* handler: uj.Field2 type=int kind=int quoted=false*/ 299 | 300 | { 301 | if tok != fflib.FFTok_integer && tok != fflib.FFTok_null { 302 | return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for int", tok)) 303 | } 304 | } 305 | 306 | { 307 | 308 | if tok == fflib.FFTok_null { 309 | 310 | } else { 311 | 312 | tval, err := fflib.ParseInt(fs.Output.Bytes(), 10, 64) 313 | 314 | if err != nil { 315 | return fs.WrapErr(err) 316 | } 317 | 318 | uj.Field2 = int(tval) 319 | 320 | } 321 | } 322 | 323 | state = fflib.FFParse_after_value 324 | goto mainparse 325 | 326 | handle_Field3: 327 | 328 | /* handler: uj.Field3 type=[]string kind=slice quoted=false*/ 329 | 330 | { 331 | 332 | { 333 | if tok != fflib.FFTok_left_brace && tok != fflib.FFTok_null { 334 | return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for ", tok)) 335 | } 336 | } 337 | 338 | if tok == fflib.FFTok_null { 339 | uj.Field3 = nil 340 | } else { 341 | 342 | uj.Field3 = make([]string, 0) 343 | 344 | wantVal := true 345 | 346 | for { 347 | 348 | var tmp_uj__Field3 string 349 | 350 | tok = fs.Scan() 351 | if tok == fflib.FFTok_error { 352 | goto tokerror 353 | } 354 | if tok == fflib.FFTok_right_brace { 355 | break 356 | } 357 | 358 | if tok == fflib.FFTok_comma { 359 | if wantVal == true { 360 | // TODO(pquerna): this isn't an ideal error message, this handles 361 | // things like [,,,] as an array value. 362 | return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok)) 363 | } 364 | continue 365 | } else { 366 | wantVal = true 367 | } 368 | 369 | /* handler: tmp_uj__Field3 type=string kind=string quoted=false*/ 370 | 371 | { 372 | 373 | { 374 | if tok != fflib.FFTok_string && tok != fflib.FFTok_null { 375 | return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok)) 376 | } 377 | } 378 | 379 | if tok == fflib.FFTok_null { 380 | 381 | } else { 382 | 383 | outBuf := fs.Output.Bytes() 384 | 385 | tmp_uj__Field3 = string(string(outBuf)) 386 | 387 | } 388 | } 389 | 390 | uj.Field3 = append(uj.Field3, tmp_uj__Field3) 391 | wantVal = false 392 | } 393 | } 394 | } 395 | 396 | state = fflib.FFParse_after_value 397 | goto mainparse 398 | 399 | handle_Field4: 400 | 401 | /* handler: uj.Field4 type=uint64 kind=uint64 quoted=false*/ 402 | 403 | { 404 | if tok != fflib.FFTok_integer && tok != fflib.FFTok_null { 405 | return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for uint64", tok)) 406 | } 407 | } 408 | 409 | { 410 | 411 | if tok == fflib.FFTok_null { 412 | 413 | } else { 414 | 415 | tval, err := fflib.ParseUint(fs.Output.Bytes(), 10, 64) 416 | 417 | if err != nil { 418 | return fs.WrapErr(err) 419 | } 420 | 421 | uj.Field4 = uint64(tval) 422 | 423 | } 424 | } 425 | 426 | state = fflib.FFParse_after_value 427 | goto mainparse 428 | 429 | handle_Field5: 430 | 431 | /* handler: uj.Field5 type=string kind=string quoted=false*/ 432 | 433 | { 434 | 435 | { 436 | if tok != fflib.FFTok_string && tok != fflib.FFTok_null { 437 | return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok)) 438 | } 439 | } 440 | 441 | if tok == fflib.FFTok_null { 442 | 443 | } else { 444 | 445 | outBuf := fs.Output.Bytes() 446 | 447 | uj.Field5 = string(string(outBuf)) 448 | 449 | } 450 | } 451 | 452 | state = fflib.FFParse_after_value 453 | goto mainparse 454 | 455 | handle_Field6: 456 | 457 | /* handler: uj.Field6 type=string kind=string quoted=false*/ 458 | 459 | { 460 | 461 | { 462 | if tok != fflib.FFTok_string && tok != fflib.FFTok_null { 463 | return fs.WrapErr(fmt.Errorf("cannot unmarshal %s into Go value for string", tok)) 464 | } 465 | } 466 | 467 | if tok == fflib.FFTok_null { 468 | 469 | } else { 470 | 471 | outBuf := fs.Output.Bytes() 472 | 473 | uj.Field6 = string(string(outBuf)) 474 | 475 | } 476 | } 477 | 478 | state = fflib.FFParse_after_value 479 | goto mainparse 480 | 481 | wantedvalue: 482 | return fs.WrapErr(fmt.Errorf("wanted value token, but got token: %v", tok)) 483 | wrongtokenerror: 484 | return fs.WrapErr(fmt.Errorf("ffjson: wanted token: %v, but got token: %v output=%s", wantedTok, tok, fs.Output.String())) 485 | tokerror: 486 | if fs.BigError != nil { 487 | return fs.WrapErr(fs.BigError) 488 | } 489 | err = fs.Error.ToError() 490 | if err != nil { 491 | return fs.WrapErr(err) 492 | } 493 | panic("ffjson-generated: unreachable, please report bug.") 494 | done: 495 | return nil 496 | } 497 | --------------------------------------------------------------------------------