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 overrideTags for objects #5

Merged
merged 20 commits into from
Aug 6, 2024
Merged

Conversation

wesmoncrief
Copy link

@wesmoncrief wesmoncrief commented Aug 2, 2024

A couple of optimizations here. These work for when you have tags as objects, not arrays.

  1. Often, you only have global tags, and no child tags. This memoizes that case.
  2. overrideTagsUnoptimized is slow because it does a lot of string manipulation - it serializes then unserializes tag/value pairs. overrideTags2 can avoid this by only taking in objects. So this optimization is only able to be used when the caller passes in objects (instead of arrays).
  3. if there's no intersection between child and global tags, use a memoized global tags string
  4. String concat is a bit faster than making an array, then string joining

benchmark results:

overrideTagsUnoptimized performance benchmark: 23.790959 ms
overrideTagsUnoptimized for non-pre-made arrays performance benchmark: 19.103083 ms
overrideTags2 performance benchmark: 12.292041 ms

@wesmoncrief wesmoncrief changed the title Wes/optimize override tags Optimize overrideTags for objects Aug 5, 2024
@wesmoncrief wesmoncrief marked this pull request as ready for review August 5, 2024 13:42
lib/statsd.js Outdated
else if (Object.keys(tags).length === 0) {
return globalTagsMemo;
}
if (isTagsArray || Array.isArray(globalTags)) {
Copy link

Choose a reason for hiding this comment

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

the lack of typing here (not really your fault) makes it really hard to follow what the assumptions here are... to is it possible for tags to be an array but for globalTags to be an object? or the other way around?

does overrideTagsToString work if you pass in objects for globalTags or tags?

Copy link
Author

Choose a reason for hiding this comment

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

tags could be array or object. globalTags could be array or object. Welcome to the wild west.

overrideTagsToString does work for any combination of objects/arrays.

overrideTags2 only works if globalTags is an object, and also tags is an object

Copy link
Author

Choose a reason for hiding this comment

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

btw, the unit tests seem pretty good, but i was thinking i'd also test this by testing on a manual image deployment of xover in a small staging region

Copy link

@donhcd donhcd left a comment

Choose a reason for hiding this comment

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

LGTM

lib/helpers.js Outdated
for (const g of Object.keys(globalTags)) {
if (!usedGlobalKeys.has(g)) {
const serializedTagWithValue = `${g}:${sanitizeTags(globalTags[g], telegraf)}`;
result = serializedTagWithValue + separator + result;
Copy link

Choose a reason for hiding this comment

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

should we use addToResult(serializedTagWithValue, true) here?

Copy link
Author

Choose a reason for hiding this comment

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

ya

@wesmoncrief wesmoncrief merged commit 894d4bd into master Aug 6, 2024
3 checks passed
@donhcd donhcd deleted the wes/optimize-override-tags branch August 6, 2024 19:44
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