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

opentelemetry-sdk: speed up exemplars a bit #4260

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xrmx
Copy link
Contributor

@xrmx xrmx commented Nov 8, 2024

Description

Use a sparse dict to allocate ExemplarsBucket on demand instead of preallocating all of them in FixedSizeExemplarReservoirABC.

Make the following return around 2X more loops for both trace_based and always_off exemplars filter:

.tox/benchmark-opentelemetry-sdk/bin/pytest opentelemetry-sdk/benchmarks/metrics/ -k 'test_histogram_record_1000[7]'

I get 8% less rounds than with a8aacb0c6f2f06bf19b501d98e62f7c0e667fa4c that is the commit before the introductions of exemplars. Without this we are doing 60% less rounds.

Refs #4243

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • .tox/benchmark-opentelemetry-sdk/bin/pytest opentelemetry-sdk/benchmarks/metrics/ -k 'test_histogram_record_1000[7]'

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

Use a sparse dict to allocate ExemplarsBucket on demand instead of
preallocating all of them in FixedSizeExemplarReservoirABC.

Make the following return around 2X more loops for both trace_based and
always_off exemplars filter:

.tox/benchmark-opentelemetry-sdk/bin/pytest opentelemetry-sdk/benchmarks/metrics/ -k 'test_histogram_record_1000[7]'
@xrmx xrmx requested a review from a team as a code owner November 8, 2024 16:27
@xrmx xrmx added the Skip Changelog PRs that do not require a CHANGELOG.md entry label Nov 8, 2024
@aabmass
Copy link
Member

aabmass commented Nov 8, 2024

Does the dict have any downside vs list in the steady state (measuring the same attribute set repeatedly)?

Copy link
Contributor

@fcollonval fcollonval left a comment

Choose a reason for hiding this comment

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

Thanks @xrmx
This looks good to me.

@fcollonval
Copy link
Contributor

Does the dict have any downside vs list in the steady state (measuring the same attribute set repeatedly)?

Without any proof, I would say accessing a list item by index is faster than a dict value by key. So if there are lots of buckets this may be less optimized; but the number of buckets should never be very high.

@xrmx
Copy link
Contributor Author

xrmx commented Nov 11, 2024

Does the dict have any downside vs list in the steady state (measuring the same attribute set repeatedly)?

Without any proof, I would say accessing a list item by index is faster than a dict value by key. So if there are lots of buckets this may be less optimized; but the number of buckets should never be very high.

With small data sets a list may be faster because it's simpler than the dict but with more items the O(n) list access time would lose against dict O(1) access time.

@aabmass
Copy link
Member

aabmass commented Nov 11, 2024

but the number of buckets should never be very high.

If we don't already have a benchmark for taking a measurement with a pre-existing attribute set, it would be great to add. Maybe with the default buckets and one for an ExponentialHistogram, which defaults to maximum of 160 buckets

@fcollonval
Copy link
Contributor

One important note about the exponential histogram case is that the number of buckets for the exemplar has a lower upper bound for buckets than the histogram:

Base2 Exponential Histogram Aggregation SHOULD use a SimpleFixedSizeExemplarReservoir with a reservoir equal to the smaller of the maximum number of buckets configured on the aggregation or twenty (e.g. min(20, max_buckets)).
Xref: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/metrics/sdk.md#exemplar-defaults

@fcollonval
Copy link
Contributor

the O(n) list access time would lose against dict O(1) access time.

Is it that complexity for search rather than for accessing? If you know the index like in this case, I doubt the complexity of the list is higher.

@xrmx
Copy link
Contributor Author

xrmx commented Nov 12, 2024

the O(n) list access time would lose against dict O(1) access time.

Is it that complexity for search rather than for accessing? If you know the index like in this case, I doubt the complexity of the list is higher.

Got nerdsniped in this and yes the getting a list item looks O(1) too:

PyObject *
PyList_GetItem(PyObject *op, Py_ssize_t i)
{
    if (!PyList_Check(op)) {
        PyErr_BadInternalCall();
        return NULL;
    }
    if (!valid_index(i, Py_SIZE(op))) {
        _Py_DECLARE_STR(list_err, "list index out of range");
        PyErr_SetObject(PyExc_IndexError, &_Py_STR(list_err));
        return NULL;
    }
    return ((PyListObject *)op) -> ob_item[i];
}

So I guess the difference in performance comes just from the lazyness of the ExemplarBucket() instantiations.

@aabmass
Copy link
Member

aabmass commented Nov 14, 2024

If we don't already have a benchmark for taking a measurement with a pre-existing attribute set, it would be great to add.

@xrmx would you be open to adding a benchmark for this to make sure the dict doesn't make this much slower? That seems like the more common case to optimize for vs churning attributes.

@xrmx
Copy link
Contributor Author

xrmx commented Nov 18, 2024

If we don't already have a benchmark for taking a measurement with a pre-existing attribute set, it would be great to add.

@xrmx would you be open to adding a benchmark for this to make sure the dict doesn't make this much slower? That seems like the more common case to optimize for vs churning attributes.

Need to sort out how to write such benchmark 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Skip Changelog PRs that do not require a CHANGELOG.md entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants