Skip to content

Commit aaa7a0c

Browse files
author
Chris Busbey
committed
more low hanging allocation fruit for outbound
1 parent bcf5a2e commit aaa7a0c

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

field_map.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ func (f *field) Tag() Tag {
1616
}
1717

1818
func (f *field) init(tag Tag, value []byte) {
19-
f.tvs = make([]TagValue, 1)
19+
if len(f.tvs) == 0 {
20+
f.tvs = make([]TagValue, 1)
21+
} else {
22+
f.tvs = f.tvs[:1]
23+
}
2024
f.tvs[0].init(tag, value)
2125
}
2226

@@ -168,8 +172,13 @@ func (m FieldMap) GetGroup(parser FieldGroupReader) MessageRejectError {
168172

169173
//SetField sets the field with Tag tag
170174
func (m *FieldMap) SetField(tag Tag, field FieldValueWriter) *FieldMap {
175+
return m.SetBytes(tag, field.Write())
176+
}
177+
178+
//SetBytes sets bytes
179+
func (m *FieldMap) SetBytes(tag Tag, value []byte) *FieldMap {
171180
f := m.getOrCreate(tag)
172-
f.init(tag, field.Write())
181+
f.init(tag, value)
173182
return m
174183
}
175184

@@ -180,12 +189,13 @@ func (m *FieldMap) SetBool(tag Tag, value bool) *FieldMap {
180189

181190
//SetInt is a SetField wrapper for int fields
182191
func (m *FieldMap) SetInt(tag Tag, value int) *FieldMap {
183-
return m.SetField(tag, FIXInt(value))
192+
v := FIXInt(value)
193+
return m.SetBytes(tag, v.Write())
184194
}
185195

186196
//SetString is a SetField wrapper for string fields
187197
func (m *FieldMap) SetString(tag Tag, value string) *FieldMap {
188-
return m.SetField(tag, FIXString(value))
198+
return m.SetBytes(tag, []byte(value))
189199
}
190200

191201
//Clear purges all fields from field map

fix_int.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,5 @@ func (f *FIXInt) Read(bytes []byte) error {
5959
}
6060

6161
func (f FIXInt) Write() []byte {
62-
return []byte(strconv.Itoa(int(f)))
62+
return strconv.AppendInt(nil, int64(f), 10)
6363
}

message.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ func (m *Message) String() string {
305305
return string(m.build())
306306
}
307307

308-
func newCheckSum(value int) FIXString {
309-
return FIXString(fmt.Sprintf("%03d", value))
308+
func formatCheckSum(value int) string {
309+
return fmt.Sprintf("%03d", value)
310310
}
311311

312312
//Build constructs a []byte from a Message instance
@@ -322,7 +322,7 @@ func (m *Message) build() []byte {
322322

323323
func (m *Message) cook() {
324324
bodyLength := m.Header.length() + m.Body.length() + m.Trailer.length()
325-
m.Header.SetField(tagBodyLength, FIXInt(bodyLength))
325+
m.Header.SetInt(tagBodyLength, bodyLength)
326326
checkSum := (m.Header.total() + m.Body.total() + m.Trailer.total()) % 256
327-
m.Trailer.SetField(tagCheckSum, newCheckSum(checkSum))
327+
m.Trailer.SetString(tagCheckSum, formatCheckSum(checkSum))
328328
}

session.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,21 @@ func (s *session) insertSendingTime(msg *Message) {
9999
}
100100
}
101101

102-
func optionallySetID(header Header, field Tag, value string) {
102+
func optionallySetID(msg *Message, field Tag, value string) {
103103
if len(value) != 0 {
104-
header.SetString(field, value)
104+
msg.Header.SetString(field, value)
105105
}
106106
}
107107

108108
func (s *session) fillDefaultHeader(msg *Message, inReplyTo *Message) {
109-
msg.Header.SetField(tagBeginString, FIXString(s.sessionID.BeginString))
110-
111-
msg.Header.SetField(tagSenderCompID, FIXString(s.sessionID.SenderCompID))
112-
optionallySetID(msg.Header, tagSenderSubID, s.sessionID.SenderSubID)
113-
optionallySetID(msg.Header, tagSenderLocationID, s.sessionID.SenderLocationID)
114-
115-
msg.Header.SetField(tagTargetCompID, FIXString(s.sessionID.TargetCompID))
116-
optionallySetID(msg.Header, tagTargetSubID, s.sessionID.TargetSubID)
117-
optionallySetID(msg.Header, tagTargetLocationID, s.sessionID.TargetLocationID)
109+
msg.Header.SetString(tagBeginString, s.sessionID.BeginString)
110+
msg.Header.SetString(tagSenderCompID, s.sessionID.SenderCompID)
111+
optionallySetID(msg, tagSenderSubID, s.sessionID.SenderSubID)
112+
optionallySetID(msg, tagSenderLocationID, s.sessionID.SenderLocationID)
113+
114+
msg.Header.SetString(tagTargetCompID, s.sessionID.TargetCompID)
115+
optionallySetID(msg, tagTargetSubID, s.sessionID.TargetSubID)
116+
optionallySetID(msg, tagTargetLocationID, s.sessionID.TargetLocationID)
118117

119118
s.insertSendingTime(msg)
120119

tag_value.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,12 @@ type TagValue struct {
1414
}
1515

1616
func (tv *TagValue) init(tag Tag, value []byte) {
17-
var buf bytes.Buffer
18-
buf.WriteString(strconv.Itoa(int(tag)))
19-
buf.WriteString("=")
20-
buf.Write(value)
21-
buf.WriteString("")
17+
tv.bytes = strconv.AppendInt(nil, int64(tag), 10)
18+
tv.bytes = append(tv.bytes, []byte("=")...)
19+
tv.bytes = append(tv.bytes, value...)
20+
tv.bytes = append(tv.bytes, []byte("")...)
2221

2322
tv.tag = tag
24-
tv.bytes = buf.Bytes()
2523
tv.value = value
2624
}
2725

@@ -41,8 +39,9 @@ func (tv *TagValue) parse(rawFieldBytes []byte) (err error) {
4139
}
4240

4341
tv.tag = Tag(parsedTag)
44-
tv.value = rawFieldBytes[(sepIndex + 1):(len(rawFieldBytes) - 1)]
45-
tv.bytes = rawFieldBytes
42+
n := len(rawFieldBytes)
43+
tv.value = rawFieldBytes[(sepIndex + 1):(n - 1):(n - 1)]
44+
tv.bytes = rawFieldBytes[:n:n]
4645

4746
return
4847
}

0 commit comments

Comments
 (0)