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

perf(propagator-jaeger): improve deserializeSpanContext performance #3541

Merged

Conversation

doochik
Copy link
Contributor

@doochik doochik commented Jan 17, 2023

Which problem is this PR solving?

Function deserializeSpanContext() at opentelemetry-propagator-jaeger is not optimized.

Short description of the changes

  1. re.test(str) works much faster than str.match(re) because it doesn't store captured results
  2. RegExp compilation (flags.match(/^[0-9a-f]{1,2}$/i)) on every extract() is slow. Compile it once much better.

Type of change

  • Performance improvement

How Has This Been Tested?

existing unit-tests is enough

Checklist:

  • Followed the style guidelines of this project

@doochik doochik requested a review from a team January 17, 2023 15:32
@codecov
Copy link

codecov bot commented Jan 17, 2023

Codecov Report

Merging #3541 (7730d65) into main (d1aa906) will increase coverage by 0.02%.
The diff coverage is 100.00%.

❗ Current head 7730d65 differs from pull request most recent head 4e1b82c. Consider uploading reports for the commit 4e1b82c to get more accurate results

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3541      +/-   ##
==========================================
+ Coverage   93.96%   93.99%   +0.02%     
==========================================
  Files         260      260              
  Lines        7805     7804       -1     
  Branches     1622     1621       -1     
==========================================
+ Hits         7334     7335       +1     
+ Misses        471      469       -2     
Impacted Files Coverage Δ
...elemetry-propagator-jaeger/src/JaegerPropagator.ts 100.00% <100.00%> (+1.69%) ⬆️
...-trace-base/src/platform/node/RandomIdGenerator.ts 93.75% <0.00%> (+6.25%) ⬆️

@doochik doochik force-pushed the deserializeSpanContext-performance branch from 82dd086 to e6b1df5 Compare January 17, 2023 16:03
@doochik doochik changed the title perf(opentelemetry-propagator-jaeger): improve deserializeSpanContext performance perf(propagator-jaeger): improve deserializeSpanContext performance Jan 17, 2023
Copy link
Member

@dyladan dyladan left a comment

Choose a reason for hiding this comment

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

Seems fine but can you show benchmark results? AFAIK regex literals are not recompiled on each use by optimized runtimes like v8

@doochik
Copy link
Contributor Author

doochik commented Jan 28, 2023

Seems fine but can you show benchmark results? AFAIK regex literals are not recompiled on each use by optimized runtimes like v8

% node --version
v18.13.0

% cat benny.js
const crypto = require('crypto');
const b = require('benny');

b.suite(
`https://github.com/open-telemetry/opentelemetry-js/pull/3541/files`,
    b.add('original code (match + create regexp)', () => {
        const flags = crypto.randomBytes(1).toString('hex');

        return () => {
            return flags.match(/^[0-9a-f]{1,2}$/i);
        };
    }),
    b.add('match + cached regexp', () => {
        const VALID_HEX_RE = /^[0-9a-f]{1,2}$/i;
        const flags = crypto.randomBytes(1).toString('hex');

        return () => {
            return flags.match(VALID_HEX_RE);
        };
    }),
    b.add('test + cached regexp', () => {
        const VALID_HEX_RE = /^[0-9a-f]{1,2}$/i;
        const flags = crypto.randomBytes(1).toString('hex');
        
        return () => {
            return VALID_HEX_RE.test(flags);
        };
    }),
    b.cycle(),
    b.complete()
);

% node benny.js
Running "https://github.com/open-telemetry/opentelemetry-js/pull/3541/files" suite...
Progress: 100%

  original code (match + create regexp):
    38 598 500 ops/s, ±0.37%   | slowest, 50.01% slower

  match + cached regexp:
    43 168 140 ops/s, ±0.21%   | 44.09% slower

  test + cached regexp:
    77 212 612 ops/s, ±0.18%   | fastest

Finished 3 cases!
  Fastest: test + cached regexp
  Slowest: original code (match + create regexp)

@doochik doochik force-pushed the deserializeSpanContext-performance branch from ddad538 to 071a32c Compare January 30, 2023 15:22
@doochik doochik requested a review from dyladan January 30, 2023 15:24
@doochik
Copy link
Contributor Author

doochik commented Feb 9, 2023

@dyladan @legendecas i'm sorry, it's very hard to merge it without your approve

@dyladan
Copy link
Member

dyladan commented Feb 10, 2023

Yeah I'll merge it there were just other things pulling me away yesterday

@dyladan dyladan merged commit 326fd55 into open-telemetry:main Feb 10, 2023
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.

3 participants