tangled
alpha
login
or
join now
sajidanwar.com
/
atcute
forked from
mary.my.id/atcute
0
fork
atom
a collection of lightweight TypeScript packages for AT Protocol, the protocol powering Bluesky
0
fork
atom
overview
issues
pulls
pipelines
refactor(tid): faster encode/decode
mary.my.id
1 month ago
a46b7efd
a891529d
verified
This commit was signed with the committer's
known signature
.
mary.my.id
SSH Key Fingerprint:
SHA256:ZlTP/auFSGpGnaoDg4mCTG1g9OZvXp62jWR4c6H4O3c=
+39
-9
4 changed files
expand all
collapse all
unified
split
.changeset
bright-snails-hide.md
little-bars-care.md
packages
utilities
tid
lib
index.ts
s32.ts
+1
-1
.changeset/bright-snails-hide.md
···
2
2
'@atcute/cbor': patch
3
3
---
4
4
5
5
-
quickly bail out to slow path on server runtimes
5
5
+
quickly bail out to slow path on server runtimes
+5
.changeset/little-bars-care.md
···
1
1
+
---
2
2
+
'@atcute/tid': patch
3
3
+
---
4
4
+
5
5
+
faster encode/decode
+5
-5
packages/utilities/tid/lib/index.ts
···
2
2
3
3
import { random } from '#platform/random';
4
4
5
5
-
import { s32decode, s32encode } from './s32.ts';
5
5
+
import { S32_2CHAR_TABLE, s32decode, s32encode } from './s32.ts';
6
6
7
7
let lastTimestamp = 0;
8
8
let lastCurrentTime = 0;
···
13
13
* Creates a TID based off provided timestamp and clockid, with no validation.
14
14
*/
15
15
export const createRaw = (timestamp: number, clockid: number): string => {
16
16
-
return s32encode(timestamp).padStart(11, '2') + s32encode(clockid).padStart(2, '2');
16
16
+
return s32encode(timestamp).padStart(11, '2') + S32_2CHAR_TABLE[clockid]!;
17
17
};
18
18
19
19
/**
···
59
59
throw new Error(`invalid TID`);
60
60
}
61
61
62
62
-
const timestamp = s32decode(tid.slice(0, 11));
63
63
-
const clockid = s32decode(tid.slice(11, 13));
62
62
+
const timestamp = s32decode(tid, 0, 11);
63
63
+
const clockid = s32decode(tid, 11, 2);
64
64
65
65
-
return { timestamp: timestamp, clockid: clockid };
65
65
+
return { timestamp, clockid };
66
66
};
67
67
68
68
/**
+28
-3
packages/utilities/tid/lib/s32.ts
···
1
1
const S32_CHAR = '234567abcdefghijklmnopqrstuvwxyz';
2
2
3
3
+
const S32_DECODE_TABLE = (() => {
4
4
+
const table = new Int16Array(123);
5
5
+
table.fill(-1);
6
6
+
7
7
+
for (let i = 0; i < S32_CHAR.length; i++) {
8
8
+
const code = S32_CHAR.charCodeAt(i);
9
9
+
table[code] = i;
10
10
+
}
11
11
+
12
12
+
return table;
13
13
+
})();
14
14
+
15
15
+
export const S32_2CHAR_TABLE = (() => {
16
16
+
const table = Array.from<string>({ length: 1024 });
17
17
+
18
18
+
for (let i = 0; i < 1024; i++) {
19
19
+
const hi = S32_CHAR.charAt((i >> 5) & 31);
20
20
+
const lo = S32_CHAR.charAt(i & 31);
21
21
+
table[i] = hi + lo;
22
22
+
}
23
23
+
24
24
+
return table;
25
25
+
})();
26
26
+
3
27
export const s32encode = (i: number): string => {
4
28
let s = '';
5
29
···
12
36
return s;
13
37
};
14
38
15
15
-
export const s32decode = (s: string): number => {
39
39
+
export const s32decode = (s: string, offset: number, length: number): number => {
16
40
let i = 0;
41
41
+
const end = offset + length;
17
42
18
18
-
for (const c of s) {
19
19
-
i = i * 32 + S32_CHAR.indexOf(c);
43
43
+
for (let idx = offset; idx < end; idx++) {
44
44
+
i = i * 32 + S32_DECODE_TABLE[s.charCodeAt(idx)]!;
20
45
}
21
46
22
47
return i;