From e59fac3fe3ab8dac73becd6b4094ed1e0484f3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antonio=20Rom=C3=A1n?= Date: Mon, 29 Nov 2021 11:20:18 +0100 Subject: [PATCH] refactor(SnowflakeUtil): clean up utils and improve perf (#7036) --- src/util/SnowflakeUtil.js | 25 +++++++++---------- src/util/Util.js | 51 --------------------------------------- typings/index.d.ts | 2 -- 3 files changed, 11 insertions(+), 67 deletions(-) diff --git a/src/util/SnowflakeUtil.js b/src/util/SnowflakeUtil.js index b66d24982049..640692535cbc 100644 --- a/src/util/SnowflakeUtil.js +++ b/src/util/SnowflakeUtil.js @@ -1,10 +1,8 @@ 'use strict'; -const Util = require('./Util'); - // Discord epoch (2015-01-01T00:00:00.000Z) const EPOCH = 1_420_070_400_000; -let INCREMENT = 0; +let INCREMENT = BigInt(0); /** * A container for useful snowflake-related methods. @@ -36,11 +34,10 @@ class SnowflakeUtil extends null { `"timestamp" argument must be a number (received ${isNaN(timestamp) ? 'NaN' : typeof timestamp})`, ); } - if (INCREMENT >= 4095) INCREMENT = 0; - const BINARY = `${(timestamp - EPOCH).toString(2).padStart(42, '0')}0000100000${(INCREMENT++) - .toString(2) - .padStart(12, '0')}`; - return Util.binaryToId(BINARY); + if (INCREMENT >= 4095n) INCREMENT = BigInt(0); + + // Assign WorkerId as 1 and ProcessId as 0: + return ((BigInt(timestamp - EPOCH) << 22n) | (1n << 17n) | INCREMENT++).toString(); } /** @@ -60,16 +57,16 @@ class SnowflakeUtil extends null { * @returns {DeconstructedSnowflake} */ static deconstruct(snowflake) { - const BINARY = Util.idToBinary(snowflake).toString(2).padStart(64, '0'); + const bigIntSnowflake = BigInt(snowflake); return { - timestamp: parseInt(BINARY.substring(0, 42), 2) + EPOCH, + timestamp: Number(bigIntSnowflake >> 22n) + EPOCH, get date() { return new Date(this.timestamp); }, - workerId: parseInt(BINARY.substring(42, 47), 2), - processId: parseInt(BINARY.substring(47, 52), 2), - increment: parseInt(BINARY.substring(52, 64), 2), - binary: BINARY, + workerId: Number((bigIntSnowflake >> 17n) & 0b11111n), + processId: Number((bigIntSnowflake >> 12n) & 0b11111n), + increment: Number(bigIntSnowflake & 0b111111111111n), + binary: bigIntSnowflake.toString(2).padStart(64, '0'), }; } diff --git a/src/util/Util.js b/src/util/Util.js index 2c6cbb2daaa7..b40f8e40deef 100644 --- a/src/util/Util.js +++ b/src/util/Util.js @@ -522,57 +522,6 @@ class Util extends null { return ext && res.ext.startsWith(ext) ? res.name : res.base.split('?')[0]; } - /** - * Transforms a snowflake from a decimal string to a bit string. - * @param {Snowflake} num Snowflake to be transformed - * @returns {string} - * @private - */ - static idToBinary(num) { - let bin = ''; - let high = parseInt(num.slice(0, -10)) || 0; - let low = parseInt(num.slice(-10)); - while (low > 0 || high > 0) { - bin = String(low & 1) + bin; - low = Math.floor(low / 2); - if (high > 0) { - low += 5_000_000_000 * (high % 2); - high = Math.floor(high / 2); - } - } - return bin; - } - - /** - * Transforms a snowflake from a bit string to a decimal string. - * @param {string} num Bit string to be transformed - * @returns {Snowflake} - * @private - */ - static binaryToId(num) { - let dec = ''; - - while (num.length > 50) { - const high = parseInt(num.slice(0, -32), 2); - const low = parseInt((high % 10).toString(2) + num.slice(-32), 2); - - dec = (low % 10).toString() + dec; - num = - Math.floor(high / 10).toString(2) + - Math.floor(low / 10) - .toString(2) - .padStart(32, '0'); - } - - num = parseInt(num, 2); - while (num > 0) { - dec = (num % 10).toString() + dec; - num = Math.floor(num / 10); - } - - return dec; - } - /** * Breaks user, role and everyone/here mentions by adding a zero width space after every @ character * @param {string} str The string to sanitize diff --git a/typings/index.d.ts b/typings/index.d.ts index a8bec3256448..212b8fc94a6f 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -2205,7 +2205,6 @@ export class Util extends null { private constructor(); public static archivedThreadSweepFilter(lifetime?: number): SweepFilter; public static basename(path: string, ext?: string): string; - public static binaryToId(num: string): Snowflake; public static cleanContent(str: string, channel: TextBasedChannels): string; /** @deprecated Use {@link MessageOptions.allowedMentions} to control mentions in a message instead. */ public static removeMentions(str: string): string; @@ -2225,7 +2224,6 @@ export class Util extends null { public static cleanCodeBlockContent(text: string): string; public static fetchRecommendedShards(token: string, options?: FetchRecommendedShardsOptions): Promise; public static flatten(obj: unknown, ...props: Record[]): unknown; - public static idToBinary(num: Snowflake): string; public static makeError(obj: MakeErrorOptions): Error; public static makePlainError(err: Error): MakeErrorOptions; public static mergeDefault(def: unknown, given: unknown): unknown;