-
Notifications
You must be signed in to change notification settings - Fork 166
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
performance: cache the ID for Header entities #1279
Changes from 16 commits
9bc64fe
24b71de
41b3fc4
82358e7
60483ec
d1e9cdf
9e9f497
49d65c6
70d7fdf
f477da6
32dfcce
d8b923a
50fb67d
267da49
9ec0926
9c0cf94
d7db22c
f98fffd
c1a93b0
47cc965
847b3b6
8c21989
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package flow | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"sync" | ||
"time" | ||
|
||
"github.com/fxamacker/cbor/v2" | ||
|
@@ -72,6 +74,10 @@ func (h Header) Fingerprint() []byte { | |
return fingerprint.Fingerprint(h.Body()) | ||
} | ||
|
||
var mutexHeader sync.Mutex | ||
var previdHeader Identifier | ||
var prevHeader Header | ||
|
||
// ID returns a unique ID to singularly identify the header and its block | ||
// within the flow system. | ||
func (h Header) ID() Identifier { | ||
|
@@ -80,7 +86,50 @@ func (h Header) ID() Identifier { | |
if h.Timestamp.Location() != time.UTC { | ||
h.Timestamp = h.Timestamp.UTC() | ||
} | ||
return MakeID(h) | ||
|
||
mutexHeader.Lock() | ||
|
||
// unlock at the return | ||
defer mutexHeader.Unlock() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't quite understand this change. Wouldn't this lock block two different threads computing IDs of two headers? If engine A is calling header1.ID(), and engine B is calling header2.ID(), before this change, they can run concurrently, but with this change, one engine has to be blocked and wait until the other finishes its call. no? |
||
|
||
// compare these elements individually | ||
if prevHeader.ParentVoterIDs != nil && | ||
prevHeader.ParentVoterSigData != nil && | ||
prevHeader.ProposerSigData != nil && | ||
len(h.ParentVoterIDs) == len(prevHeader.ParentVoterIDs) && | ||
len(h.ParentVoterSigData) == len(prevHeader.ParentVoterSigData) && | ||
len(h.ProposerSigData) == len(prevHeader.ProposerSigData) { | ||
bNotEqual := false | ||
|
||
for i, v := range h.ParentVoterIDs { | ||
if v == prevHeader.ParentVoterIDs[i] { | ||
continue | ||
} | ||
bNotEqual = true | ||
break | ||
} | ||
if !bNotEqual && | ||
h.ChainID == prevHeader.ChainID && | ||
h.Timestamp == prevHeader.Timestamp && | ||
h.Height == prevHeader.Height && | ||
h.ParentID == prevHeader.ParentID && | ||
h.View == prevHeader.View && | ||
h.PayloadHash == prevHeader.PayloadHash && | ||
bytes.Equal(h.ProposerSigData, prevHeader.ProposerSigData) && | ||
bytes.Equal(h.ParentVoterSigData, prevHeader.ParentVoterSigData) && | ||
h.ProposerID == prevHeader.ProposerID { | ||
|
||
// cache hit, return the previous identifier | ||
return previdHeader | ||
} | ||
} | ||
jwinkler2083233 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
previdHeader = MakeID(h) | ||
|
||
// store a reference to the Header entity data | ||
prevHeader = h | ||
|
||
return previdHeader | ||
} | ||
|
||
// Checksum returns the checksum of the header. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this added by accident?