Skip to content

Commit

Permalink
more low hanging allocation fruit for outbound
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Busbey committed Oct 21, 2016
1 parent bcf5a2e commit aaa7a0c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 28 deletions.
18 changes: 14 additions & 4 deletions field_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ func (f *field) Tag() Tag {
}

func (f *field) init(tag Tag, value []byte) {
f.tvs = make([]TagValue, 1)
if len(f.tvs) == 0 {
f.tvs = make([]TagValue, 1)
} else {
f.tvs = f.tvs[:1]
}
f.tvs[0].init(tag, value)
}

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

//SetField sets the field with Tag tag
func (m *FieldMap) SetField(tag Tag, field FieldValueWriter) *FieldMap {
return m.SetBytes(tag, field.Write())
}

//SetBytes sets bytes
func (m *FieldMap) SetBytes(tag Tag, value []byte) *FieldMap {
f := m.getOrCreate(tag)
f.init(tag, field.Write())
f.init(tag, value)
return m
}

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

//SetInt is a SetField wrapper for int fields
func (m *FieldMap) SetInt(tag Tag, value int) *FieldMap {
return m.SetField(tag, FIXInt(value))
v := FIXInt(value)
return m.SetBytes(tag, v.Write())
}

//SetString is a SetField wrapper for string fields
func (m *FieldMap) SetString(tag Tag, value string) *FieldMap {
return m.SetField(tag, FIXString(value))
return m.SetBytes(tag, []byte(value))
}

//Clear purges all fields from field map
Expand Down
2 changes: 1 addition & 1 deletion fix_int.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ func (f *FIXInt) Read(bytes []byte) error {
}

func (f FIXInt) Write() []byte {
return []byte(strconv.Itoa(int(f)))
return strconv.AppendInt(nil, int64(f), 10)
}
8 changes: 4 additions & 4 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ func (m *Message) String() string {
return string(m.build())
}

func newCheckSum(value int) FIXString {
return FIXString(fmt.Sprintf("%03d", value))
func formatCheckSum(value int) string {
return fmt.Sprintf("%03d", value)
}

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

func (m *Message) cook() {
bodyLength := m.Header.length() + m.Body.length() + m.Trailer.length()
m.Header.SetField(tagBodyLength, FIXInt(bodyLength))
m.Header.SetInt(tagBodyLength, bodyLength)
checkSum := (m.Header.total() + m.Body.total() + m.Trailer.total()) % 256
m.Trailer.SetField(tagCheckSum, newCheckSum(checkSum))
m.Trailer.SetString(tagCheckSum, formatCheckSum(checkSum))
}
21 changes: 10 additions & 11 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,21 @@ func (s *session) insertSendingTime(msg *Message) {
}
}

func optionallySetID(header Header, field Tag, value string) {
func optionallySetID(msg *Message, field Tag, value string) {
if len(value) != 0 {
header.SetString(field, value)
msg.Header.SetString(field, value)
}
}

func (s *session) fillDefaultHeader(msg *Message, inReplyTo *Message) {
msg.Header.SetField(tagBeginString, FIXString(s.sessionID.BeginString))

msg.Header.SetField(tagSenderCompID, FIXString(s.sessionID.SenderCompID))
optionallySetID(msg.Header, tagSenderSubID, s.sessionID.SenderSubID)
optionallySetID(msg.Header, tagSenderLocationID, s.sessionID.SenderLocationID)

msg.Header.SetField(tagTargetCompID, FIXString(s.sessionID.TargetCompID))
optionallySetID(msg.Header, tagTargetSubID, s.sessionID.TargetSubID)
optionallySetID(msg.Header, tagTargetLocationID, s.sessionID.TargetLocationID)
msg.Header.SetString(tagBeginString, s.sessionID.BeginString)
msg.Header.SetString(tagSenderCompID, s.sessionID.SenderCompID)
optionallySetID(msg, tagSenderSubID, s.sessionID.SenderSubID)
optionallySetID(msg, tagSenderLocationID, s.sessionID.SenderLocationID)

msg.Header.SetString(tagTargetCompID, s.sessionID.TargetCompID)
optionallySetID(msg, tagTargetSubID, s.sessionID.TargetSubID)
optionallySetID(msg, tagTargetLocationID, s.sessionID.TargetLocationID)

s.insertSendingTime(msg)

Expand Down
15 changes: 7 additions & 8 deletions tag_value.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ type TagValue struct {
}

func (tv *TagValue) init(tag Tag, value []byte) {
var buf bytes.Buffer
buf.WriteString(strconv.Itoa(int(tag)))
buf.WriteString("=")
buf.Write(value)
buf.WriteString("")
tv.bytes = strconv.AppendInt(nil, int64(tag), 10)
tv.bytes = append(tv.bytes, []byte("=")...)
tv.bytes = append(tv.bytes, value...)
tv.bytes = append(tv.bytes, []byte("")...)

tv.tag = tag
tv.bytes = buf.Bytes()
tv.value = value
}

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

tv.tag = Tag(parsedTag)
tv.value = rawFieldBytes[(sepIndex + 1):(len(rawFieldBytes) - 1)]
tv.bytes = rawFieldBytes
n := len(rawFieldBytes)
tv.value = rawFieldBytes[(sepIndex + 1):(n - 1):(n - 1)]
tv.bytes = rawFieldBytes[:n:n]

return
}
Expand Down

0 comments on commit aaa7a0c

Please sign in to comment.