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 all 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
40 changes: 33 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,40 @@
* 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;
}

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

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);
let buf;
if (hexStr.length === 16) {
buf = buf8;
} else if (hexStr.length === 32) {
buf = buf16;
} else {
buf = Buffer.alloc(hexStr.length / 2);
dyladan marked this conversation as resolved.
Show resolved Hide resolved
}
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,12 @@ 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==');
// Don't use the preallocated path
assert.strictEqual(hexToBase64(id2.repeat(2)), 'EqvANNVn6J/ybmCMjPQsgBKrwDTVZ+if8m5gjIz0LIA=');
});
});