Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mem: skip defer for performance #48

Merged
merged 1 commit into from
Sep 4, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,22 +167,27 @@ func (m *Message) NumSegments() int64 {

// Segment returns the segment with the given ID.
func (m *Message) Segment(id SegmentID) (*Segment, error) {
m.mu.Lock()
defer m.mu.Unlock()
var seg *Segment
if isInt32Bit() && id > maxInt32 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this case doesn't require the mutex

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean we can move m.mu.Lock() below this block correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup.

return nil, errSegment32Bit
}
if seg := m.segment(id); seg != nil {
m.mu.Lock()
if seg = m.segment(id); seg != nil {
m.mu.Unlock()
return seg, nil
}
if int64(id) >= m.Arena.NumSegments() {
m.mu.Unlock()
return nil, errSegmentOutOfBounds
}
data, err := m.Arena.Data(id)
if err != nil {
m.mu.Unlock()
return nil, err
}
return m.setSegment(id, data), nil
seg = m.setSegment(id, data)
m.mu.Unlock()
return seg, nil
}

// segment returns the segment with the given ID.
Expand Down Expand Up @@ -230,19 +235,23 @@ func (m *Message) setSegment(id SegmentID, data []byte) *Segment {
// cap(seg.Data) - len(seg.Data) >= sz.
func (m *Message) allocSegment(sz Size) (*Segment, error) {
m.mu.Lock()
defer m.mu.Unlock()
var seg *Segment
if m.segs == nil && m.firstSeg.msg != nil {
m.segs = make(map[SegmentID]*Segment)
m.segs[0] = &m.firstSeg
}
id, data, err := m.Arena.Allocate(sz, m.segs)
if err != nil {
m.mu.Unlock()
return nil, err
}
if isInt32Bit() && id > maxInt32 {
m.mu.Unlock()
return nil, errSegment32Bit
}
return m.setSegment(id, data), nil
seg = m.setSegment(id, data)
m.mu.Unlock()
return seg, nil
}

// alloc allocates sz zero-filled bytes. It prefers using s, but may
Expand Down