Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Metrics SDK] Use nostd::function_ref in AttributesHashMap #3393

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

Conversation

yashykt
Copy link
Contributor

@yashykt yashykt commented May 7, 2025

Changes

Use nostd::function_ref in AttributesHashMap for aggregation_callback.

A benchmark at https://github.com/grpc/grpc/blob/507a5de9483c9b8675e626977a51d9286f4104f0/test/cpp/microbenchmarks/bm_stats_plugin.cc#L81. shows time go down from 200+ns to ~20ns.

Additionally, with the benchmarks run by @psx95 on BM_AttributseHashMap -

Current HEAD (merged PR#3383):

"benchmarks": [
    {
      "name": "BM_AttributseHashMap",
      "family_index": 0,
      "per_family_instance_index": 0,
      "run_name": "BM_AttributseHashMap",
      "run_type": "iteration",
      "repetitions": 1,
      "repetition_index": 0,
      "threads": 1,
      "iterations": 7,
      "real_time": 3.8004398345947266e+07,
      "cpu_time": 2.0847316142857142e+07,
      "time_unit": "ns"
    }
  ]

This PR's CI:

"benchmarks": [
    {
      "name": "BM_AttributseHashMap",
      "family_index": 0,
      "per_family_instance_index": 0,
      "run_name": "BM_AttributseHashMap",
      "run_type": "iteration",
      "repetitions": 1,
      "repetition_index": 0,
      "threads": 1,
      "iterations": 8,
      "real_time": 2.9205620288848877e+07,
      "cpu_time": 2.0728138000000000e+07,
      "time_unit": "ns"
    }
  ]

Looking at the implementation, we don't need to make a copy of the passed in callable, so it seems desirable to do this.

For significant contributions please make sure you have completed the following items:

  • CHANGELOG.md updated for non-trivial changes
  • Unit tests have been added
  • Changes in public API reviewed

@yashykt yashykt requested a review from a team as a code owner May 7, 2025 08:43
Copy link

netlify bot commented May 7, 2025

Deploy Preview for opentelemetry-cpp-api-docs canceled.

Name Link
🔨 Latest commit 54b0785
🔍 Latest deploy log https://app.netlify.com/sites/opentelemetry-cpp-api-docs/deploys/681d34fd4ce8110008e63c5d

Copy link

codecov bot commented May 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 89.98%. Comparing base (9451f0e) to head (54b0785).
Report is 1 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main    #3393      +/-   ##
==========================================
+ Coverage   89.96%   89.98%   +0.02%     
==========================================
  Files         211      211              
  Lines        6812     6812              
==========================================
+ Hits         6128     6129       +1     
+ Misses        684      683       -1     
Files with missing lines Coverage Δ
...entelemetry/sdk/metrics/state/attributes_hashmap.h 96.37% <100.00%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Aggregation *GetOrSetDefault(
const opentelemetry::common::KeyValueIterable &attributes,
const AttributesProcessor *attributes_processor,
nostd::function_ref<std::unique_ptr<Aggregation>()> aggregation_callback)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it necessary to use nostd::function_ref instead of std::function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've got a benchmark at https://github.com/grpc/grpc/blob/507a5de9483c9b8675e626977a51d9286f4104f0/test/cpp/microbenchmarks/bm_stats_plugin.cc#L81. After this change, it goes down from 200+ns to ~20ns.

Looking at the implementation, we don't need to make a copy of the passed in callable, so it seems desirable to do this.

Copy link
Contributor

Choose a reason for hiding this comment

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

I compared the BM_AttributseHashMap benchmark result for this PR vs the PR from the last commit on main,
there does seem to be reduction in the realtime and cpu time:

Current HEAD (merged PR#3383):

"benchmarks": [
    {
      "name": "BM_AttributseHashMap",
      "family_index": 0,
      "per_family_instance_index": 0,
      "run_name": "BM_AttributseHashMap",
      "run_type": "iteration",
      "repetitions": 1,
      "repetition_index": 0,
      "threads": 1,
      "iterations": 7,
      "real_time": 3.8004398345947266e+07,
      "cpu_time": 2.0847316142857142e+07,
      "time_unit": "ns"
    }
  ]

This PR's CI:

"benchmarks": [
    {
      "name": "BM_AttributseHashMap",
      "family_index": 0,
      "per_family_instance_index": 0,
      "run_name": "BM_AttributseHashMap",
      "run_type": "iteration",
      "repetitions": 1,
      "repetition_index": 0,
      "threads": 1,
      "iterations": 8,
      "real_time": 2.9205620288848877e+07,
      "cpu_time": 2.0728138000000000e+07,
      "time_unit": "ns"
    }
  ]

Additionally, I think we are using the function_ref to immediately call and compute the result here, which makes me think it might be ok to add this change.

Note: Result located in sdk/test/metrics/attributes_hashmap_benchmark_result.json

Copy link
Member

@lalitb lalitb May 8, 2025

Choose a reason for hiding this comment

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

Thanks @yashykt , @psx95 for benchmark.

@yashykt Can you add this reasoning in the PR desc. The changes look good once CI is fixed.

Separately. we may want to revisit other usage of std::function in the SDK if they are candidate for replacing with nostd::function_ref.

Copy link
Contributor

@psx95 psx95 left a comment

Choose a reason for hiding this comment

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

The iwyu CI check is failing which needs to be addressed.

@ThomsonTan ThomsonTan enabled auto-merge (squash) May 8, 2025 23:11
@ThomsonTan ThomsonTan merged commit 4e4d8de into open-telemetry:main May 8, 2025
66 checks passed
malkia added a commit to malkia/opentelemetry-cpp that referenced this pull request May 9, 2025
[Metrics SDK] Use nostd::function_ref in AttributesHashMap (open-telemetry#3393)
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.

4 participants