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

refactor: Simplify verifyVersion() #19323

Merged
merged 1 commit into from
Aug 13, 2020
Merged

refactor: Simplify verifyVersion() #19323

merged 1 commit into from
Aug 13, 2020

Conversation

ayang64
Copy link
Contributor

@ayang64 ayang64 commented Aug 13, 2020

This PR adds tests and benchmarks for verifyVersion() and refactors verifyVersion() to call only binary.Read(). This saves some lines of code and make the code clearer and easier to understand.

@ayang64 ayang64 requested a review from a team August 13, 2020 17:47
@ayang64 ayang64 changed the base branch from master to master-1.x August 13, 2020 17:47
@ayang64 ayang64 self-assigned this Aug 13, 2020
@ayang64 ayang64 changed the title Verify version refactor: Simplify verifyVersion() Aug 13, 2020
@ayang64
Copy link
Contributor Author

ayang64 commented Aug 13, 2020

I added tests to ensure the old functionality was preserved.

@ayang64 ayang64 requested review from stuartcarnie, dgnorton and docmerlin and removed request for a team August 13, 2020 17:55
The original version of verifyVersion() reads into a byte slice,
manually ensures its byte order, then converts it to a type comparable
with Version and MagicNumber.

This patch hides those details by calling binary.Read() and reading
values into properly typed variables.

This adds a bit of overhead but this code isn't in the hot-path and this
patch greatly simplifies the code.

verifyVersion() originally accepted an io.ReadSeeker.  It is only called
in once place and that function immediately calls seek after
verifyVersion(), therefore it is probably safe to call Seek() BEFORE
verifyVersion().

The benefit is that verifyVersion() is easier to test since we can pass
it a bytes.Buffer.

This patch adds a test for verifyVersion() as well as a benchmark.

benchmark                    old ns/op     new ns/op     delta
BenchmarkVerifyVersion-8     73.5          123           +67.35%

Finally, this commit moves verifyVersion() from writer.go to reader.go
which is where it is actually used.
Copy link
Contributor

@stuartcarnie stuartcarnie left a comment

Choose a reason for hiding this comment

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

Nice improvements and 💯 for the clear tests

@ayang64 ayang64 added the 1.x label Aug 13, 2020
@ayang64 ayang64 added this to the 1.8.3 milestone Aug 13, 2020
@ayang64 ayang64 merged commit 3436db4 into master-1.x Aug 13, 2020
@jacobmarble jacobmarble deleted the verify-version branch January 2, 2024 22:45
chengshiwen pushed a commit to chengshiwen/influxdb that referenced this pull request Aug 27, 2024
The original version of verifyVersion() reads into a byte slice,
manually ensures its byte order, then converts it to a type comparable
with Version and MagicNumber.

This patch hides those details by calling binary.Read() and reading
values into properly typed variables.

This adds a bit of overhead but this code isn't in the hot-path and this
patch greatly simplifies the code.

verifyVersion() originally accepted an io.ReadSeeker.  It is only called
in once place and that function immediately calls seek after
verifyVersion(), therefore it is probably safe to call Seek() BEFORE
verifyVersion().

The benefit is that verifyVersion() is easier to test since we can pass
it a bytes.Buffer.

This patch adds a test for verifyVersion() as well as a benchmark.

benchmark                    old ns/op     new ns/op     delta
BenchmarkVerifyVersion-8     73.5          123           +67.35%

Finally, this commit moves verifyVersion() from writer.go to reader.go
which is where it is actually used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants