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: improve hexToBase64 #3178

Merged
merged 5 commits into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.

### :rocket: (Enhancement)

* perf(opentelemetry-core): improve hexToBase64 performance [#3178](https://github.com/open-telemetry/opentelemetry-js/pull/3178)
* feat(sdk-trace-base): move Sampler declaration into sdk-trace-base [#3088](https://github.com/open-telemetry/opentelemetry-js/pull/3088) @legendecas
* fix(grpc-instrumentation): added grpc attributes in instrumentation [#3127](https://github.com/open-telemetry/opentelemetry-js/pull/3127) @andrewzenkov
* feat: support latest `@opentelemetry/api` [#3177](https://github.com/open-telemetry/opentelemetry-js/pull/3177) @dyladan
Expand Down
30 changes: 23 additions & 7 deletions packages/opentelemetry-core/src/platform/node/hex-to-base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,30 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
function intValue(charCode: number): number {
// 0-9
if (charCode >= 48 && charCode <= 57) {
return charCode - 48;
}

// a-f
if (charCode >= 97 && charCode <= 102) {
return charCode - 87;
}

// A-F
return charCode - 55;
}

export function hexToBase64(hexStr: string): string {
const hexStrLen = hexStr.length;
let hexAsciiCharsStr = '';
for (let i = 0; i < hexStrLen; i += 2) {
const hexPair = hexStr.substring(i, i + 2);
const hexVal = parseInt(hexPair, 16);
hexAsciiCharsStr += String.fromCharCode(hexVal);
const buf = Buffer.alloc(hexStr.length / 2);
Copy link
Member

Choose a reason for hiding this comment

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

The alloc is probably the most expensive part of this function at this point. Preallocating common lengths likely results in a significant speedup

const buf8 = Buffer.alloc(8)
const buf16 = Buffer.alloc(16)

function hexToBase64(hexStr) {
    let buf;
    if (hexStr.length === 16) buf = buf8;
    else if (hexStr.length === 32) buf = buf16;
    else buf = Buffer.alloc(hexStr.length / 2);

    let offset = 0;

    for (let i = 0; i < hexStr.length; i += 2) {
        const hi = intValue(hexStr.charCodeAt(i));
        const lo = intValue(hexStr.charCodeAt(i + 1));
        buf.writeUInt8((hi << 4) | lo, offset++);
    }

    return buf.toString('base64');
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good point, added in 95966fa

Copy link
Member

Choose a reason for hiding this comment

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

I used node-microbenchmark and the speedup is definitely real

Iterations: 375
  +-------------------------+--------------------+----------------------+-----------+
  | Name                    | nano seconds       | milli seconds        | Slower by |
  +-------------------------+--------------------+----------------------+-----------+
  | testHexToBase64Prealloc | 8349.088           | 0.008349088          | fastest   |
  | testHexToBase64         | 12648.157333333333 | 0.012648157333333333 | 1x        |
  +-------------------------+--------------------+----------------------+-----------+

let offset = 0;

for (let i = 0; i < hexStr.length; i += 2) {
const hi = intValue(hexStr.charCodeAt(i));
const lo = intValue(hexStr.charCodeAt(i + 1));
buf.writeUInt8((hi << 4) | lo, offset++);
}

return Buffer.from(hexAsciiCharsStr, 'ascii').toString('base64');
dyladan marked this conversation as resolved.
Show resolved Hide resolved
return buf.toString('base64');
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import { hexToBase64 } from '../../src/platform';
describe('hexToBase64', () => {
it('convert hex to base64', () => {
const id1 = '7deb739e02e44ef2';
const id2 = '46cef837b919a16ff26e608c8cf42c80';
const id2 = '12abc034d567e89ff26e608c8cf42c80';
const id3 = id2.toUpperCase();
assert.strictEqual(hexToBase64(id1), 'fetzngLkTvI=');
assert.strictEqual(hexToBase64(id2), 'Rs74N7kZoW/ybmCMjPQsgA==');
assert.strictEqual(hexToBase64(id2), 'EqvANNVn6J/ybmCMjPQsgA==');
assert.strictEqual(hexToBase64(id3), 'EqvANNVn6J/ybmCMjPQsgA==');
});
});