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

Optimize OStream implementation #1399

Merged
merged 10 commits into from
Feb 25, 2019
Merged

Optimize OStream implementation #1399

merged 10 commits into from
Feb 25, 2019

Conversation

richardartoul
Copy link
Contributor

@richardartoul richardartoul commented Feb 21, 2019

When writing annotation heavy code, we spend 80% of our time in this routine:

screen shot 2019-02-21 at 1 53 13 pm

This P.R makes that code-path 10x faster

Before

goos: darwin
goarch: amd64
pkg: github.com/m3db/m3/src/dbnode/encoding
BenchmarkWriteBytes-8   	  100000	     22507 ns/op	      20 B/op	       0 allocs/op
PASS
ok  	github.com/m3db/m3/src/dbnode/encoding	2.495s
Success: Benchmarks passed.

After

goos: darwin
goarch: amd64
pkg: github.com/m3db/m3/src/dbnode/encoding
BenchmarkWriteBytes-8   	 1000000	      2226 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/m3db/m3/src/dbnode/encoding	2.271s

@richardartoul richardartoul changed the title [WIP] - Optimize OStream implementation Optimize OStream implementation Feb 21, 2019
Copy link
Contributor

@simonrobb simonrobb left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov
Copy link

codecov bot commented Feb 22, 2019

Codecov Report

Merging #1399 into master will not change coverage.
The diff coverage is n/a.

Impacted file tree graph

@@          Coverage Diff           @@
##           master   #1399   +/-   ##
======================================
  Coverage    63.6%   63.6%           
======================================
  Files         753     753           
  Lines       64733   64733           
======================================
  Hits        41191   41191           
  Misses      20455   20455           
  Partials     3087    3087
Flag Coverage Δ
#aggregator 74.1% <0%> (ø) ⬆️
#cluster 84.3% <0%> (ø) ⬆️
#collector 63.7% <0%> (ø) ⬆️
#dbnode 68.7% <0%> (ø) ⬆️
#m3em 61.9% <0%> (ø) ⬆️
#m3ninx 67.8% <0%> (ø) ⬆️
#m3nsch 28.4% <0%> (ø) ⬆️
#metrics 17.6% <0%> (ø) ⬆️
#msg 74.9% <0%> (ø) ⬆️
#query 64.3% <0%> (ø) ⬆️
#x 65.8% <0%> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fa7efee...d398624. Read the comment docs.

@codecov
Copy link

codecov bot commented Feb 22, 2019

Codecov Report

Merging #1399 into master will decrease coverage by <.1%.
The diff coverage is 100%.

Impacted file tree graph

@@           Coverage Diff            @@
##           master   #1399     +/-   ##
========================================
- Coverage    70.8%   70.8%   -0.1%     
========================================
  Files         832     832             
  Lines       71370   71400     +30     
========================================
- Hits        50559   50553      -6     
- Misses      17511   17535     +24     
- Partials     3300    3312     +12
Flag Coverage Δ
#aggregator 82.3% <ø> (ø) ⬆️
#cluster 85.9% <ø> (ø) ⬆️
#collector 63.7% <ø> (ø) ⬆️
#dbnode 80.6% <100%> (-0.2%) ⬇️
#m3em 73.2% <ø> (ø) ⬆️
#m3ninx 74.2% <ø> (ø) ⬆️
#m3nsch 51.1% <ø> (ø) ⬆️
#metrics 17.6% <ø> (ø) ⬆️
#msg 74.9% <ø> (ø) ⬆️
#query 65.5% <ø> (ø) ⬆️
#x 76% <ø> (ø) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a1cc0ee...261eaab. Read the comment docs.

var (
currCap = cap(os.rawBuffer)
currLen = len(os.rawBuffer)
availableCap = currCap - currLen
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Given that availableCap is only used in the next line, may as well just do the calc of missingCap in 1 line?

return os.checked, os.pos
}

// repairCheckedBytes makes suer that the checked.Bytes wraps the rawBuffer as
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: Typo on sure.

@@ -180,5 +234,21 @@ func (os *ostream) Reset(buffer checked.Bytes) {

// Rawbytes returns the Osteam's raw bytes
func (os *ostream) Rawbytes() (checked.Bytes, int) {
return os.rawBuffer, os.pos
os.repairCheckedBytes()
return os.checked, os.pos
Copy link
Collaborator

Choose a reason for hiding this comment

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

This changes the cost accounting accuracy right? eg, checked now will only give the cost when this was called and not the underlying bytes which may be written to afterwards. Not sure if it matters, how long do we hold the ref from this call for and what is the cost accounting used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As far as I'm aware we don't use checked bytes for tracking cost, we use them for tracking ownership.

This changes the semantics slightly, but the way .the OStream is used this should be fine. Basically what I'm doing is while the OStream is holding onto the checked bytes, it uses the raw buffer to write to them, and only when it exposes the bytes to someone else does it use the checked bytes.

The only thing that could cause issues is if someone got a hold of the checked bytes from this thing, and then they kept writing to the ostream and the rawbuffer and checked bytes they grabbed earlier got out of sync, but thats not how we use this structure in the encoder luckily

Copy link
Collaborator

@robskillington robskillington left a comment

Choose a reason for hiding this comment

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

This is gross but makes sense =]

Nice work, couldn't find any bugs, LGTM, ship it.

@richardartoul richardartoul merged commit 985eb5f into master Feb 25, 2019
@justinjc justinjc deleted the ra/optimize-ostream branch June 17, 2019 21:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants