-
Notifications
You must be signed in to change notification settings - Fork 2
/
string.go
174 lines (143 loc) · 4.24 KB
/
string.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package plenccodec
import (
"sync"
"sync/atomic"
"unsafe"
"github.com/philpearl/plenc/plenccore"
)
// StringCodec is a codec for an string
type StringCodec struct{}
// size returns the number of bytes needed to encode a string
func (StringCodec) size(ptr unsafe.Pointer) int {
return len(*(*string)(ptr))
}
// append encodes a string
func (StringCodec) append(data []byte, ptr unsafe.Pointer) []byte {
s := *(*string)(ptr)
return append(data, s...)
}
// Read decodes a string
func (StringCodec) Read(data []byte, ptr unsafe.Pointer, wt plenccore.WireType) (n int, err error) {
*(*string)(ptr) = string(data)
return len(data), nil
}
// New creates a pointer to a new string header
func (StringCodec) New() unsafe.Pointer {
return unsafe.Pointer(new(string))
}
// WireType returns the wire type used to encode this type
func (StringCodec) WireType() plenccore.WireType {
return plenccore.WTLength
}
// Omit indicates whether this field should be omitted
func (StringCodec) Omit(ptr unsafe.Pointer) bool {
return len(*(*string)(ptr)) == 0
}
func (StringCodec) Descriptor() Descriptor {
return Descriptor{Type: FieldTypeString}
}
func (c StringCodec) Size(ptr unsafe.Pointer, tag []byte) int {
l := c.size(ptr)
if len(tag) > 0 {
l += len(tag) + plenccore.SizeVarUint(uint64(l))
}
return l
}
func (c StringCodec) Append(data []byte, ptr unsafe.Pointer, tag []byte) []byte {
if len(tag) != 0 {
data = append(data, tag...)
data = plenccore.AppendVarUint(data, uint64(c.size(ptr)))
}
return c.append(data, ptr)
}
// BytesCodec is a codec for a byte slice
type BytesCodec struct{}
// size returns the number of bytes needed to encode a string
func (BytesCodec) size(ptr unsafe.Pointer) int {
return len(*(*[]byte)(ptr))
}
// append encodes a []byte
func (BytesCodec) append(data []byte, ptr unsafe.Pointer) []byte {
s := *(*[]byte)(ptr)
return append(data, s...)
}
// Read decodes a []byte
func (BytesCodec) Read(data []byte, ptr unsafe.Pointer, wt plenccore.WireType) (n int, err error) {
// really must copy this data to be safe from the underlying buffer changing
// later
*(*[]byte)(ptr) = append([]byte(nil), data...)
return len(data), nil
}
// New creates a pointer to a new bool
func (c BytesCodec) New() unsafe.Pointer {
return unsafe.Pointer(new([]byte))
}
// WireType returns the wire type used to encode this type
func (c BytesCodec) WireType() plenccore.WireType {
return plenccore.WTLength
}
// Omit indicates whether this field should be omitted
func (c BytesCodec) Omit(ptr unsafe.Pointer) bool {
return len(*(*[]byte)(ptr)) == 0
}
func (BytesCodec) Descriptor() Descriptor {
return Descriptor{Type: FieldTypeString}
}
func (c BytesCodec) Size(ptr unsafe.Pointer, tag []byte) int {
l := c.size(ptr)
if len(tag) != 0 {
l += len(tag) + plenccore.SizeVarUint(uint64(l))
}
return l
}
func (c BytesCodec) Append(data []byte, ptr unsafe.Pointer, tag []byte) []byte {
if len(tag) != 0 {
data = append(data, tag...)
data = plenccore.AppendVarUint(data, uint64(c.size(ptr)))
}
return c.append(data, ptr)
}
type Interner interface {
WithInterning() Codec
}
type InternedStringCodec struct {
sync.Mutex
strings unsafe.Pointer
StringCodec
}
func (c *InternedStringCodec) Read(data []byte, ptr unsafe.Pointer, wt plenccore.WireType) (n int, err error) {
p := atomic.LoadPointer(&c.strings)
m := *(*map[string]string)((unsafe.Pointer)(&p))
s, ok := m[string(data)]
if !ok {
s = c.addString(data)
}
*(*string)(ptr) = s
return len(data), nil
}
func (c *InternedStringCodec) addString(data []byte) string {
c.Lock()
defer c.Unlock()
p := atomic.LoadPointer(&c.strings)
m := *(*map[string]string)((unsafe.Pointer)(&p))
s, ok := m[string(data)]
if !ok {
// We completely replace the map with a new one, so the old one can
// be read without locks. We're expecting the number of different values
// to be small, so that this is a reasonable thing to do.
m2 := make(map[string]string, len(m)+1)
for k, v := range m {
m2[k] = v
}
s = string(data)
m2[s] = s
atomic.StorePointer(&c.strings, *(*unsafe.Pointer)(unsafe.Pointer(&m2)))
}
return s
}
func (c StringCodec) WithInterning() Codec {
ic := &InternedStringCodec{}
m := map[string]string{}
atomic.StorePointer(&ic.strings, *(*unsafe.Pointer)(unsafe.Pointer(&m)))
return ic
}