sol-trade-sdk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +390 -0
- package/dist/chunk-MMQAMIKR.mjs +3735 -0
- package/dist/chunk-NEZDFAYA.mjs +7744 -0
- package/dist/clients-VITWK7B6.mjs +1370 -0
- package/dist/index-1BK_FXsW.d.mts +2327 -0
- package/dist/index-1BK_FXsW.d.ts +2327 -0
- package/dist/index.d.mts +2659 -0
- package/dist/index.d.ts +2659 -0
- package/dist/index.js +13265 -0
- package/dist/index.mjs +562 -0
- package/dist/perf/index.d.mts +2 -0
- package/dist/perf/index.d.ts +2 -0
- package/dist/perf/index.js +3742 -0
- package/dist/perf/index.mjs +214 -0
- package/package.json +101 -0
- package/src/__tests__/complete_sdk.test.ts +354 -0
- package/src/__tests__/hotpath.test.ts +486 -0
- package/src/__tests__/nonce.test.ts +45 -0
- package/src/__tests__/sdk.test.ts +425 -0
- package/src/address-lookup/index.ts +197 -0
- package/src/cache/cache.ts +308 -0
- package/src/calc/index.ts +1058 -0
- package/src/calc/pumpfun.ts +124 -0
- package/src/common/bonding_curve.ts +272 -0
- package/src/common/compute-budget.ts +148 -0
- package/src/common/confirm-any-signature.ts +184 -0
- package/src/common/fast-timing.ts +481 -0
- package/src/common/fast_fn.ts +150 -0
- package/src/common/gas-fee-strategy.ts +253 -0
- package/src/common/map-pool.ts +23 -0
- package/src/common/nonce.ts +40 -0
- package/src/common/sdk-log.ts +460 -0
- package/src/common/seed.ts +381 -0
- package/src/common/spl-token.ts +578 -0
- package/src/common/subscription-handle.ts +644 -0
- package/src/common/trading-utils.ts +239 -0
- package/src/common/wsol-manager.ts +325 -0
- package/src/compute/compute_budget_manager.ts +187 -0
- package/src/compute/index.ts +21 -0
- package/src/constants/index.ts +96 -0
- package/src/execution/execution.ts +532 -0
- package/src/execution/index.ts +42 -0
- package/src/hotpath/executor.ts +464 -0
- package/src/hotpath/index.ts +64 -0
- package/src/hotpath/state.ts +435 -0
- package/src/index.ts +2117 -0
- package/src/instruction/bonk_builder.ts +730 -0
- package/src/instruction/index.ts +24 -0
- package/src/instruction/meteora_damm_v2_builder.ts +509 -0
- package/src/instruction/pumpfun_builder.ts +1183 -0
- package/src/instruction/pumpswap.ts +1123 -0
- package/src/instruction/raydium_amm_v4_builder.ts +692 -0
- package/src/instruction/raydium_cpmm_builder.ts +795 -0
- package/src/middleware/traits.ts +407 -0
- package/src/params/index.ts +483 -0
- package/src/perf/compiler-optimization.ts +529 -0
- package/src/perf/hardware.ts +631 -0
- package/src/perf/index.ts +9 -0
- package/src/perf/kernel-bypass.ts +656 -0
- package/src/perf/protocol.ts +682 -0
- package/src/perf/realtime.ts +592 -0
- package/src/perf/simd.ts +668 -0
- package/src/perf/syscall-bypass.ts +331 -0
- package/src/perf/ultra-low-latency.ts +505 -0
- package/src/perf/zero-copy.ts +589 -0
- package/src/pool/pool.ts +294 -0
- package/src/rpc/client.ts +345 -0
- package/src/sdk-errors.ts +13 -0
- package/src/security/index.ts +26 -0
- package/src/security/secure-key.ts +303 -0
- package/src/security/validators.ts +281 -0
- package/src/seed/pda.ts +262 -0
- package/src/serialization/index.ts +28 -0
- package/src/serialization/serialization.ts +288 -0
- package/src/swqos/clients.ts +1754 -0
- package/src/swqos/index.ts +50 -0
- package/src/swqos/providers.ts +1707 -0
- package/src/trading/core/async-executor.ts +702 -0
- package/src/trading/core/confirmation-monitor.ts +711 -0
- package/src/trading/core/index.ts +82 -0
- package/src/trading/core/retry-handler.ts +683 -0
- package/src/trading/core/transaction-pool.ts +780 -0
- package/src/trading/executor.ts +385 -0
- package/src/trading/factory.ts +282 -0
- package/src/trading/index.ts +30 -0
- package/src/types.ts +8 -0
- package/src/utils/index.ts +155 -0
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hardware Optimization Module for Sol Trade SDK
|
|
3
|
+
* Provides CPU affinity, NUMA optimization, and cache optimizations.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// ===== Types =====
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* CPU topology information
|
|
10
|
+
*/
|
|
11
|
+
export interface CPUTopology {
|
|
12
|
+
physicalCores: number;
|
|
13
|
+
logicalCores: number;
|
|
14
|
+
numaNodes: number;
|
|
15
|
+
cacheLevels: CacheInfo[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Cache information
|
|
20
|
+
*/
|
|
21
|
+
export interface CacheInfo {
|
|
22
|
+
level: number;
|
|
23
|
+
size: number;
|
|
24
|
+
lineSize: number;
|
|
25
|
+
associativity: number;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* CPU affinity configuration
|
|
30
|
+
*/
|
|
31
|
+
export interface AffinityConfig {
|
|
32
|
+
/** Enable CPU pinning */
|
|
33
|
+
enablePinning: boolean;
|
|
34
|
+
/** Preferred CPU cores */
|
|
35
|
+
preferredCores: number[];
|
|
36
|
+
/** Avoid hyperthreading pairs */
|
|
37
|
+
avoidSMT: boolean;
|
|
38
|
+
/** NUMA node preference */
|
|
39
|
+
numaNode: number;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Default affinity configuration
|
|
44
|
+
*/
|
|
45
|
+
export function defaultAffinityConfig(): AffinityConfig {
|
|
46
|
+
return {
|
|
47
|
+
enablePinning: false,
|
|
48
|
+
preferredCores: [],
|
|
49
|
+
avoidSMT: true,
|
|
50
|
+
numaNode: 0,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* NUMA configuration
|
|
56
|
+
*/
|
|
57
|
+
export interface NUMAConfig {
|
|
58
|
+
/** Enable NUMA-aware allocation */
|
|
59
|
+
enableNUMA: boolean;
|
|
60
|
+
/** Preferred NUMA node */
|
|
61
|
+
preferredNode: number;
|
|
62
|
+
/** Interleave memory across nodes */
|
|
63
|
+
interleave: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Default NUMA configuration
|
|
68
|
+
*/
|
|
69
|
+
export function defaultNUMAConfig(): NUMAConfig {
|
|
70
|
+
return {
|
|
71
|
+
enableNUMA: false,
|
|
72
|
+
preferredNode: 0,
|
|
73
|
+
interleave: false,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ===== CPU Affinity =====
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* CPU affinity manager for pinning threads to specific cores.
|
|
81
|
+
* Reduces context switches and improves cache locality.
|
|
82
|
+
*/
|
|
83
|
+
export class CPUAffinity {
|
|
84
|
+
private config: AffinityConfig;
|
|
85
|
+
private pinnedCores: Set<number> = new Set();
|
|
86
|
+
|
|
87
|
+
constructor(config: AffinityConfig = defaultAffinityConfig()) {
|
|
88
|
+
this.config = config;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Get CPU topology information
|
|
93
|
+
*/
|
|
94
|
+
getTopology(): CPUTopology {
|
|
95
|
+
// In Node.js, we have limited access to CPU info
|
|
96
|
+
const os = require('os');
|
|
97
|
+
const cpus = os.cpus();
|
|
98
|
+
|
|
99
|
+
// Estimate physical cores (this is approximate)
|
|
100
|
+
const logicalCores = cpus.length;
|
|
101
|
+
const physicalCores = Math.ceil(logicalCores / 2); // Assume SMT
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
physicalCores,
|
|
105
|
+
logicalCores,
|
|
106
|
+
numaNodes: 1, // Node.js doesn't expose NUMA info
|
|
107
|
+
cacheLevels: [
|
|
108
|
+
{ level: 1, size: 32 * 1024, lineSize: 64, associativity: 8 },
|
|
109
|
+
{ level: 2, size: 256 * 1024, lineSize: 64, associativity: 8 },
|
|
110
|
+
{ level: 3, size: 8 * 1024 * 1024, lineSize: 64, associativity: 16 },
|
|
111
|
+
],
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Pin current execution context to a specific core
|
|
117
|
+
* Note: In Node.js, this is advisory only via worker threads
|
|
118
|
+
*/
|
|
119
|
+
pinToCore(coreId: number): boolean {
|
|
120
|
+
if (!this.config.enablePinning) {
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Node.js doesn't support true CPU pinning
|
|
125
|
+
// This is a placeholder for potential native addon integration
|
|
126
|
+
this.pinnedCores.add(coreId);
|
|
127
|
+
return true;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Pin to multiple cores
|
|
132
|
+
*/
|
|
133
|
+
pinToCores(coreIds: number[]): boolean {
|
|
134
|
+
if (!this.config.enablePinning) {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
for (const coreId of coreIds) {
|
|
139
|
+
this.pinnedCores.add(coreId);
|
|
140
|
+
}
|
|
141
|
+
return true;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Get recommended cores for pinning
|
|
146
|
+
*/
|
|
147
|
+
getRecommendedCores(count: number = 1): number[] {
|
|
148
|
+
const topology = this.getTopology();
|
|
149
|
+
const cores: number[] = [];
|
|
150
|
+
|
|
151
|
+
if (this.config.preferredCores.length > 0) {
|
|
152
|
+
// Use preferred cores
|
|
153
|
+
for (let i = 0; i < count && i < this.config.preferredCores.length; i++) {
|
|
154
|
+
cores.push(this.config.preferredCores[i]!);
|
|
155
|
+
}
|
|
156
|
+
} else {
|
|
157
|
+
// Use first N physical cores (avoiding SMT if configured)
|
|
158
|
+
const step = this.config.avoidSMT ? 2 : 1;
|
|
159
|
+
for (let i = 0; i < count * step && i < topology.physicalCores; i += step) {
|
|
160
|
+
cores.push(i);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return cores;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Check if a core is available
|
|
169
|
+
*/
|
|
170
|
+
isCoreAvailable(coreId: number): boolean {
|
|
171
|
+
const topology = this.getTopology();
|
|
172
|
+
return coreId >= 0 && coreId < topology.logicalCores;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Get current core (simulated)
|
|
177
|
+
*/
|
|
178
|
+
getCurrentCore(): number {
|
|
179
|
+
// Node.js doesn't expose this information
|
|
180
|
+
return 0;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Get pinned cores
|
|
185
|
+
*/
|
|
186
|
+
getPinnedCores(): number[] {
|
|
187
|
+
return Array.from(this.pinnedCores);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Unpin all cores
|
|
192
|
+
*/
|
|
193
|
+
unpinAll(): void {
|
|
194
|
+
this.pinnedCores.clear();
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ===== NUMA Optimizer =====
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* NUMA (Non-Uniform Memory Access) optimizer.
|
|
202
|
+
* Optimizes memory allocation for NUMA architectures.
|
|
203
|
+
*/
|
|
204
|
+
export class NUMAOptimizer {
|
|
205
|
+
private config: NUMAConfig;
|
|
206
|
+
private allocations: Map<string, NUMAAllocation> = new Map();
|
|
207
|
+
|
|
208
|
+
constructor(config: NUMAConfig = defaultNUMAConfig()) {
|
|
209
|
+
this.config = config;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Allocate memory with NUMA awareness
|
|
214
|
+
*/
|
|
215
|
+
allocate(size: number, tag: string = 'default'): ArrayBuffer {
|
|
216
|
+
// In JavaScript, we can't control NUMA placement
|
|
217
|
+
// This is a placeholder for potential WASM/native integration
|
|
218
|
+
const buffer = new ArrayBuffer(size);
|
|
219
|
+
|
|
220
|
+
this.allocations.set(tag, {
|
|
221
|
+
buffer,
|
|
222
|
+
size,
|
|
223
|
+
node: this.config.preferredNode,
|
|
224
|
+
timestamp: Date.now(),
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
return buffer;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Allocate typed array with NUMA awareness
|
|
232
|
+
*/
|
|
233
|
+
allocateTypedArray<T extends TypedArray>(
|
|
234
|
+
constructor: TypedArrayConstructor<T>,
|
|
235
|
+
length: number,
|
|
236
|
+
tag: string = 'default'
|
|
237
|
+
): T {
|
|
238
|
+
const bytesPerElement = constructor.BYTES_PER_ELEMENT;
|
|
239
|
+
const buffer = this.allocate(length * bytesPerElement, tag);
|
|
240
|
+
return new constructor(buffer) as T;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* Get allocation information
|
|
245
|
+
*/
|
|
246
|
+
getAllocation(tag: string): NUMAAllocation | undefined {
|
|
247
|
+
return this.allocations.get(tag);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Get local memory node for current thread
|
|
252
|
+
*/
|
|
253
|
+
getLocalNode(): number {
|
|
254
|
+
// Node.js doesn't expose NUMA info
|
|
255
|
+
return this.config.preferredNode;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Get number of NUMA nodes
|
|
260
|
+
*/
|
|
261
|
+
getNodeCount(): number {
|
|
262
|
+
// Node.js doesn't expose NUMA info
|
|
263
|
+
return 1;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Check if NUMA is available
|
|
268
|
+
*/
|
|
269
|
+
isNUMAAvailable(): boolean {
|
|
270
|
+
return false; // Not available in Node.js
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Migrate allocation to preferred node
|
|
275
|
+
* Note: No-op in JavaScript
|
|
276
|
+
*/
|
|
277
|
+
migrateToNode(tag: string, node: number): boolean {
|
|
278
|
+
const allocation = this.allocations.get(tag);
|
|
279
|
+
if (!allocation) {
|
|
280
|
+
return false;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
allocation.node = node;
|
|
284
|
+
// Actual migration not possible in JS
|
|
285
|
+
return true;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Free allocation tracking
|
|
290
|
+
*/
|
|
291
|
+
free(tag: string): void {
|
|
292
|
+
this.allocations.delete(tag);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Get all allocations
|
|
297
|
+
*/
|
|
298
|
+
getAllAllocations(): Map<string, NUMAAllocation> {
|
|
299
|
+
return new Map(this.allocations);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* NUMA allocation tracking
|
|
305
|
+
*/
|
|
306
|
+
interface NUMAAllocation {
|
|
307
|
+
buffer: ArrayBuffer;
|
|
308
|
+
size: number;
|
|
309
|
+
node: number;
|
|
310
|
+
timestamp: number;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/**
|
|
314
|
+
* Typed array types
|
|
315
|
+
*/
|
|
316
|
+
type TypedArray = Uint8Array | Uint16Array | Uint32Array | BigUint64Array |
|
|
317
|
+
Int8Array | Int16Array | Int32Array | BigInt64Array |
|
|
318
|
+
Float32Array | Float64Array;
|
|
319
|
+
|
|
320
|
+
interface TypedArrayConstructor<T> {
|
|
321
|
+
new (buffer: ArrayBuffer): T;
|
|
322
|
+
BYTES_PER_ELEMENT: number;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// ===== Cache Optimizer =====
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Cache optimization utilities.
|
|
329
|
+
* Provides cache-friendly data structure layouts and prefetching hints.
|
|
330
|
+
*/
|
|
331
|
+
export class CacheOptimizer {
|
|
332
|
+
private cacheLineSize: number = 64;
|
|
333
|
+
private l1Size: number = 32 * 1024;
|
|
334
|
+
private l2Size: number = 256 * 1024;
|
|
335
|
+
private l3Size: number = 8 * 1024 * 1024;
|
|
336
|
+
|
|
337
|
+
constructor() {
|
|
338
|
+
// Default cache sizes - can be overridden
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Set cache parameters
|
|
343
|
+
*/
|
|
344
|
+
setCacheParams(params: {
|
|
345
|
+
cacheLineSize?: number;
|
|
346
|
+
l1Size?: number;
|
|
347
|
+
l2Size?: number;
|
|
348
|
+
l3Size?: number;
|
|
349
|
+
}): void {
|
|
350
|
+
if (params.cacheLineSize) this.cacheLineSize = params.cacheLineSize;
|
|
351
|
+
if (params.l1Size) this.l1Size = params.l1Size;
|
|
352
|
+
if (params.l2Size) this.l2Size = params.l2Size;
|
|
353
|
+
if (params.l3Size) this.l3Size = params.l3Size;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Align size to cache line boundary
|
|
358
|
+
*/
|
|
359
|
+
alignToCacheLine(size: number): number {
|
|
360
|
+
const mask = this.cacheLineSize - 1;
|
|
361
|
+
return (size + mask) & ~mask;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Check if address is cache line aligned
|
|
366
|
+
*/
|
|
367
|
+
isCacheLineAligned(address: number): boolean {
|
|
368
|
+
return (address & (this.cacheLineSize - 1)) === 0;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Calculate cache-friendly array stride
|
|
373
|
+
*/
|
|
374
|
+
calculateStride(elementSize: number): number {
|
|
375
|
+
// Ensure elements don't share cache lines
|
|
376
|
+
return Math.max(elementSize, this.cacheLineSize);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Create cache-friendly layout for array of objects
|
|
381
|
+
*/
|
|
382
|
+
createSoALayout<T extends Record<string, number | bigint>>(
|
|
383
|
+
count: number,
|
|
384
|
+
schema: Record<keyof T, 'u8' | 'u16' | 'u32' | 'u64' | 'f32' | 'f64'>
|
|
385
|
+
): StructureOfArrays<T> {
|
|
386
|
+
const typeSizes: Record<string, number> = {
|
|
387
|
+
u8: 1, u16: 2, u32: 4, u64: 8, f32: 4, f64: 8,
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
const arrays: Partial<Record<keyof T, TypedArray>> = {};
|
|
391
|
+
const offsets: Partial<Record<keyof T, number>> = {};
|
|
392
|
+
let currentOffset = 0;
|
|
393
|
+
|
|
394
|
+
for (const [key, type] of Object.entries(schema)) {
|
|
395
|
+
const size = typeSizes[type];
|
|
396
|
+
if (size === undefined) {
|
|
397
|
+
throw new Error(`Unsupported struct field type: ${type}`);
|
|
398
|
+
}
|
|
399
|
+
const alignedOffset = this.alignToCacheLine(currentOffset);
|
|
400
|
+
offsets[key as keyof T] = alignedOffset;
|
|
401
|
+
|
|
402
|
+
// Create appropriate typed array
|
|
403
|
+
const totalSize = alignedOffset + count * size;
|
|
404
|
+
const buffer = new ArrayBuffer(totalSize);
|
|
405
|
+
|
|
406
|
+
switch (type) {
|
|
407
|
+
case 'u8':
|
|
408
|
+
arrays[key as keyof T] = new Uint8Array(buffer, alignedOffset, count) as any;
|
|
409
|
+
break;
|
|
410
|
+
case 'u16':
|
|
411
|
+
arrays[key as keyof T] = new Uint16Array(buffer, alignedOffset, count) as any;
|
|
412
|
+
break;
|
|
413
|
+
case 'u32':
|
|
414
|
+
arrays[key as keyof T] = new Uint32Array(buffer, alignedOffset, count) as any;
|
|
415
|
+
break;
|
|
416
|
+
case 'u64':
|
|
417
|
+
arrays[key as keyof T] = new BigUint64Array(buffer, alignedOffset, count) as any;
|
|
418
|
+
break;
|
|
419
|
+
case 'f32':
|
|
420
|
+
arrays[key as keyof T] = new Float32Array(buffer, alignedOffset, count) as any;
|
|
421
|
+
break;
|
|
422
|
+
case 'f64':
|
|
423
|
+
arrays[key as keyof T] = new Float64Array(buffer, alignedOffset, count) as any;
|
|
424
|
+
break;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
currentOffset = alignedOffset + count * size;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
return {
|
|
431
|
+
arrays: arrays as Record<keyof T, TypedArray>,
|
|
432
|
+
offsets: offsets as Record<keyof T, number>,
|
|
433
|
+
count,
|
|
434
|
+
get(index: number): T {
|
|
435
|
+
const result = {} as T;
|
|
436
|
+
for (const key of Object.keys(arrays) as Array<keyof T>) {
|
|
437
|
+
result[key] = (arrays[key] as any)[index];
|
|
438
|
+
}
|
|
439
|
+
return result;
|
|
440
|
+
},
|
|
441
|
+
set(index: number, value: T): void {
|
|
442
|
+
for (const key of Object.keys(arrays) as Array<keyof T>) {
|
|
443
|
+
(arrays[key] as any)[index] = value[key];
|
|
444
|
+
}
|
|
445
|
+
},
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Get cache line size
|
|
451
|
+
*/
|
|
452
|
+
getCacheLineSize(): number {
|
|
453
|
+
return this.cacheLineSize;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
/**
|
|
457
|
+
* Get L1 cache size
|
|
458
|
+
*/
|
|
459
|
+
getL1Size(): number {
|
|
460
|
+
return this.l1Size;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/**
|
|
464
|
+
* Get L2 cache size
|
|
465
|
+
*/
|
|
466
|
+
getL2Size(): number {
|
|
467
|
+
return this.l2Size;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Get L3 cache size
|
|
472
|
+
*/
|
|
473
|
+
getL3Size(): number {
|
|
474
|
+
return this.l3Size;
|
|
475
|
+
}
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Structure of Arrays layout
|
|
480
|
+
*/
|
|
481
|
+
interface StructureOfArrays<T> {
|
|
482
|
+
arrays: Record<keyof T, TypedArray>;
|
|
483
|
+
offsets: Record<keyof T, number>;
|
|
484
|
+
count: number;
|
|
485
|
+
get(index: number): T;
|
|
486
|
+
set(index: number, value: T): void;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
// ===== Hardware Monitor =====
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Hardware performance monitoring
|
|
493
|
+
*/
|
|
494
|
+
export class HardwareMonitor {
|
|
495
|
+
private startTime: number = 0;
|
|
496
|
+
private measurements: HardwareMeasurement[] = [];
|
|
497
|
+
|
|
498
|
+
/**
|
|
499
|
+
* Start monitoring
|
|
500
|
+
*/
|
|
501
|
+
start(): void {
|
|
502
|
+
this.startTime = performance.now();
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Take a measurement
|
|
507
|
+
*/
|
|
508
|
+
measure(label: string): void {
|
|
509
|
+
this.measurements.push({
|
|
510
|
+
label,
|
|
511
|
+
timestamp: performance.now() - this.startTime,
|
|
512
|
+
memory: this.getMemoryInfo(),
|
|
513
|
+
});
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
/**
|
|
517
|
+
* Get memory information
|
|
518
|
+
*/
|
|
519
|
+
getMemoryInfo(): MemoryInfo {
|
|
520
|
+
if (typeof process !== 'undefined' && process.memoryUsage) {
|
|
521
|
+
const usage = process.memoryUsage();
|
|
522
|
+
return {
|
|
523
|
+
used: usage.heapUsed,
|
|
524
|
+
total: usage.heapTotal,
|
|
525
|
+
external: usage.external,
|
|
526
|
+
rss: usage.rss,
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
|
|
530
|
+
if (typeof performance !== 'undefined' && (performance as any).memory) {
|
|
531
|
+
const mem = (performance as any).memory;
|
|
532
|
+
return {
|
|
533
|
+
used: mem.usedJSHeapSize,
|
|
534
|
+
total: mem.totalJSHeapSize,
|
|
535
|
+
external: 0,
|
|
536
|
+
rss: 0,
|
|
537
|
+
};
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
return { used: 0, total: 0, external: 0, rss: 0 };
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Get all measurements
|
|
545
|
+
*/
|
|
546
|
+
getMeasurements(): HardwareMeasurement[] {
|
|
547
|
+
return [...this.measurements];
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/**
|
|
551
|
+
* Reset measurements
|
|
552
|
+
*/
|
|
553
|
+
reset(): void {
|
|
554
|
+
this.measurements = [];
|
|
555
|
+
this.startTime = performance.now();
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
/**
|
|
560
|
+
* Memory information
|
|
561
|
+
*/
|
|
562
|
+
interface MemoryInfo {
|
|
563
|
+
used: number;
|
|
564
|
+
total: number;
|
|
565
|
+
external: number;
|
|
566
|
+
rss: number;
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
/**
|
|
570
|
+
* Hardware measurement
|
|
571
|
+
*/
|
|
572
|
+
interface HardwareMeasurement {
|
|
573
|
+
label: string;
|
|
574
|
+
timestamp: number;
|
|
575
|
+
memory: MemoryInfo;
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
// ===== Convenience Functions =====
|
|
579
|
+
|
|
580
|
+
/**
|
|
581
|
+
* Create a CPU affinity manager
|
|
582
|
+
*/
|
|
583
|
+
export function createCPUAffinity(config?: Partial<AffinityConfig>): CPUAffinity {
|
|
584
|
+
const fullConfig = { ...defaultAffinityConfig(), ...config };
|
|
585
|
+
return new CPUAffinity(fullConfig);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
/**
|
|
589
|
+
* Create a NUMA optimizer
|
|
590
|
+
*/
|
|
591
|
+
export function createNUMAOptimizer(config?: Partial<NUMAConfig>): NUMAOptimizer {
|
|
592
|
+
const fullConfig = { ...defaultNUMAConfig(), ...config };
|
|
593
|
+
return new NUMAOptimizer(fullConfig);
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/**
|
|
597
|
+
* Create a cache optimizer
|
|
598
|
+
*/
|
|
599
|
+
export function createCacheOptimizer(): CacheOptimizer {
|
|
600
|
+
return new CacheOptimizer();
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
/**
|
|
604
|
+
* Create a hardware monitor
|
|
605
|
+
*/
|
|
606
|
+
export function createHardwareMonitor(): HardwareMonitor {
|
|
607
|
+
return new HardwareMonitor();
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
/**
|
|
611
|
+
* Get system CPU information
|
|
612
|
+
*/
|
|
613
|
+
export function getSystemCPUInfo(): { cores: number; model: string } {
|
|
614
|
+
const os = require('os');
|
|
615
|
+
const cpus = os.cpus();
|
|
616
|
+
return {
|
|
617
|
+
cores: cpus.length,
|
|
618
|
+
model: cpus[0]?.model || 'Unknown',
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
/**
|
|
623
|
+
* Get system memory information
|
|
624
|
+
*/
|
|
625
|
+
export function getSystemMemoryInfo(): { total: number; free: number } {
|
|
626
|
+
const os = require('os');
|
|
627
|
+
return {
|
|
628
|
+
total: os.totalmem(),
|
|
629
|
+
free: os.freemem(),
|
|
630
|
+
};
|
|
631
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './compiler-optimization';
|
|
2
|
+
export * from './hardware';
|
|
3
|
+
export * from './kernel-bypass';
|
|
4
|
+
export * from './protocol';
|
|
5
|
+
export * from './realtime';
|
|
6
|
+
export * from './simd';
|
|
7
|
+
export * from './syscall-bypass';
|
|
8
|
+
export * from './ultra-low-latency';
|
|
9
|
+
export * from './zero-copy';
|