Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

improved make_lines efficiency #433

Merged
merged 1 commit into from
Apr 5, 2017

Conversation

RonRothman
Copy link
Contributor

This PR reduces some unneeded allocations and operations in make_lines.

We're using the influxdb client in our high throughput, low latency web service. We've profiled our service and found that make_lines was a bottleneck. When make_lines takes a long time, it blocks other greenlets from servicing requests.

No change in functionality; only efficiency.

no change in functionality; only efficiency.

if key != '' and value != '':
key_values.append("{key}={value}".format(key=key, value=value))
key_values.append(key + "=" + value)
Copy link
Collaborator

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?

Copy link
Contributor Author

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?

Copy link
Collaborator

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.

Copy link

@pkittenis pkittenis Apr 4, 2017

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

Copy link

@pkittenis pkittenis Apr 4, 2017

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:

key_values = ["=".join((key, _escape_tag(val))) 
              for key, val in sorted(iteritems(tags))
              if key != "" and val != ""]

Cleaner and more readable to boot.

key=key,
value=value
))
field_values.append(key + "=" + value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

ditto above regarding concatenating strings

@aviau
Copy link
Collaborator

aviau commented Apr 5, 2017

Thank you for the PR!

Thanks to @xginn8 for reviewing :)

@aviau aviau merged commit ee6a6b6 into influxdata:master Apr 5, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants