This repository was archived by the owner on Oct 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 524
improved make_lines efficiency #433
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,13 +5,12 @@ | |
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
from copy import copy | ||
from datetime import datetime | ||
from numbers import Integral | ||
|
||
from pytz import UTC | ||
from dateutil.parser import parse | ||
from six import binary_type, text_type, integer_types, PY2 | ||
from six import iteritems, binary_type, text_type, integer_types, PY2 | ||
|
||
EPOCH = UTC.localize(datetime.utcfromtimestamp(0)) | ||
|
||
|
@@ -108,7 +107,7 @@ def make_lines(data, precision=None): | |
matching the line protocol introduced in InfluxDB 0.9.0. | ||
""" | ||
lines = [] | ||
static_tags = data.get('tags', None) | ||
static_tags = data.get('tags') | ||
for point in data['points']: | ||
elements = [] | ||
|
||
|
@@ -119,32 +118,29 @@ def make_lines(data, precision=None): | |
key_values = [measurement] | ||
|
||
# add tags | ||
if static_tags is None: | ||
tags = point.get('tags', {}) | ||
if static_tags: | ||
tags = dict(static_tags) # make a copy, since we'll modify | ||
tags.update(point.get('tags') or {}) | ||
else: | ||
tags = copy(static_tags) | ||
tags.update(point.get('tags', {})) | ||
tags = point.get('tags') or {} | ||
|
||
# tags should be sorted client-side to take load off server | ||
for tag_key in sorted(tags.keys()): | ||
for tag_key, tag_value in sorted(iteritems(tags)): | ||
key = _escape_tag(tag_key) | ||
value = _escape_tag(tags[tag_key]) | ||
value = _escape_tag(tag_value) | ||
|
||
if key != '' and value != '': | ||
key_values.append("{key}={value}".format(key=key, value=value)) | ||
key_values.append(key + "=" + value) | ||
key_values = ','.join(key_values) | ||
elements.append(key_values) | ||
|
||
# add fields | ||
field_values = [] | ||
for field_key in sorted(point['fields'].keys()): | ||
for field_key, field_value in sorted(iteritems(point['fields'])): | ||
key = _escape_tag(field_key) | ||
value = _escape_value(point['fields'][field_key]) | ||
value = _escape_value(field_value) | ||
if key != '' and value != '': | ||
field_values.append("{key}={value}".format( | ||
key=key, | ||
value=value | ||
)) | ||
field_values.append(key + "=" + value) | ||
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. ditto above regarding concatenating strings |
||
field_values = ','.join(field_values) | ||
elements.append(field_values) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I believe it's better to use string formatting methods rather than appending strings via the
+
operator, can you please restore that behavior?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.
@xginn8 Thanks for the review!
Better in what way?
+
is generally one of the fastest methods of concatenating short strings; conversely,format
with keywords is one of the slowest.Because
key + "=" + value
is short and readable, and because this code is in a frequently executed loop body, I saw no need to sacrifice performance--we can have both efficient and readable code. Thoughts?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.
I stand corrected, that's my fault for not running some tests 👍. Looks like a good change.
Uh oh!
There was an error while loading. Please reload this page.
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.
key_values.append("=".join((key, value)))
would be faster[1] actually.String concatenation creates temporary strings for each concatenation and will get slower as strings gets larger.
join
OTOH is implemented in C and does the whole operation in one go.[1] For large enough strings
Uh oh!
There was an error while loading. Please reload this page.
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.
Speed wise would actually be best to replace the whole loop with a list comprehension:
Cleaner and more readable to boot.