8000 Use precompiled struct methods for pack, unpack data by umax · Pull Request #266 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Use precompiled struct methods for pack, unpack data #266

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

Merged
merged 1 commit into from
Apr 5, 2018

Conversation

umax
Copy link
Contributor
@umax umax commented Apr 4, 2018

Notes for struct usage optimizations: https://docs.python.org/2/library/struct.html#classes
Regarding string formatting:

In [1]: %timeit '%s' % 1
14.7 ns ± 0.325 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

In [2]: %timeit '{0}'.format(1)
134 ns ± 2.93 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

@@ -29,6 +29,11 @@
_MINUS_INF = float("-inf")
_INITIAL_MMAP_SIZE = 1 << 20

pack_integer = struct.Struct(b'i').pack_into
Copy link
Contributor

Choose a reason for hiding this comment

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

put an _ in front of these so they aren't exported

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

while pos < self._used:
encoded_len = struct.unpack_from(b'i', self._m, pos)[0]

# performance tricks
Copy link
Contributor

Choose a reason for hiding this comment

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

Explain what you're doing a bit more. Not everyone will have seen the relevant talks by Raymond Hettinger

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

I know why this works, but someone reading this code might not. A quick comment will help future readers of the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@umax umax force-pushed the struct-usage-improvements branch from 223267c to 1b295a7 Compare April 4, 2018 08:47
@umax umax force-pushed the struct-usage-improvements branch from 1b295a7 to adaad2d Compare April 4, 2018 13:02
@brian-brazil brian-brazil merged commit a5e7c4e into prometheus:master Apr 5, 2018
@brian-brazil
Copy link
Contributor

Thanks!

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.

2 participants
0