diff --git a/.gitignore b/.gitignore index b86fa12a..18327e16 100644 --- a/.gitignore +++ b/.gitignore @@ -7,7 +7,6 @@ yarn-error.log* # 构建输出 bin/ dist/ -wasm/ *.tgz # TypeScript @@ -49,11 +48,6 @@ logs/ coverage/ *.lcov -# Rust 构建文件 -src/wasm/*/target/ -src/wasm/*/pkg/ -Cargo.lock - # 包管理器锁文件(保留npm的,忽略其他的) yarn.lock pnpm-lock.yaml diff --git a/.npmignore b/.npmignore index d2a21cad..07701740 100644 --- a/.npmignore +++ b/.npmignore @@ -23,12 +23,6 @@ node_modules/ *.tmp *.temp -# Rust 构建文件(保留编译后的WASM) -src/wasm/rust-ecs-core/target/ -src/wasm/rust-ecs-core/Cargo.lock -src/wasm/rust-ecs-core/pkg/ -!bin/wasm/ - # 文档草稿 docs/draft/ *.draft.md diff --git a/README.md b/README.md index 5bfa1549..112c4e97 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ eventBus.emit('player:levelup', { playerId: 123, newLevel: 5 }); | **TypeScript 支持** | ✅ 原生支持 | ✅ 完整支持 | ⚠️ 部分支持 | ✅ 原生支持 | | **事件系统** | ✅ 类型安全+装饰器 | ❌ 无内置事件系统 | ⚠️ 基础事件 | ✅ 响应式事件 | | **查询系统** | ✅ 智能查询+流式API | ✅ 高性能 | ✅ 基础查询 | ✅ 响应式查询 | -| **性能优化** | ✅ 多层优化系统 | ✅ WASM优化 | ⚠️ 基础优化 | ✅ React集成优化 | +| **性能优化** | ✅ 多层优化系统 | ✅ 高性能优化 | ⚠️ 基础优化 | ✅ React集成优化 | | **实体管理器** | ✅ 统一管理接口 | ❌ 无统一接口 | ✅ 基础管理 | ✅ 响应式管理 | | **组件索引** | ✅ 哈希+位图索引 | ✅ 原生支持 | ❌ 无索引系统 | ✅ 自动索引 | | **Archetype系统** | ✅ 内置支持 | ✅ 内置支持 | ❌ 无Archetype | ❌ 无Archetype | diff --git a/package.json b/package.json index b7ba4688..c05054c0 100644 --- a/package.json +++ b/package.json @@ -19,19 +19,15 @@ "egret" ], "scripts": { - "clean": "rimraf bin wasm dist", - "clean:wasm": "rimraf src/wasm/rust-ecs-core/pkg src/wasm/rust-ecs-core/target", - "build:wasm": "cd src/wasm/rust-ecs-core && wasm-pack build --target web --out-dir ../../../bin/wasm --release", + "clean": "rimraf bin dist", "build:ts": "tsc", "prebuild": "npm run clean", - "build": "npm run build:wasm && npm run build:ts", + "build": "npm run build:ts", "build:watch": "tsc --watch", - "rebuild": "npm run clean && npm run clean:wasm && npm run build", + "rebuild": "npm run clean && npm run build", "build:npm": "npm run build && node scripts/build-rollup.js", - "build:wasm-release": "node scripts/build-wasm-release.js", - "test:benchmark": "npm run build && node bin/Testing/Performance/benchmark.js", - "test:unit": "npm run build && node bin/Testing/test-runner.js", - "benchmark": "node scripts/benchmark.js", + + "preversion": "npm run rebuild", "publish:patch": "npm version patch && npm run build:npm && cd dist && npm publish", "publish:minor": "npm version minor && npm run build:npm && cd dist && npm publish", diff --git a/scripts/benchmark.js b/scripts/benchmark.js deleted file mode 100644 index dde556fa..00000000 --- a/scripts/benchmark.js +++ /dev/null @@ -1,39 +0,0 @@ -#!/usr/bin/env node - -/** - * ECS框架性能基准测试入口 - * - * 使用方法: - * node benchmark.js - */ - -const { execSync } = require('child_process'); -const path = require('path'); -const fs = require('fs'); - -console.log('🚀 启动ECS框架性能基准测试...\n'); - -const sourceDir = path.join(__dirname, '..'); - -try { - console.log('📦 准备构建项目...'); - - // 构建TypeScript代码 - console.log('🔨 构建TypeScript代码...'); - execSync('npm run build', { - stdio: 'inherit', - cwd: sourceDir - }); - console.log('✅ TypeScript构建完成\n'); - - // 运行性能测试 - console.log('🏃 运行性能基准测试...'); - execSync('node bin/Testing/Performance/benchmark.js', { - stdio: 'inherit', - cwd: sourceDir - }); - -} catch (error) { - console.error('❌ 性能测试失败:', error.message); - process.exit(1); -} \ No newline at end of file diff --git a/scripts/build-rollup.js b/scripts/build-rollup.js index 3ab51e99..8fdfbfbf 100644 --- a/scripts/build-rollup.js +++ b/scripts/build-rollup.js @@ -106,9 +106,6 @@ function copyFiles() { console.log(` ⚠️ 文件不存在: ${src}`); } }); - - // WASM文件不再包含在npm包中,单独发布 - console.log(' ⚠️ WASM文件已移除,请从GitHub Release下载单独的WASM包'); } function showBuildResults() { diff --git a/scripts/build-wasm-release.js b/scripts/build-wasm-release.js deleted file mode 100644 index b37dfa31..00000000 --- a/scripts/build-wasm-release.js +++ /dev/null @@ -1,214 +0,0 @@ -const fs = require('fs'); -const path = require('path'); -const { execSync } = require('child_process'); - -console.log('🚀 构建 WASM 发布包...'); - -async function main() { - try { - // 构建通用版本 - console.log('🌐 构建通用 WASM 版本...'); - await buildUniversalVersion(); - - console.log('\n✅ WASM 发布包构建完成!'); - console.log('📦 输出目录: wasm-release/'); - console.log('💡 可以将整个目录打包为 zip 文件上传到 GitHub Release'); - - } catch (error) { - console.error('❌ 构建失败:', error.message); - process.exit(1); - } -} - - - -async function buildUniversalVersion() { - // 确保通用 WASM 已构建 - if (!fs.existsSync('./bin/wasm')) { - console.log('📦 构建通用 WASM...'); - execSync('npm run build:wasm', { stdio: 'inherit' }); - } - - // 创建发布目录 - const releaseDir = './wasm-release'; - if (fs.existsSync(releaseDir)) { - execSync(`rimraf ${releaseDir}`, { stdio: 'inherit' }); - } - fs.mkdirSync(releaseDir); - - // 复制 WASM 文件 - console.log('📁 复制 WASM 文件...'); - const wasmDir = './bin/wasm'; - fs.readdirSync(wasmDir).forEach(file => { - if (file !== '.gitignore') { - fs.copyFileSync( - path.join(wasmDir, file), - path.join(releaseDir, file) - ); - console.log(` ✓ ${file}`); - } - }); - - // 生成包信息 - console.log('📋 生成包信息...'); - generatePackageInfo(releaseDir); - - // 创建使用说明 - console.log('📋 生成使用说明...'); - generateUsageInstructions(releaseDir); - - // 显示结果 - showReleaseResults(releaseDir); -} - -function generatePackageInfo(releaseDir) { - const packageInfo = { - name: "@esengine/ecs-framework-wasm", - version: "1.0.0", - description: "ECS Framework WASM 加速模块", - main: "ecs_wasm_core.js", - files: [ - "ecs_wasm_core.js", - "ecs_wasm_core_bg.wasm", - "*.d.ts", - "README.md" - ], - keywords: ["ecs", "wasm", "game-engine", "performance"], - author: "ESEngine Team", - license: "MIT", - peerDependencies: { - "@esengine/ecs-framework": "^1.0.0" - } - }; - - fs.writeFileSync( - path.join(releaseDir, 'package.json'), - JSON.stringify(packageInfo, null, 2) - ); -} - -function generateUsageInstructions(releaseDir) { - const instructions = `# ECS Framework WASM 支持包 - -这个包包含了 @esengine/ecs-framework 的 WASM 加速模块。 - -## 包含文件 - -- \`ecs_wasm_core.js\` - WASM 胶水代码 -- \`ecs_wasm_core.d.ts\` - TypeScript 类型定义 -- \`ecs_wasm_core_bg.wasm\` - WASM 二进制文件 -- \`ecs_wasm_core_bg.wasm.d.ts\` - WASM 类型定义 -- \`package.json\` - 包信息 - -## 使用方法 - -### Node.js 环境 - -\`\`\`javascript -import init, { EcsCore } from './ecs_wasm_core.js'; - -async function useWasm() { - // 初始化 WASM 模块 - await init(); - - // 创建 ECS 核心实例 - const ecsCore = new EcsCore(); - - // 使用 WASM 加速的 ECS 功能 - const entity = ecsCore.create_entity(); - console.log('创建实体:', entity); -} - -useWasm(); -\`\`\` - -### 浏览器环境 - -\`\`\`html - - - - - - -

ECS Framework WASM Demo

- - -\`\`\` - -### TypeScript 支持 - -确保包含类型定义: - -\`\`\`typescript -import init, { EcsCore } from './ecs_wasm_core.js'; - -async function typedExample(): Promise { - await init(); - - const ecsCore = new EcsCore(); - const entityId: number = ecsCore.create_entity(); - - // 使用类型安全的 API - const mask = BigInt(0b1010); - ecsCore.update_entity_mask(entityId, mask); -} -\`\`\` - -## 性能优势 - -WASM 模块主要优化以下操作: - -- 🚀 实体查询(10-100x 性能提升) -- 🔥 组件掩码操作 -- ⚡ 批量实体处理 - -## 兼容性 - -- **浏览器**: 支持 WebAssembly 的现代浏览器 -- **Node.js**: 16.0+ 版本 -- **TypeScript**: 4.0+ 版本 - -## 许可证 - -MIT License - 详见 LICENSE 文件 -`; - - fs.writeFileSync(path.join(releaseDir, 'README.md'), instructions); -} - -function showReleaseResults(releaseDir) { - const files = fs.readdirSync(releaseDir); - const totalSize = files.reduce((size, file) => { - const filePath = path.join(releaseDir, file); - const stats = fs.statSync(filePath); - return size + stats.size; - }, 0); - - console.log(`\n📦 发布包内容 (${releaseDir}):`); - files.forEach(file => { - const filePath = path.join(releaseDir, file); - const stats = fs.statSync(filePath); - const sizeKB = (stats.size / 1024).toFixed(1); - console.log(` ✓ ${file} (${sizeKB} KB)`); - }); - - console.log(`\n📊 总计 ${files.length} 个文件,大小: ${(totalSize / 1024).toFixed(1)} KB`); -} - -if (require.main === module) { - main(); -} - -module.exports = { main }; \ No newline at end of file diff --git a/src/Core.ts b/src/Core.ts index b8c230c6..1bb07570 100644 --- a/src/Core.ts +++ b/src/Core.ts @@ -189,32 +189,14 @@ export class Core { * @param options - 额外的配置选项 * @returns Core实例 */ - public static create(debug: boolean = true, options?: { disableWasm?: boolean }): Core { + public static create(debug: boolean = true): Core { if (this._instance == null) { this._instance = new Core(debug); - - // 如果指定禁用WASM,设置静默模式 - if (options?.disableWasm) { - this.disableWasm(); - } } return this._instance; } - /** - * 禁用WASM支持 - * - * 当WASM加载失败或在不支持的环境中使用时调用。 - * 这将使ECS系统使用JavaScript回退实现。 - */ - public static disableWasm(): void { - // 动态导入WASM实例并设置为静默模式 - import('./Utils/Wasm/instance').then(({ ecsCore }) => { - ecsCore.setSilent(true); - }).catch(() => { - // 如果导入失败,忽略错误 - }); - } + /** * 注册全局管理器 diff --git a/src/ECS/Core/QuerySystem.ts b/src/ECS/Core/QuerySystem.ts index 0ecfb220..d7924233 100644 --- a/src/ECS/Core/QuerySystem.ts +++ b/src/ECS/Core/QuerySystem.ts @@ -1,7 +1,7 @@ import { Entity } from '../Entity'; import { Component } from '../Component'; import { ComponentRegistry, ComponentType } from './ComponentStorage'; -import { ecsCore } from '../../Utils/WasmCore'; + import { ComponentPoolManager } from './ComponentPool'; import { BitMaskOptimizer } from './BitMaskOptimizer'; import { IndexUpdateBatcher } from './IndexUpdateBatcher'; @@ -84,7 +84,6 @@ interface QueryCacheEntry { */ export class QuerySystem { private entities: Entity[] = []; - private wasmAvailable = false; private entityIndex: EntityIndex; private indexDirty = true; @@ -150,31 +149,9 @@ export class QuerySystem { this.addEntityToIndexes(update.entity); } }; - - this.initializeWasm(); } - /** - * 初始化WebAssembly支持 - * - * 自动检测运行环境并启用WebAssembly计算加速。 - * 如果WebAssembly不可用,系统将自动回退到JavaScript实现。 - */ - private async initializeWasm(): Promise { - try { - const wasmLoaded = await ecsCore.initialize(); - this.wasmAvailable = wasmLoaded && ecsCore.isUsingWasm(); - if (this.wasmAvailable) { - console.log('QuerySystem: WebAssembly计算加速已启用'); - } else { - console.log('QuerySystem: 使用JavaScript实现'); - } - } catch (error) { - console.warn('QuerySystem: WebAssembly初始化失败,使用JavaScript实现:', error); - this.wasmAvailable = false; - } - } /** * 设置实体列表并重建索引 @@ -859,7 +836,6 @@ export class QuerySystem { * 批量更新实体组件 * * 对大量实体进行批量组件更新操作。 - * 当更新数量超过阈值时,系统会自动使用WebAssembly加速。 * * @param updates 更新操作列表,包含实体ID和新的组件掩码 * @@ -874,80 +850,33 @@ export class QuerySystem { * ``` */ public batchUpdateComponents(updates: Array<{ entityId: number, componentMask: bigint }>): void { - if (this.wasmAvailable && updates.length > 100) { - try { - const entityIds = updates.map(u => u.entityId); - const masks = updates.map(u => u.componentMask); - ecsCore.batchUpdateMasks(entityIds, masks); - console.log(`WebAssembly加速批量更新 ${updates.length} 个实体`); - } catch (error) { - console.warn('WebAssembly批量更新失败,回退到JavaScript实现:', error); - this.batchUpdateComponentsJS(updates); + // 批量处理更新,先从索引中移除,再重新添加 + const entitiesToUpdate: Entity[] = []; + + for (const update of updates) { + const entity = this.entities.find(e => e.id === update.entityId); + if (entity) { + // 先从所有索引中移除 + this.removeEntityFromIndexes(entity); + entitiesToUpdate.push(entity); } - } else { - this.batchUpdateComponentsJS(updates); } - + + // 重新添加到索引中(此时实体的组件掩码已经更新) + for (const entity of entitiesToUpdate) { + this.addEntityToIndexes(entity); + } + + // 标记脏实体进行处理 + for (const entity of entitiesToUpdate) { + this.dirtyTrackingSystem.markDirty(entity, DirtyFlag.COMPONENT_MODIFIED, []); + } + // 批量更新后清除缓存 this.clearQueryCache(); } - /** - * JavaScript实现的批量更新 - */ - private batchUpdateComponentsJS(updates: Array<{ entityId: number, componentMask: bigint }>): void { - for (const update of updates) { - const entity = this.entities.find(e => e.id === update.entityId); - if (entity) { - // 注意:componentMask是只读属性,实际应用中需要通过添加/移除组件来更新 - console.log(`更新实体 ${update.entityId} 的组件掩码: ${update.componentMask}`); - } - } - this.rebuildIndexes(); - } - /** - * 获取加速状态信息 - * - * 返回当前查询系统的加速状态和性能信息。 - * 包括WebAssembly可用性、缓存统计等详细信息。 - * - * @returns 加速状态信息对象 - */ - public getAccelerationStatus(): { - wasmEnabled: boolean; - currentProvider: string; - availableProviders: string[]; - performanceInfo?: any; - } { - return { - wasmEnabled: this.wasmAvailable, - currentProvider: this.wasmAvailable ? 'hybrid' : 'javascript', - availableProviders: ['javascript', 'hybrid'], - performanceInfo: { - entityCount: this.entities.length, - wasmEnabled: this.wasmAvailable, - cacheStats: { - size: this.queryCache.size, - hitRate: this.queryStats.totalQueries > 0 ? - (this.queryStats.cacheHits / this.queryStats.totalQueries * 100).toFixed(2) + '%' : '0%' - } - } - }; - } - - /** - * 切换加速提供者 - * - * 兼容性接口,保持向后兼容。 - * 系统会自动选择最佳的实现方式。 - * - * @param providerName 提供者名称 - * @returns 是否切换成功 - */ - public async switchAccelerationProvider(providerName: string): Promise { - return true; - } /** * 创建组件掩码 @@ -986,7 +915,6 @@ export class QuerySystem { tagIndexSize: number; nameIndexSize: number; }; - accelerationStatus: ReturnType; queryStats: { totalQueries: number; cacheHits: number; @@ -1001,6 +929,10 @@ export class QuerySystem { archetypeSystem: any; dirtyTracking: any; }; + cacheStats: { + size: number; + hitRate: string; + }; } { return { entityCount: this.entities.length, @@ -1010,7 +942,6 @@ export class QuerySystem { tagIndexSize: this.entityIndex.byTag.size, nameIndexSize: this.entityIndex.byName.size }, - accelerationStatus: this.getAccelerationStatus(), queryStats: { ...this.queryStats, cacheHitRate: this.queryStats.totalQueries > 0 ? @@ -1024,6 +955,11 @@ export class QuerySystem { entityCount: a.entities.length })), dirtyTracking: this.dirtyTrackingSystem.getStats() + }, + cacheStats: { + size: this.queryCache.size, + hitRate: this.queryStats.totalQueries > 0 ? + (this.queryStats.cacheHits / this.queryStats.totalQueries * 100).toFixed(2) + '%' : '0%' } }; } diff --git a/src/ECS/Scene.ts b/src/ECS/Scene.ts index 7d2e1e6a..3c90e14c 100644 --- a/src/ECS/Scene.ts +++ b/src/ECS/Scene.ts @@ -91,9 +91,8 @@ export class Scene { /** * 创建场景实例 - * @param enableWasmAcceleration 是否启用WebAssembly加速,默认为true */ - constructor(enableWasmAcceleration: boolean = true) { + constructor() { this.entities = new EntityList(this); this.entityProcessors = new EntityProcessorList(); this.identifierPool = new IdentifierPool(); diff --git a/src/Testing/Performance/benchmark.ts b/src/Testing/Performance/benchmark.ts deleted file mode 100644 index 1de03b23..00000000 --- a/src/Testing/Performance/benchmark.ts +++ /dev/null @@ -1,768 +0,0 @@ -/** - * ECS框架性能基准测试 - * 测试框架在不同场景下的性能表现 - */ - -import { Scene } from '../../ECS/Scene'; -import { Entity } from '../../ECS/Entity'; -import { Component } from '../../ECS/Component'; - -console.log('🚀 ECS框架性能基准测试'); -console.log('============================================================'); -console.log('测试目标: 评估ECS框架在不同场景下的性能表现'); -console.log('============================================================'); - -/** - * 位置组件 - */ -class PositionComponent extends Component { - public x: number = 0; - public y: number = 0; - - constructor(x: number = 0, y: number = 0) { - super(); - this.x = x; - this.y = y; - } -} - -/** - * 速度组件 - */ -class VelocityComponent extends Component { - public vx: number = 0; - public vy: number = 0; - - constructor(vx: number = 0, vy: number = 0) { - super(); - this.vx = vx; - this.vy = vy; - } -} - -/** - * 生命值组件 - */ -class HealthComponent extends Component { - public health: number = 100; - public maxHealth: number = 100; - - constructor(health: number = 100) { - super(); - this.health = health; - this.maxHealth = health; - } -} - -/** - * 渲染组件 - */ -class RenderComponent extends Component { - public sprite: string = ''; - public visible: boolean = true; - - constructor(sprite: string = '') { - super(); - this.sprite = sprite; - } -} - -/** - * AI组件 - */ -class AIComponent extends Component { - public state: string = 'idle'; - public target: Entity | null = null; - - constructor(state: string = 'idle') { - super(); - this.state = state; - } -} - -/** - * 测试配置接口 - */ -interface TestConfig { - entityCounts: number[]; - queryIterations: number; - updateIterations: number; -} - -/** - * 测试配置 - */ -const TEST_CONFIG: TestConfig = { - entityCounts: [1000, 5000, 10000, 25000, 50000, 100000, 200000, 500000], - queryIterations: 1000, - updateIterations: 100 -}; - -/** - * 性能测试结果 - */ -interface PerformanceResult { - entityCount: number; - singleQuery: number; - multiQuery: number; - complexQuery: number; - tagQuery: number; - singleTagQuery: number; - entityUpdate: number; - memoryUsage: number; -} - -/** - * 测试创建实体的性能 - */ -function testEntityCreation(scene: Scene, count: number): { - totalTime: number; - averageTime: number; - entitiesPerSecond: number; - breakdown: { - entityCreation: number; - componentAddition: number; - tagAssignment: number; - }; -} { - const startTime = performance.now(); - let entityCreationTime = 0; - let componentAdditionTime = 0; - let tagAssignmentTime = 0; - - // 批量创建实体(不添加组件) - const entityStart = performance.now(); - const entities = scene.createEntities(count, "Entity"); - entityCreationTime = performance.now() - entityStart; - - // 批量添加组件 - const componentStart = performance.now(); - for (let i = 0; i < entities.length; i++) { - const entity = entities[i]; - - // 所有实体都有位置组件 - entity.addComponent(new PositionComponent( - Math.random() * 1000, - Math.random() * 1000 - )); - - // 70%的实体有速度组件 - if (Math.random() < 0.7) { - entity.addComponent(new VelocityComponent( - (Math.random() - 0.5) * 10, - (Math.random() - 0.5) * 10 - )); - } - - // 50%的实体有生命值组件 - if (Math.random() < 0.5) { - entity.addComponent(new HealthComponent( - Math.floor(Math.random() * 100) + 50 - )); - } - - // 30%的实体有渲染组件 - if (Math.random() < 0.3) { - entity.addComponent(new RenderComponent(`sprite_${i % 10}`)); - } - - // 20%的实体有AI组件 - if (Math.random() < 0.2) { - entity.addComponent(new AIComponent(['idle', 'patrol', 'chase'][Math.floor(Math.random() * 3)])); - } - } - componentAdditionTime = performance.now() - componentStart; - - // 批量设置标签 - const tagStart = performance.now(); - for (const entity of entities) { - entity.tag = Math.floor(Math.random() * 10); - } - tagAssignmentTime = performance.now() - tagStart; - - const totalTime = performance.now() - startTime; - - return { - totalTime, - averageTime: totalTime / count, - entitiesPerSecond: count / (totalTime / 1000), - breakdown: { - entityCreation: entityCreationTime, - componentAddition: componentAdditionTime, - tagAssignment: tagAssignmentTime - } - }; -} - -/** - * 创建测试实体 - */ -function createTestEntities(scene: Scene, count: number): Entity[] { - const entities: Entity[] = []; - - for (let i = 0; i < count; i++) { - const entity = scene.createEntity(`Entity_${i}`); - - // 所有实体都有位置组件 - entity.addComponent(new PositionComponent( - Math.random() * 1000, - Math.random() * 1000 - )); - - // 70%的实体有速度组件 - if (Math.random() < 0.7) { - entity.addComponent(new VelocityComponent( - (Math.random() - 0.5) * 10, - (Math.random() - 0.5) * 10 - )); - } - - // 50%的实体有生命值组件 - if (Math.random() < 0.5) { - entity.addComponent(new HealthComponent( - Math.floor(Math.random() * 100) + 50 - )); - } - - // 30%的实体有渲染组件 - if (Math.random() < 0.3) { - entity.addComponent(new RenderComponent(`sprite_${i % 10}`)); - } - - // 20%的实体有AI组件 - if (Math.random() < 0.2) { - entity.addComponent(new AIComponent(['idle', 'patrol', 'chase'][Math.floor(Math.random() * 3)])); - } - - // 设置随机标签 - entity.tag = Math.floor(Math.random() * 10); - - entities.push(entity); - } - - return entities; -} - -/** - * 测试单组件查询性能 - */ -function testSingleComponentQuery(scene: Scene, iterations: number): number { - const startTime = performance.now(); - - for (let i = 0; i < iterations; i++) { - scene.querySystem.queryAll(PositionComponent); - } - - return performance.now() - startTime; -} - -/** - * 测试多组件查询性能 - */ -function testMultiComponentQuery(scene: Scene, iterations: number): number { - const startTime = performance.now(); - - for (let i = 0; i < iterations; i++) { - scene.querySystem.queryAll(PositionComponent, VelocityComponent); - } - - return performance.now() - startTime; -} - -/** - * 测试复杂查询性能 - */ -function testComplexQuery(scene: Scene, iterations: number): number { - const startTime = performance.now(); - - for (let i = 0; i < iterations; i++) { - scene.querySystem.queryAll(PositionComponent, VelocityComponent, HealthComponent); - } - - return performance.now() - startTime; -} - -/** - * 测试标签查询性能 - */ -function testTagQuery(scene: Scene, iterations: number): number { - const startTime = performance.now(); - - // 优化:预先获取所有标签查询结果,然后重复使用 - // 这更符合实际游戏中的使用模式 - const tags = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; - - for (let i = 0; i < iterations; i++) { - // 批量查询所有标签 - for (const tag of tags) { - scene.querySystem.queryByTag(tag); - } - } - - return performance.now() - startTime; -} - -/** - * 测试单个标签查询性能 - */ -function testSingleTagQuery(scene: Scene, iterations: number): number { - const startTime = performance.now(); - - // 只查询标签0,测试单个标签的查询性能和缓存效果 - for (let i = 0; i < iterations; i++) { - scene.querySystem.queryByTag(0); - } - - return performance.now() - startTime; -} - -/** - * 测试实体更新性能 - */ -function testEntityUpdate(scene: Scene, iterations: number): number { - const entities = scene.querySystem.queryAll(PositionComponent, VelocityComponent).entities; - const startTime = performance.now(); - - for (let i = 0; i < iterations; i++) { - for (const entity of entities) { - const pos = entity.getComponent(PositionComponent); - const vel = entity.getComponent(VelocityComponent); - if (pos && vel) { - pos.x += vel.vx; - pos.y += vel.vy; - } - } - } - - return performance.now() - startTime; -} - -/** - * 获取内存使用情况 - */ -function getMemoryUsage(): number { - if (typeof process !== 'undefined' && process.memoryUsage) { - return process.memoryUsage().heapUsed / 1024 / 1024; // MB - } - return 0; -} - -/** - * 运行性能测试 - */ -function runPerformanceTest(scene: Scene, entityCount: number, config: TestConfig): PerformanceResult { - console.log(`\n📊 测试 ${entityCount.toLocaleString()} 个实体...`); - - // 测试实体创建性能 - const startMemory = getMemoryUsage(); - console.log(` 🔧 测试实体创建性能...`); - const creationStats = testEntityCreation(scene, entityCount); - const endMemory = getMemoryUsage(); - - console.log(` 📈 实体创建性能分析:`); - console.log(` 总时间: ${creationStats.totalTime.toFixed(2)}ms`); - console.log(` 平均时间: ${creationStats.averageTime.toFixed(4)}ms/实体`); - console.log(` 创建速度: ${creationStats.entitiesPerSecond.toFixed(0)} 实体/秒`); - console.log(` 时间分解:`); - console.log(` - 实体创建: ${creationStats.breakdown.entityCreation.toFixed(2)}ms (${(creationStats.breakdown.entityCreation / creationStats.totalTime * 100).toFixed(1)}%)`); - console.log(` - 组件添加: ${creationStats.breakdown.componentAddition.toFixed(2)}ms (${(creationStats.breakdown.componentAddition / creationStats.totalTime * 100).toFixed(1)}%)`); - console.log(` - 标签分配: ${creationStats.breakdown.tagAssignment.toFixed(2)}ms (${(creationStats.breakdown.tagAssignment / creationStats.totalTime * 100).toFixed(1)}%)`); - console.log(` 内存使用: ${(endMemory - startMemory).toFixed(1)}MB`); - - // 运行测试 - console.log(` 🔍 执行查询测试...`); - const singleQuery = testSingleComponentQuery(scene, config.queryIterations); - const multiQuery = testMultiComponentQuery(scene, config.queryIterations); - const complexQuery = testComplexQuery(scene, config.queryIterations); - const tagQuery = testTagQuery(scene, config.queryIterations); - const singleTagQuery = testSingleTagQuery(scene, config.queryIterations); - - console.log(` ⚡ 执行更新测试...`); - const entityUpdate = testEntityUpdate(scene, config.updateIterations); - - console.log(` ✅ 测试完成`); - - return { - entityCount, - singleQuery, - multiQuery, - complexQuery, - tagQuery, - singleTagQuery, - entityUpdate, - memoryUsage: endMemory - startMemory - }; -} - -/** - * 显示系统信息 - */ -function displaySystemInfo(scene: Scene): void { - const status = scene.querySystem.getAccelerationStatus(); - const stats = scene.querySystem.getStats(); - - console.log('\n🔍 系统信息:'); - console.log(` 当前提供者: ${status.currentProvider}`); - console.log(` WebAssembly: ${status.wasmEnabled ? '已启用' : '未启用'}`); - console.log(` 可用提供者: ${status.availableProviders.join(', ')}`); - console.log(` 索引统计:`); - console.log(` 组件掩码索引: ${stats.indexStats.maskIndexSize}`); - console.log(` 组件类型索引: ${stats.indexStats.componentIndexSize}`); - console.log(` 标签索引: ${stats.indexStats.tagIndexSize}`); - console.log(` 名称索引: ${stats.indexStats.nameIndexSize}`); - - if (status.performanceInfo?.cacheStats) { - console.log(` 查询缓存:`); - console.log(` 缓存大小: ${status.performanceInfo.cacheStats.size}`); - console.log(` 命中率: ${status.performanceInfo.cacheStats.hitRate}`); - } -} - -/** - * 显示性能结果 - */ -function displayResults(results: PerformanceResult[], scene: Scene): void { - console.log('\n📈 ECS框架性能测试结果'); - console.log('='.repeat(130)); - console.log('| 实体数量 | 单组件查询 | 双组件查询 | 三组件查询 | 多标签查询 | 单标签查询 | 实体更新 | 内存使用 |'); - console.log('|' + '-'.repeat(128) + '|'); - - for (const result of results) { - const entityCount = result.entityCount.toLocaleString().padStart(9); - const singleQuery = `${result.singleQuery.toFixed(2)}ms`.padStart(10); - const multiQuery = `${result.multiQuery.toFixed(2)}ms`.padStart(10); - const complexQuery = `${result.complexQuery.toFixed(2)}ms`.padStart(10); - const tagQuery = `${result.tagQuery.toFixed(2)}ms`.padStart(10); - const singleTagQuery = `${result.singleTagQuery.toFixed(2)}ms`.padStart(10); - const entityUpdate = `${result.entityUpdate.toFixed(2)}ms`.padStart(9); - const memoryUsage = `${result.memoryUsage.toFixed(1)}MB`.padStart(9); - - console.log(`| ${entityCount} | ${singleQuery} | ${multiQuery} | ${complexQuery} | ${tagQuery} | ${singleTagQuery} | ${entityUpdate} | ${memoryUsage} |`); - } - - console.log('|' + '-'.repeat(128) + '|'); - - // 计算性能指标 - const maxEntities = Math.max(...results.map(r => r.entityCount)); - const maxResult = results.find(r => r.entityCount === maxEntities)!; - - console.log(`\n🎯 性能峰值 (${maxEntities.toLocaleString()} 个实体):`); - console.log(` 单组件查询: ${(TEST_CONFIG.queryIterations / maxResult.singleQuery * 1000).toFixed(0)} 次/秒`); - console.log(` 双组件查询: ${(TEST_CONFIG.queryIterations / maxResult.multiQuery * 1000).toFixed(0)} 次/秒`); - console.log(` 三组件查询: ${(TEST_CONFIG.queryIterations / maxResult.complexQuery * 1000).toFixed(0)} 次/秒`); - console.log(` 多标签查询: ${(TEST_CONFIG.queryIterations * 10 / maxResult.tagQuery * 1000).toFixed(0)} 次/秒`); - console.log(` 单标签查询: ${(TEST_CONFIG.queryIterations / maxResult.singleTagQuery * 1000).toFixed(0)} 次/秒`); - console.log(` 实体更新: ${(maxResult.entityCount * TEST_CONFIG.updateIterations / maxResult.entityUpdate * 1000).toFixed(0)} 个/秒`); - console.log(` 内存效率: ${(maxResult.entityCount / (maxResult.memoryUsage || 1)).toFixed(0)} 实体/MB`); - - // 性能评级 - const avgQueryTime = (maxResult.singleQuery + maxResult.multiQuery + maxResult.complexQuery + maxResult.singleTagQuery) / 4; - let rating = ''; - if (avgQueryTime < 50) rating = '🚀 优秀'; - else if (avgQueryTime < 100) rating = '✅ 良好'; - else if (avgQueryTime < 200) rating = '⚠️ 一般'; - else rating = '❌ 需要优化'; - - console.log(`\n📊 性能评级: ${rating}`); - console.log(` 平均查询时间: ${avgQueryTime.toFixed(2)}ms`); - - // 显示查询统计信息 - const queryStats = scene.querySystem.getStats().queryStats; - console.log(`\n🔍 查询统计:`); - console.log(` 总查询次数: ${queryStats.totalQueries.toLocaleString()}`); - console.log(` 缓存命中: ${queryStats.cacheHits.toLocaleString()}`); - console.log(` 索引命中: ${queryStats.indexHits.toLocaleString()}`); - console.log(` 线性扫描: ${queryStats.linearScans.toLocaleString()}`); - console.log(` 缓存命中率: ${queryStats.cacheHitRate}`); - - // 标签查询性能分析 - console.log(`\n🏷️ 标签查询分析:`); - const tagQueryRatio = maxResult.tagQuery / maxResult.singleTagQuery; - console.log(` 多标签查询 vs 单标签查询: ${tagQueryRatio.toFixed(2)}x (预期约10x)`); - if (tagQueryRatio > 15) { - console.log(` ⚠️ 多标签查询性能异常,可能存在缓存问题`); - } else if (tagQueryRatio < 5) { - console.log(` ✅ 标签查询缓存效果良好`); - } else { - console.log(` 📊 标签查询性能正常`); - } - - // 性能改进分析 - const improvement = calculatePerformanceImprovement(results); - if (improvement) { - console.log(`\n📈 性能改进分析:`); - console.log(` 双组件查询改进: ${improvement.multiQuery}x`); - console.log(` 三组件查询改进: ${improvement.complexQuery}x`); - console.log(` 整体查询改进: ${improvement.overall}x`); - } - - // 扩展性分析 - console.log(`\n📊 扩展性分析:`); - analyzeScalability(results); -} - -/** - * 计算性能改进(与优化前对比) - */ -function calculatePerformanceImprovement(results: PerformanceResult[]): { - multiQuery: string; - complexQuery: string; - overall: string; -} | null { - // 基于50,000实体的结果进行分析 - const maxResult = results.find(r => r.entityCount === 50000); - if (!maxResult) return null; - - // 优化前的基准时间(基于之前的测试结果) - const baselineMultiQuery = 1270.54; // ms - const baselineComplexQuery = 981.76; // ms - - const multiImprovement = (baselineMultiQuery / maxResult.multiQuery).toFixed(2); - const complexImprovement = (baselineComplexQuery / maxResult.complexQuery).toFixed(2); - const overallImprovement = ((baselineMultiQuery + baselineComplexQuery) / - (maxResult.multiQuery + maxResult.complexQuery)).toFixed(2); - - return { - multiQuery: multiImprovement, - complexQuery: complexImprovement, - overall: overallImprovement - }; -} - -/** - * 分析系统扩展性 - */ -function analyzeScalability(results: PerformanceResult[]): void { - if (results.length < 2) return; - - // 分析查询时间随实体数量的变化趋势 - const first = results[0]; - const last = results[results.length - 1]; - - const entityRatio = last.entityCount / first.entityCount; - const singleQueryRatio = last.singleQuery / first.singleQuery; - const multiQueryRatio = last.multiQuery / first.multiQuery; - const complexQueryRatio = last.complexQuery / first.complexQuery; - - console.log(` 实体数量增长: ${entityRatio.toFixed(1)}x (${first.entityCount.toLocaleString()} → ${last.entityCount.toLocaleString()})`); - console.log(` 单组件查询时间增长: ${singleQueryRatio.toFixed(2)}x`); - console.log(` 双组件查询时间增长: ${multiQueryRatio.toFixed(2)}x`); - console.log(` 三组件查询时间增长: ${complexQueryRatio.toFixed(2)}x`); - - // 计算复杂度 - const avgComplexity = (singleQueryRatio + multiQueryRatio + complexQueryRatio) / 3; - let complexityRating = ''; - if (avgComplexity < entityRatio * 0.1) complexityRating = '🚀 近似O(1) - 优秀'; - else if (avgComplexity < entityRatio * 0.5) complexityRating = '✅ 亚线性 - 良好'; - else if (avgComplexity < entityRatio) complexityRating = '⚠️ 接近线性 - 一般'; - else complexityRating = '❌ 超线性 - 需要优化'; - - console.log(` 时间复杂度评估: ${complexityRating}`); - - // 内存效率分析 - const memoryEfficiencyFirst = first.entityCount / first.memoryUsage; - const memoryEfficiencyLast = last.entityCount / last.memoryUsage; - const memoryEfficiencyRatio = memoryEfficiencyLast / memoryEfficiencyFirst; - - console.log(` 内存效率变化: ${memoryEfficiencyRatio.toFixed(2)}x (${memoryEfficiencyFirst.toFixed(0)} → ${memoryEfficiencyLast.toFixed(0)} 实体/MB)`); -} - -/** - * 专门测试实体创建性能 - */ -async function runEntityCreationBenchmark(): Promise { - console.log('\n🚀 实体创建性能基准测试'); - console.log('='.repeat(60)); - - const testCounts = [1000, 5000, 10000, 50000, 100000]; - - for (const count of testCounts) { - console.log(`\n📊 测试创建 ${count.toLocaleString()} 个实体:`); - - // 创建新场景 - const scene = new Scene(); - - // 测试创建性能 - const stats = testEntityCreation(scene, count); - - console.log(` 总时间: ${stats.totalTime.toFixed(2)}ms`); - console.log(` 平均时间: ${stats.averageTime.toFixed(4)}ms/实体`); - console.log(` 创建速度: ${stats.entitiesPerSecond.toFixed(0)} 实体/秒`); - console.log(` 时间分解:`); - console.log(` - 实体创建: ${stats.breakdown.entityCreation.toFixed(2)}ms (${(stats.breakdown.entityCreation / stats.totalTime * 100).toFixed(1)}%)`); - console.log(` - 组件添加: ${stats.breakdown.componentAddition.toFixed(2)}ms (${(stats.breakdown.componentAddition / stats.totalTime * 100).toFixed(1)}%)`); - console.log(` - 标签分配: ${stats.breakdown.tagAssignment.toFixed(2)}ms (${(stats.breakdown.tagAssignment / stats.totalTime * 100).toFixed(1)}%)`); - - // 分析性能瓶颈 - const { entityCreation, componentAddition, tagAssignment } = stats.breakdown; - const total = stats.totalTime; - - console.log(` 性能瓶颈分析:`); - if (componentAddition / total > 0.5) { - console.log(` ⚠️ 组件添加是主要瓶颈 (${(componentAddition / total * 100).toFixed(1)}%)`); - } - if (entityCreation / total > 0.3) { - console.log(` ⚠️ 实体创建开销较高 (${(entityCreation / total * 100).toFixed(1)}%)`); - } - if (tagAssignment / total > 0.1) { - console.log(` ⚠️ 标签分配开销异常 (${(tagAssignment / total * 100).toFixed(1)}%)`); - } - - // 分析组件添加性能(仅对较小的测试集) - if (count <= 10000) { - analyzeComponentAdditionPerformance(new Scene(), Math.min(count, 5000)); - } - - // 清理场景 - scene.end(); - } - - console.log('\n📈 实体创建性能总结:'); - console.log(' 主要性能瓶颈通常在组件添加阶段'); - console.log(' 建议优化方向:'); - console.log(' 1. 减少组件注册开销'); - console.log(' 2. 优化位掩码计算'); - console.log(' 3. 减少内存分配次数'); - console.log(' 4. 使用对象池复用组件实例'); -} - -/** - * 测试组件添加性能的详细分析 - */ -function analyzeComponentAdditionPerformance(scene: Scene, count: number): void { - console.log(`\n🔬 组件添加性能详细分析 (${count.toLocaleString()} 个实体):`); - - // 创建实体但不添加组件 - const entities = scene.createEntities(count, "TestEntity"); - - // 分别测试每种组件的添加性能 - const componentTests = [ - { - name: "PositionComponent", - create: () => new PositionComponent(Math.random() * 1000, Math.random() * 1000), - probability: 1.0 - }, - { - name: "VelocityComponent", - create: () => new VelocityComponent((Math.random() - 0.5) * 10, (Math.random() - 0.5) * 10), - probability: 0.7 - }, - { - name: "HealthComponent", - create: () => new HealthComponent(Math.floor(Math.random() * 100) + 50), - probability: 0.5 - }, - { - name: "RenderComponent", - create: () => new RenderComponent(`sprite_${Math.floor(Math.random() * 10)}`), - probability: 0.3 - }, - { - name: "AIComponent", - create: () => new AIComponent(['idle', 'patrol', 'chase'][Math.floor(Math.random() * 3)]), - probability: 0.2 - } - ]; - - for (const test of componentTests) { - const startTime = performance.now(); - let addedCount = 0; - - for (const entity of entities) { - if (Math.random() < test.probability) { - entity.addComponent(test.create()); - addedCount++; - } - } - - const endTime = performance.now(); - const totalTime = endTime - startTime; - - console.log(` ${test.name}:`); - console.log(` 添加数量: ${addedCount.toLocaleString()}`); - console.log(` 总时间: ${totalTime.toFixed(2)}ms`); - console.log(` 平均时间: ${(totalTime / addedCount).toFixed(4)}ms/组件`); - console.log(` 添加速度: ${(addedCount / (totalTime / 1000)).toFixed(0)} 组件/秒`); - } -} - -/** - * 主测试函数 - */ -async function runBenchmarks(): Promise { - console.log('🎯 ECS框架性能基准测试'); - console.log('='.repeat(60)); - - // 先运行实体创建性能测试 - await runEntityCreationBenchmark(); - - // 然后运行完整的框架测试 - console.log('\n🚀 完整框架性能测试'); - console.log('='.repeat(60)); - - console.log(`\n⚙️ 测试配置:`); - console.log(` 实体数量: ${TEST_CONFIG.entityCounts.map(n => n.toLocaleString()).join(', ')}`); - console.log(` 查询迭代: ${TEST_CONFIG.queryIterations.toLocaleString()}`); - console.log(` 更新迭代: ${TEST_CONFIG.updateIterations.toLocaleString()}`); - console.log(` 预计测试时间: ${(TEST_CONFIG.entityCounts.length * 2).toFixed(0)}-${(TEST_CONFIG.entityCounts.length * 5).toFixed(0)} 分钟`); - - console.log('\n🔧 初始化ECS框架...'); - - // 初始化WebAssembly模块 - try { - const { ecsCore } = await import('../../Utils/WasmCore'); - await ecsCore.initialize(); - console.log(`✅ WebAssembly模块: ${ecsCore.isUsingWasm() ? '已加载' : '未加载'}`); - } catch (error) { - console.log('⚠️ WebAssembly模块加载失败,使用JavaScript实现'); - } - - const scene = new Scene(); - - // 等待初始化完成 - await new Promise(resolve => setTimeout(resolve, 1000)); - - displaySystemInfo(scene); - - const results: PerformanceResult[] = []; - const totalTests = TEST_CONFIG.entityCounts.length; - - // 运行不同规模的测试 - for (let i = 0; i < TEST_CONFIG.entityCounts.length; i++) { - const entityCount = TEST_CONFIG.entityCounts[i]; - console.log(`\n🔄 进度: ${i + 1}/${totalTests} (${((i + 1) / totalTests * 100).toFixed(1)}%)`); - - const result = runPerformanceTest(scene, entityCount, TEST_CONFIG); - results.push(result); - - // 清理场景,准备下一轮测试 - console.log(` 🧹 清理内存...`); - scene.end(); - scene.begin(); - - // 强制垃圾回收 - if (typeof global !== 'undefined' && global.gc) { - global.gc(); - } - - // 大规模测试间隔稍作休息 - if (entityCount >= 100000) { - console.log(` ⏱️ 等待系统稳定...`); - await new Promise(resolve => setTimeout(resolve, 2000)); - } - } - - displayResults(results, scene); - - scene.end(); - console.log('\n✅ 性能测试完成!'); - console.log(`📊 总测试时间: ${((Date.now() - startTime) / 1000 / 60).toFixed(1)} 分钟`); -} - -// 记录开始时间 -const startTime = Date.now(); - -// 运行测试 -runBenchmarks().catch(error => { - console.error('❌ 测试失败:', error); -}); \ No newline at end of file diff --git a/src/Testing/Performance/component-performance.js b/src/Testing/Performance/component-performance.js deleted file mode 100644 index 8cfdf7eb..00000000 --- a/src/Testing/Performance/component-performance.js +++ /dev/null @@ -1,53 +0,0 @@ -const { Scene } = require('./bin/ECS/Scene.js'); - -const { Component } = require('./bin/ECS/Component.js'); - -// 简单的组件类 -class TestComponent extends Component { - constructor(value) { - super(); - this.value = value; - } -} - -console.log('🔬 组件添加性能分析'); - -// 创建场景和实体 -const scene = new Scene(); -console.log('✅ 创建场景完成'); - -const startCreate = performance.now(); -const entities = scene.createEntities(5000, 'TestEntity'); -const endCreate = performance.now(); - -console.log(`✅ 创建了 ${entities.length} 个实体,耗时: ${(endCreate - startCreate).toFixed(2)}ms`); - -// 测试单个组件添加性能 -console.log('\n📊 测试组件添加性能:'); - -const startAdd = performance.now(); -for (let i = 0; i < entities.length; i++) { - const entity = entities[i]; - entity.addComponent(new TestComponent(i)); -} -const endAdd = performance.now(); - -const addTime = endAdd - startAdd; -console.log(`添加 ${entities.length} 个组件耗时: ${addTime.toFixed(2)}ms`); -console.log(`平均每个组件: ${(addTime / entities.length).toFixed(4)}ms`); -console.log(`添加速度: ${(entities.length / (addTime / 1000)).toFixed(0)} 组件/秒`); - -// 测试组件获取性能 -console.log('\n📊 测试组件获取性能:'); - -const startGet = performance.now(); -for (let i = 0; i < entities.length; i++) { - const entity = entities[i]; - const component = entity.getComponent(TestComponent); -} -const endGet = performance.now(); - -const getTime = endGet - startGet; -console.log(`获取 ${entities.length} 个组件耗时: ${getTime.toFixed(2)}ms`); -console.log(`平均每个组件: ${(getTime / entities.length).toFixed(4)}ms`); -console.log(`获取速度: ${(entities.length / (getTime / 1000)).toFixed(0)} 组件/秒`); \ No newline at end of file diff --git a/src/Testing/Unit/bitmask-optimizer.test.ts b/src/Testing/Unit/bitmask-optimizer.test.ts deleted file mode 100644 index d5c5acbe..00000000 --- a/src/Testing/Unit/bitmask-optimizer.test.ts +++ /dev/null @@ -1,199 +0,0 @@ -import { BitMaskOptimizer } from '../../ECS/Core/BitMaskOptimizer'; - -/** - * 位掩码优化器测试 - */ -function testBitMaskOptimizer(): void { - console.log('🧪 测试位掩码优化器'); - - const optimizer = BitMaskOptimizer.getInstance(); - optimizer.reset(); - - // 测试组件类型注册 - console.log(' 📝 测试组件类型注册...'); - const positionId = optimizer.registerComponentType('Position'); - const velocityId = optimizer.registerComponentType('Velocity'); - const healthId = optimizer.registerComponentType('Health'); - - console.log(` Position ID: ${positionId}`); - console.log(` Velocity ID: ${velocityId}`); - console.log(` Health ID: ${healthId}`); - - // 测试单个组件掩码 - console.log(' 🎯 测试单个组件掩码...'); - const positionMask = optimizer.createSingleComponentMask('Position'); - const velocityMask = optimizer.createSingleComponentMask('Velocity'); - - console.log(` Position掩码: ${positionMask.toString(2)}`); - console.log(` Velocity掩码: ${velocityMask.toString(2)}`); - - // 测试组合掩码 - console.log(' 🔗 测试组合掩码...'); - const combinedMask = optimizer.createCombinedMask(['Position', 'Velocity']); - console.log(` Position+Velocity掩码: ${combinedMask.toString(2)}`); - - // 测试掩码包含检查 - console.log(' ✅ 测试掩码包含检查...'); - const hasPosition = optimizer.maskContainsComponent(combinedMask, 'Position'); - const hasVelocity = optimizer.maskContainsComponent(combinedMask, 'Velocity'); - const hasHealth = optimizer.maskContainsComponent(combinedMask, 'Health'); - - console.log(` 包含Position: ${hasPosition}`); - console.log(` 包含Velocity: ${hasVelocity}`); - console.log(` 包含Health: ${hasHealth}`); - - // 测试掩码操作 - console.log(' 🔧 测试掩码操作...'); - let entityMask = 0n; - entityMask = optimizer.addComponentToMask(entityMask, 'Position'); - entityMask = optimizer.addComponentToMask(entityMask, 'Health'); - - console.log(` 添加Position和Health后: ${entityMask.toString(2)}`); - - const hasAll = optimizer.maskContainsAllComponents(entityMask, ['Position', 'Health']); - const hasAny = optimizer.maskContainsAnyComponent(entityMask, ['Position', 'Velocity']); - - console.log(` 包含Position和Health: ${hasAll}`); - console.log(` 包含Position或Velocity: ${hasAny}`); - - // 测试掩码分析 - console.log(' 📊 测试掩码分析...'); - const componentNames = optimizer.maskToComponentNames(entityMask); - const componentCount = optimizer.getComponentCount(entityMask); - - console.log(` 掩码包含的组件: ${componentNames.join(', ')}`); - console.log(` 组件数量: ${componentCount}`); - - // 测试缓存统计 - console.log(' 📈 测试缓存统计...'); - const stats = optimizer.getCacheStats(); - console.log(` 缓存大小: ${stats.size}`); - console.log(` 组件类型数量: ${stats.componentTypes}`); - - // 测试预计算常用掩码 - console.log(' ⚡ 测试预计算常用掩码...'); - const commonCombinations = [ - ['Position', 'Velocity'], - ['Position', 'Health'], - ['Position', 'Velocity', 'Health'] - ]; - - optimizer.precomputeCommonMasks(commonCombinations); - const statsAfterPrecompute = optimizer.getCacheStats(); - console.log(` 预计算后缓存大小: ${statsAfterPrecompute.size}`); - - console.log('✅ 位掩码优化器测试完成'); -} - -/** - * 性能测试 - */ -function testBitMaskPerformance(): void { - console.log('\n🚀 位掩码优化器性能测试'); - - const optimizer = BitMaskOptimizer.getInstance(); - optimizer.reset(); - - // 注册组件类型 - const componentTypes = ['Position', 'Velocity', 'Health', 'Render', 'AI', 'Physics', 'Audio', 'Network']; - for (const type of componentTypes) { - optimizer.registerComponentType(type); - } - - const iterations = 100000; - - // 测试单个掩码创建性能 - console.log(' 🔥 测试单个掩码创建性能...'); - let start = performance.now(); - for (let i = 0; i < iterations; i++) { - optimizer.createSingleComponentMask('Position'); - } - let end = performance.now(); - console.log(` ${iterations}次单个掩码创建: ${(end - start).toFixed(2)}ms`); - - // 测试组合掩码创建性能 - console.log(' 🔥 测试组合掩码创建性能...'); - start = performance.now(); - for (let i = 0; i < iterations; i++) { - optimizer.createCombinedMask(['Position', 'Velocity', 'Health']); - } - end = performance.now(); - console.log(` ${iterations}次组合掩码创建: ${(end - start).toFixed(2)}ms`); - - // 测试掩码检查性能 - console.log(' 🔥 测试掩码检查性能...'); - const testMask = optimizer.createCombinedMask(['Position', 'Velocity', 'Health']); - - start = performance.now(); - for (let i = 0; i < iterations; i++) { - optimizer.maskContainsComponent(testMask, 'Position'); - optimizer.maskContainsComponent(testMask, 'AI'); - } - end = performance.now(); - console.log(` ${iterations * 2}次掩码检查: ${(end - start).toFixed(2)}ms`); - - // 对比原生位操作性能 - console.log(' ⚖️ 对比原生位操作性能...'); - const positionBit = 1n << 0n; - const velocityBit = 1n << 1n; - const healthBit = 1n << 2n; - const nativeMask = positionBit | velocityBit | healthBit; - - start = performance.now(); - for (let i = 0; i < iterations; i++) { - (nativeMask & positionBit) !== 0n; - (nativeMask & (1n << 7n)) !== 0n; // AI位 - } - end = performance.now(); - console.log(` ${iterations * 2}次原生位操作: ${(end - start).toFixed(2)}ms`); - - console.log('✅ 性能测试完成'); -} - -/** - * 内存使用测试 - */ -function testBitMaskMemoryUsage(): void { - console.log('\n💾 位掩码优化器内存使用测试'); - - const optimizer = BitMaskOptimizer.getInstance(); - optimizer.reset(); - - // 注册大量组件类型 - console.log(' 📝 注册组件类型...'); - for (let i = 0; i < 100; i++) { - optimizer.registerComponentType(`Component${i}`); - } - - // 创建大量掩码组合 - console.log(' 🔗 创建掩码组合...'); - const maskCount = 1000; - for (let i = 0; i < maskCount; i++) { - const componentCount = Math.floor(Math.random() * 5) + 1; - const components: string[] = []; - for (let j = 0; j < componentCount; j++) { - components.push(`Component${Math.floor(Math.random() * 100)}`); - } - optimizer.createCombinedMask(components); - } - - const stats = optimizer.getCacheStats(); - console.log(` 📊 最终统计:`); - console.log(` 组件类型数量: ${stats.componentTypes}`); - console.log(` 缓存掩码数量: ${stats.size}`); - console.log(` 平均每个掩码占用: ~${(stats.size * 64 / 1024).toFixed(2)} KB`); - - console.log('✅ 内存使用测试完成'); -} - -// 运行所有测试 -export function runBitMaskOptimizerTests(): void { - console.log('🧪 位掩码优化器测试套件'); - console.log('='.repeat(50)); - - testBitMaskOptimizer(); - testBitMaskPerformance(); - testBitMaskMemoryUsage(); - - console.log('\n✅ 所有测试完成'); -} \ No newline at end of file diff --git a/src/Testing/Unit/component-pool.test.ts b/src/Testing/Unit/component-pool.test.ts deleted file mode 100644 index a0b841f6..00000000 --- a/src/Testing/Unit/component-pool.test.ts +++ /dev/null @@ -1,189 +0,0 @@ -import { ComponentPool, ComponentPoolManager } from '../../ECS/Core/ComponentPool'; -import { Component } from '../../ECS/Component'; - -/** - * 测试用组件 - */ -class TestComponent extends Component { - public value: number = 0; - - constructor(value: number = 0) { - super(); - this.value = value; - } - - reset(): void { - this.value = 0; - } -} - -/** - * 运行组件对象池测试 - */ -export function runComponentPoolTests(): void { - console.log('🧪 组件对象池测试'); - console.log('='.repeat(50)); - - testBasicFunctionality(); - testPoolManager(); - testPerformance(); - - console.log('✅ 组件对象池测试完成'); -} - -/** - * 基础功能测试 - */ -function testBasicFunctionality(): void { - console.log('\n📝 基础功能测试...'); - - const pool = new ComponentPool( - () => new TestComponent(), - (component) => component.reset(), - 10 - ); - - // 测试获取新组件实例 - console.log(' 测试获取新组件实例...'); - const component = pool.acquire(); - console.assert(component instanceof TestComponent, '应该返回TestComponent实例'); - console.assert(component.value === 0, '新组件的值应该为0'); - - // 测试释放和复用 - console.log(' 测试组件释放和复用...'); - component.value = 42; - pool.release(component); - console.assert(pool.getAvailableCount() === 1, '池中应该有1个可用组件'); - - const reusedComponent = pool.acquire(); - console.assert(reusedComponent === component, '应该复用同一个组件实例'); - console.assert(reusedComponent.value === 0, '复用的组件应该被重置'); - - // 测试预填充 - console.log(' 测试对象池预填充...'); - pool.prewarm(5); - console.assert(pool.getAvailableCount() === 5, '预填充后应该有5个可用组件'); - - const components: TestComponent[] = []; - for (let i = 0; i < 5; i++) { - components.push(pool.acquire()); - } - console.assert(pool.getAvailableCount() === 0, '获取5个组件后池应该为空'); - - // 测试最大容量限制 - console.log(' 测试最大容量限制...'); - pool.prewarm(10); - const extraComponent = new TestComponent(); - pool.release(extraComponent); - console.assert(pool.getAvailableCount() === 10, '不应该超过最大容量'); - - // 测试清空池 - console.log(' 测试清空对象池...'); - pool.clear(); - console.assert(pool.getAvailableCount() === 0, '清空后池应该为空'); - - console.log(' ✅ 基础功能测试通过'); -} - -/** - * 池管理器测试 - */ -function testPoolManager(): void { - console.log('\n📝 池管理器测试...'); - - const manager = ComponentPoolManager.getInstance(); - manager.clearAll(); - - // 测试单例模式 - console.log(' 测试单例模式...'); - const manager1 = ComponentPoolManager.getInstance(); - const manager2 = ComponentPoolManager.getInstance(); - console.assert(manager1 === manager2, '应该返回同一个实例'); - - // 测试注册组件池 - console.log(' 测试注册组件池...'); - manager.registerPool( - 'TestComponent', - () => new TestComponent(), - (component) => component.reset(), - 5 - ); - - const stats = manager.getPoolStats(); - console.assert(stats.has('TestComponent'), '应该包含已注册的组件类型'); - console.assert(stats.get('TestComponent')?.maxSize === 5, '最大容量应该为5'); - - // 测试获取和释放组件 - console.log(' 测试获取和释放组件...'); - const component = manager.acquireComponent('TestComponent'); - console.assert(component instanceof TestComponent, '应该返回TestComponent实例'); - - if (component) { - component.value = 42; - manager.releaseComponent('TestComponent', component); - - const reusedComponent = manager.acquireComponent('TestComponent'); - console.assert(reusedComponent === component, '应该复用同一个组件'); - console.assert(reusedComponent?.value === 0, '复用的组件应该被重置'); - } - - // 测试预热所有池 - console.log(' 测试预热所有池...'); - manager.registerPool('TestComponent1', () => new TestComponent()); - manager.registerPool('TestComponent2', () => new TestComponent()); - - manager.prewarmAll(3); - - const finalStats = manager.getPoolStats(); - console.assert(finalStats.get('TestComponent1')?.available === 3, 'TestComponent1应该有3个可用组件'); - console.assert(finalStats.get('TestComponent2')?.available === 3, 'TestComponent2应该有3个可用组件'); - - // 测试未注册的组件类型 - console.log(' 测试未注册的组件类型...'); - const nullComponent = manager.acquireComponent('NonExistentComponent'); - console.assert(nullComponent === null, '未注册的组件类型应该返回null'); - - manager.clearAll(); - console.log(' ✅ 池管理器测试通过'); -} - -/** - * 性能测试 - */ -function testPerformance(): void { - console.log('\n📝 性能测试...'); - - const pool = new ComponentPool(() => new TestComponent()); - const iterations = 10000; - - // 预热池 - pool.prewarm(100); - - // 测试对象池性能 - const poolStart = performance.now(); - for (let i = 0; i < iterations; i++) { - const component = pool.acquire(); - pool.release(component); - } - const poolEnd = performance.now(); - const poolTime = poolEnd - poolStart; - - // 测试直接创建性能 - const directStart = performance.now(); - for (let i = 0; i < iterations; i++) { - new TestComponent(); - } - const directEnd = performance.now(); - const directTime = directEnd - directStart; - - console.log(` 对象池时间: ${poolTime.toFixed(2)}ms`); - console.log(` 直接创建时间: ${directTime.toFixed(2)}ms`); - const improvement = ((directTime - poolTime) / directTime * 100); - console.log(` 性能提升: ${improvement.toFixed(1)}%`); - - if (poolTime < directTime) { - console.log(' ✅ 对象池性能测试通过 - 比直接创建更快'); - } else { - console.log(' ⚠️ 对象池在小规模测试中可能不如直接创建快'); - } -} \ No newline at end of file diff --git a/src/Testing/test-runner.ts b/src/Testing/test-runner.ts deleted file mode 100644 index 7970e6b9..00000000 --- a/src/Testing/test-runner.ts +++ /dev/null @@ -1,182 +0,0 @@ -import { runBitMaskOptimizerTests } from './Unit/bitmask-optimizer.test'; -import { runComponentPoolTests } from './Unit/component-pool.test'; - -/** - * 测试运行器 - 统一运行所有测试 - */ -export class TestRunner { - private results: Map = new Map(); - - /** - * 运行所有单元测试 - */ - async runUnitTests(): Promise { - console.log('🧪 运行单元测试'); - console.log('='.repeat(50)); - - await this.runTest('组件对象池', runComponentPoolTests); - await this.runTest('位掩码优化器', runBitMaskOptimizerTests); - - console.log('\n📊 单元测试总结:'); - this.printSummary(); - } - - /** - * 运行性能测试 - */ - async runPerformanceTests(): Promise { - console.log('\n🚀 运行性能测试'); - console.log('='.repeat(50)); - - // 性能测试需要从benchmark.ts文件中导入 - console.log('⚠️ 性能测试需要单独运行 - 请使用: node benchmark.ts'); - - console.log('\n📊 性能测试总结:'); - this.printSummary(); - } - - /** - * 运行集成测试 - */ - async runIntegrationTests(): Promise { - console.log('\n🔗 运行集成测试'); - console.log('='.repeat(50)); - - // 集成测试待实现 - console.log('⚠️ 集成测试尚未实现'); - } - - /** - * 运行所有测试 - */ - async runAllTests(): Promise { - console.log('🎯 ECS框架完整测试套件'); - console.log('='.repeat(60)); - - const startTime = performance.now(); - - await this.runUnitTests(); - await this.runPerformanceTests(); - await this.runIntegrationTests(); - - const endTime = performance.now(); - const totalDuration = endTime - startTime; - - console.log('\n✅ 所有测试完成'); - console.log(`🕐 总测试时间: ${(totalDuration / 1000).toFixed(2)}秒`); - - this.printFinalSummary(); - } - - /** - * 运行单个测试 - */ - private async runTest(testName: string, testFunction: () => void | Promise): Promise { - console.log(`\n▶️ 开始测试: ${testName}`); - - const startTime = performance.now(); - let passed = 0; - let failed = 0; - - try { - await testFunction(); - passed = 1; - console.log(`✅ ${testName} 测试通过`); - } catch (error) { - failed = 1; - console.error(`❌ ${testName} 测试失败:`, error); - } - - const endTime = performance.now(); - const duration = endTime - startTime; - - this.results.set(testName, { passed, failed, duration }); - - console.log(`⏱️ 耗时: ${duration.toFixed(2)}ms`); - } - - /** - * 打印测试摘要 - */ - private printSummary(): void { - let totalPassed = 0; - let totalFailed = 0; - let totalDuration = 0; - - for (const [name, result] of this.results) { - totalPassed += result.passed; - totalFailed += result.failed; - totalDuration += result.duration; - - const status = result.failed > 0 ? '❌' : '✅'; - console.log(` ${status} ${name}: ${result.duration.toFixed(2)}ms`); - } - - console.log(`\n📈 测试统计:`); - console.log(` 通过: ${totalPassed}`); - console.log(` 失败: ${totalFailed}`); - console.log(` 总时间: ${totalDuration.toFixed(2)}ms`); - console.log(` 成功率: ${totalPassed + totalFailed > 0 ? (totalPassed / (totalPassed + totalFailed) * 100).toFixed(1) : 0}%`); - } - - /** - * 打印最终测试摘要 - */ - private printFinalSummary(): void { - console.log('\n📋 最终测试报告'); - console.log('='.repeat(60)); - - let totalPassed = 0; - let totalFailed = 0; - - for (const [, result] of this.results) { - totalPassed += result.passed; - totalFailed += result.failed; - } - - if (totalFailed === 0) { - console.log('🎉 所有测试都通过了!'); - } else { - console.log(`⚠️ 有 ${totalFailed} 个测试失败`); - } - - console.log(`📊 测试覆盖率: ${this.results.size} 个测试模块`); - console.log(`✅ 通过率: ${totalPassed + totalFailed > 0 ? (totalPassed / (totalPassed + totalFailed) * 100).toFixed(1) : 0}%`); - } - - /** - * 清除测试结果 - */ - clearResults(): void { - this.results.clear(); - } -} - -/** - * 便捷函数:运行所有测试 - */ -export async function runAllTests(): Promise { - const runner = new TestRunner(); - await runner.runAllTests(); -} - -/** - * 便捷函数:仅运行单元测试 - */ -export async function runUnitTests(): Promise { - const runner = new TestRunner(); - await runner.runUnitTests(); -} - -/** - * 便捷函数:仅运行性能测试 - */ -export async function runPerformanceTests(): Promise { - const runner = new TestRunner(); - await runner.runPerformanceTests(); -} - -// 如果直接运行此文件,执行所有测试 -if (require.main === module) { - runAllTests().catch(console.error); -} \ No newline at end of file diff --git a/src/Utils/WasmCore.ts b/src/Utils/WasmCore.ts deleted file mode 100644 index c71cf6d4..00000000 --- a/src/Utils/WasmCore.ts +++ /dev/null @@ -1,8 +0,0 @@ -/** - * WASM ECS核心模块 - * - * 提供高性能的ECS操作,支持WASM和JavaScript双重实现 - */ - -export * from './Wasm'; -export type Query = import('./Wasm').QueryResult; \ No newline at end of file diff --git a/src/Utils/index.ts b/src/Utils/index.ts index 8979e9e1..2df380ee 100644 --- a/src/Utils/index.ts +++ b/src/Utils/index.ts @@ -3,15 +3,4 @@ export * from './Pool'; export * from './Emitter'; export * from './GlobalManager'; export * from './PerformanceMonitor'; -export { Time } from './Time'; -export { - WasmEcsCore, - ecsCore, - initializeEcs, - EntityId, - ComponentMask, - QueryResult, - PerformanceStats as WasmPerformanceStats, - WasmLoader, - JavaScriptFallback -} from './Wasm'; \ No newline at end of file +export { Time } from './Time'; \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 3d5fb10e..51658b56 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -38,7 +38,6 @@ "exclude": [ "node_modules", "bin", - "src/wasm/**/*", "**/*.test.ts", "**/*.spec.ts" ] diff --git a/wasm-release/README.md b/wasm-release/README.md deleted file mode 100644 index c965f284..00000000 --- a/wasm-release/README.md +++ /dev/null @@ -1,95 +0,0 @@ -# ECS Framework WASM 支持包 - -这个包包含了 @esengine/ecs-framework 的 WASM 加速模块。 - -## 包含文件 - -- `ecs_wasm_core.js` - WASM 胶水代码 -- `ecs_wasm_core.d.ts` - TypeScript 类型定义 -- `ecs_wasm_core_bg.wasm` - WASM 二进制文件 -- `ecs_wasm_core_bg.wasm.d.ts` - WASM 类型定义 -- `package.json` - 包信息 - -## 使用方法 - -### Node.js 环境 - -```javascript -import init, { EcsCore } from './ecs_wasm_core.js'; - -async function useWasm() { - // 初始化 WASM 模块 - await init(); - - // 创建 ECS 核心实例 - const ecsCore = new EcsCore(); - - // 使用 WASM 加速的 ECS 功能 - const entity = ecsCore.create_entity(); - console.log('创建实体:', entity); -} - -useWasm(); -``` - -### 浏览器环境 - -```html - - - - - - -

ECS Framework WASM Demo

- - -``` - -### TypeScript 支持 - -确保包含类型定义: - -```typescript -import init, { EcsCore } from './ecs_wasm_core.js'; - -async function typedExample(): Promise { - await init(); - - const ecsCore = new EcsCore(); - const entityId: number = ecsCore.create_entity(); - - // 使用类型安全的 API - const mask = BigInt(0b1010); - ecsCore.update_entity_mask(entityId, mask); -} -``` - -## 性能优势 - -WASM 模块主要优化以下操作: - -- 🚀 实体查询(10-100x 性能提升) -- 🔥 组件掩码操作 -- ⚡ 批量实体处理 - -## 兼容性 - -- **浏览器**: 支持 WebAssembly 的现代浏览器 -- **Node.js**: 16.0+ 版本 -- **TypeScript**: 4.0+ 版本 - -## 许可证 - -MIT License - 详见 LICENSE 文件 diff --git a/wasm-release/ecs_wasm_core.d.ts b/wasm-release/ecs_wasm_core.d.ts deleted file mode 100644 index 8f869281..00000000 --- a/wasm-release/ecs_wasm_core.d.ts +++ /dev/null @@ -1,141 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -/** - * 创建组件掩码的辅助函数 - */ -export function create_component_mask(component_ids: Uint32Array): bigint; -/** - * 检查掩码是否包含指定组件 - */ -export function mask_contains_component(mask: bigint, component_id: number): boolean; -/** - * 初始化函数 - */ -export function main(): void; -/** - * 高性能ECS核心,专注于实体查询和掩码管理 - */ -export class EcsCore { - free(): void; - /** - * 创建新的ECS核心 - */ - constructor(); - /** - * 创建新实体 - */ - create_entity(): number; - /** - * 删除实体 - */ - destroy_entity(entity_id: number): boolean; - /** - * 更新实体的组件掩码 - */ - update_entity_mask(entity_id: number, mask: bigint): void; - /** - * 批量更新实体掩码 - */ - batch_update_masks(entity_ids: Uint32Array, masks: BigUint64Array): void; - /** - * 查询实体 - */ - query_entities(mask: bigint, max_results: number): number; - /** - * 获取查询结果数量 - */ - get_query_result_count(): number; - /** - * 缓存查询实体 - */ - query_cached(mask: bigint): number; - /** - * 获取缓存查询结果数量 - */ - get_cached_query_count(mask: bigint): number; - /** - * 多组件查询 - */ - query_multiple_components(masks: BigUint64Array, max_results: number): number; - /** - * 排除查询 - */ - query_with_exclusion(include_mask: bigint, exclude_mask: bigint, max_results: number): number; - /** - * 获取实体的组件掩码 - */ - get_entity_mask(entity_id: number): bigint; - /** - * 检查实体是否存在 - */ - entity_exists(entity_id: number): boolean; - /** - * 获取实体数量 - */ - get_entity_count(): number; - /** - * 获取性能统计信息 - */ - get_performance_stats(): Array; - /** - * 清理所有数据 - */ - clear(): void; - /** - * 重建查询缓存 - */ - rebuild_query_cache(): void; -} - -export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module; - -export interface InitOutput { - readonly memory: WebAssembly.Memory; - readonly __wbg_ecscore_free: (a: number, b: number) => void; - readonly ecscore_new: () => number; - readonly ecscore_create_entity: (a: number) => number; - readonly ecscore_destroy_entity: (a: number, b: number) => number; - readonly ecscore_update_entity_mask: (a: number, b: number, c: bigint) => void; - readonly ecscore_batch_update_masks: (a: number, b: number, c: number, d: number, e: number) => void; - readonly ecscore_query_entities: (a: number, b: bigint, c: number) => number; - readonly ecscore_get_query_result_count: (a: number) => number; - readonly ecscore_query_cached: (a: number, b: bigint) => number; - readonly ecscore_get_cached_query_count: (a: number, b: bigint) => number; - readonly ecscore_query_multiple_components: (a: number, b: number, c: number, d: number) => number; - readonly ecscore_query_with_exclusion: (a: number, b: bigint, c: bigint, d: number) => number; - readonly ecscore_get_entity_mask: (a: number, b: number) => bigint; - readonly ecscore_entity_exists: (a: number, b: number) => number; - readonly ecscore_get_entity_count: (a: number) => number; - readonly ecscore_get_performance_stats: (a: number) => any; - readonly ecscore_clear: (a: number) => void; - readonly ecscore_rebuild_query_cache: (a: number) => void; - readonly create_component_mask: (a: number, b: number) => bigint; - readonly mask_contains_component: (a: bigint, b: number) => number; - readonly main: () => void; - readonly __wbindgen_exn_store: (a: number) => void; - readonly __externref_table_alloc: () => number; - readonly __wbindgen_export_2: WebAssembly.Table; - readonly __wbindgen_malloc: (a: number, b: number) => number; - readonly __wbindgen_start: () => void; -} - -export type SyncInitInput = BufferSource | WebAssembly.Module; -/** -* Instantiates the given `module`, which can either be bytes or -* a precompiled `WebAssembly.Module`. -* -* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated. -* -* @returns {InitOutput} -*/ -export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput; - -/** -* If `module_or_path` is {RequestInfo} or {URL}, makes a request and -* for everything else, calls `WebAssembly.instantiate` directly. -* -* @param {{ module_or_path: InitInput | Promise }} module_or_path - Passing `InitInput` directly is deprecated. -* -* @returns {Promise} -*/ -export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise } | InitInput | Promise): Promise; diff --git a/wasm-release/ecs_wasm_core.js b/wasm-release/ecs_wasm_core.js deleted file mode 100644 index cdb1b1b5..00000000 --- a/wasm-release/ecs_wasm_core.js +++ /dev/null @@ -1,415 +0,0 @@ -let wasm; - -let cachedUint8ArrayMemory0 = null; - -function getUint8ArrayMemory0() { - if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { - cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); - } - return cachedUint8ArrayMemory0; -} - -function getArrayU8FromWasm0(ptr, len) { - ptr = ptr >>> 0; - return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); -} - -function addToExternrefTable0(obj) { - const idx = wasm.__externref_table_alloc(); - wasm.__wbindgen_export_2.set(idx, obj); - return idx; -} - -function handleError(f, args) { - try { - return f.apply(this, args); - } catch (e) { - const idx = addToExternrefTable0(e); - wasm.__wbindgen_exn_store(idx); - } -} - -const cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } ); - -if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); }; - -function getStringFromWasm0(ptr, len) { - ptr = ptr >>> 0; - return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); -} - -let cachedUint32ArrayMemory0 = null; - -function getUint32ArrayMemory0() { - if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) { - cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer); - } - return cachedUint32ArrayMemory0; -} - -let WASM_VECTOR_LEN = 0; - -function passArray32ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 4, 4) >>> 0; - getUint32ArrayMemory0().set(arg, ptr / 4); - WASM_VECTOR_LEN = arg.length; - return ptr; -} - -let cachedBigUint64ArrayMemory0 = null; - -function getBigUint64ArrayMemory0() { - if (cachedBigUint64ArrayMemory0 === null || cachedBigUint64ArrayMemory0.byteLength === 0) { - cachedBigUint64ArrayMemory0 = new BigUint64Array(wasm.memory.buffer); - } - return cachedBigUint64ArrayMemory0; -} - -function passArray64ToWasm0(arg, malloc) { - const ptr = malloc(arg.length * 8, 8) >>> 0; - getBigUint64ArrayMemory0().set(arg, ptr / 8); - WASM_VECTOR_LEN = arg.length; - return ptr; -} -/** - * 创建组件掩码的辅助函数 - * @param {Uint32Array} component_ids - * @returns {bigint} - */ -export function create_component_mask(component_ids) { - const ptr0 = passArray32ToWasm0(component_ids, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.create_component_mask(ptr0, len0); - return BigInt.asUintN(64, ret); -} - -/** - * 检查掩码是否包含指定组件 - * @param {bigint} mask - * @param {number} component_id - * @returns {boolean} - */ -export function mask_contains_component(mask, component_id) { - const ret = wasm.mask_contains_component(mask, component_id); - return ret !== 0; -} - -/** - * 初始化函数 - */ -export function main() { - wasm.main(); -} - -const EcsCoreFinalization = (typeof FinalizationRegistry === 'undefined') - ? { register: () => {}, unregister: () => {} } - : new FinalizationRegistry(ptr => wasm.__wbg_ecscore_free(ptr >>> 0, 1)); -/** - * 高性能ECS核心,专注于实体查询和掩码管理 - */ -export class EcsCore { - - __destroy_into_raw() { - const ptr = this.__wbg_ptr; - this.__wbg_ptr = 0; - EcsCoreFinalization.unregister(this); - return ptr; - } - - free() { - const ptr = this.__destroy_into_raw(); - wasm.__wbg_ecscore_free(ptr, 0); - } - /** - * 创建新的ECS核心 - */ - constructor() { - const ret = wasm.ecscore_new(); - this.__wbg_ptr = ret >>> 0; - EcsCoreFinalization.register(this, this.__wbg_ptr, this); - return this; - } - /** - * 创建新实体 - * @returns {number} - */ - create_entity() { - const ret = wasm.ecscore_create_entity(this.__wbg_ptr); - return ret >>> 0; - } - /** - * 删除实体 - * @param {number} entity_id - * @returns {boolean} - */ - destroy_entity(entity_id) { - const ret = wasm.ecscore_destroy_entity(this.__wbg_ptr, entity_id); - return ret !== 0; - } - /** - * 更新实体的组件掩码 - * @param {number} entity_id - * @param {bigint} mask - */ - update_entity_mask(entity_id, mask) { - wasm.ecscore_update_entity_mask(this.__wbg_ptr, entity_id, mask); - } - /** - * 批量更新实体掩码 - * @param {Uint32Array} entity_ids - * @param {BigUint64Array} masks - */ - batch_update_masks(entity_ids, masks) { - const ptr0 = passArray32ToWasm0(entity_ids, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ptr1 = passArray64ToWasm0(masks, wasm.__wbindgen_malloc); - const len1 = WASM_VECTOR_LEN; - wasm.ecscore_batch_update_masks(this.__wbg_ptr, ptr0, len0, ptr1, len1); - } - /** - * 查询实体 - * @param {bigint} mask - * @param {number} max_results - * @returns {number} - */ - query_entities(mask, max_results) { - const ret = wasm.ecscore_query_entities(this.__wbg_ptr, mask, max_results); - return ret >>> 0; - } - /** - * 获取查询结果数量 - * @returns {number} - */ - get_query_result_count() { - const ret = wasm.ecscore_get_query_result_count(this.__wbg_ptr); - return ret >>> 0; - } - /** - * 缓存查询实体 - * @param {bigint} mask - * @returns {number} - */ - query_cached(mask) { - const ret = wasm.ecscore_query_cached(this.__wbg_ptr, mask); - return ret >>> 0; - } - /** - * 获取缓存查询结果数量 - * @param {bigint} mask - * @returns {number} - */ - get_cached_query_count(mask) { - const ret = wasm.ecscore_get_cached_query_count(this.__wbg_ptr, mask); - return ret >>> 0; - } - /** - * 多组件查询 - * @param {BigUint64Array} masks - * @param {number} max_results - * @returns {number} - */ - query_multiple_components(masks, max_results) { - const ptr0 = passArray64ToWasm0(masks, wasm.__wbindgen_malloc); - const len0 = WASM_VECTOR_LEN; - const ret = wasm.ecscore_query_multiple_components(this.__wbg_ptr, ptr0, len0, max_results); - return ret >>> 0; - } - /** - * 排除查询 - * @param {bigint} include_mask - * @param {bigint} exclude_mask - * @param {number} max_results - * @returns {number} - */ - query_with_exclusion(include_mask, exclude_mask, max_results) { - const ret = wasm.ecscore_query_with_exclusion(this.__wbg_ptr, include_mask, exclude_mask, max_results); - return ret >>> 0; - } - /** - * 获取实体的组件掩码 - * @param {number} entity_id - * @returns {bigint} - */ - get_entity_mask(entity_id) { - const ret = wasm.ecscore_get_entity_mask(this.__wbg_ptr, entity_id); - return BigInt.asUintN(64, ret); - } - /** - * 检查实体是否存在 - * @param {number} entity_id - * @returns {boolean} - */ - entity_exists(entity_id) { - const ret = wasm.ecscore_entity_exists(this.__wbg_ptr, entity_id); - return ret !== 0; - } - /** - * 获取实体数量 - * @returns {number} - */ - get_entity_count() { - const ret = wasm.ecscore_get_entity_count(this.__wbg_ptr); - return ret >>> 0; - } - /** - * 获取性能统计信息 - * @returns {Array} - */ - get_performance_stats() { - const ret = wasm.ecscore_get_performance_stats(this.__wbg_ptr); - return ret; - } - /** - * 清理所有数据 - */ - clear() { - wasm.ecscore_clear(this.__wbg_ptr); - } - /** - * 重建查询缓存 - */ - rebuild_query_cache() { - wasm.ecscore_rebuild_query_cache(this.__wbg_ptr); - } -} - -async function __wbg_load(module, imports) { - if (typeof Response === 'function' && module instanceof Response) { - if (typeof WebAssembly.instantiateStreaming === 'function') { - try { - return await WebAssembly.instantiateStreaming(module, imports); - - } catch (e) { - if (module.headers.get('Content-Type') != 'application/wasm') { - console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e); - - } else { - throw e; - } - } - } - - const bytes = await module.arrayBuffer(); - return await WebAssembly.instantiate(bytes, imports); - - } else { - const instance = await WebAssembly.instantiate(module, imports); - - if (instance instanceof WebAssembly.Instance) { - return { instance, module }; - - } else { - return instance; - } - } -} - -function __wbg_get_imports() { - const imports = {}; - imports.wbg = {}; - imports.wbg.__wbg_getRandomValues_3c9c0d586e575a16 = function() { return handleError(function (arg0, arg1) { - globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1)); - }, arguments) }; - imports.wbg.__wbg_log_bb5387ff27ac9b37 = function(arg0, arg1) { - console.log(getStringFromWasm0(arg0, arg1)); - }; - imports.wbg.__wbg_new_78feb108b6472713 = function() { - const ret = new Array(); - return ret; - }; - imports.wbg.__wbg_push_737cfc8c1432c2c6 = function(arg0, arg1) { - const ret = arg0.push(arg1); - return ret; - }; - imports.wbg.__wbindgen_init_externref_table = function() { - const table = wasm.__wbindgen_export_2; - const offset = table.grow(4); - table.set(0, undefined); - table.set(offset + 0, undefined); - table.set(offset + 1, null); - table.set(offset + 2, true); - table.set(offset + 3, false); - ; - }; - imports.wbg.__wbindgen_number_new = function(arg0) { - const ret = arg0; - return ret; - }; - imports.wbg.__wbindgen_throw = function(arg0, arg1) { - throw new Error(getStringFromWasm0(arg0, arg1)); - }; - - return imports; -} - -function __wbg_init_memory(imports, memory) { - -} - -function __wbg_finalize_init(instance, module) { - wasm = instance.exports; - __wbg_init.__wbindgen_wasm_module = module; - cachedBigUint64ArrayMemory0 = null; - cachedUint32ArrayMemory0 = null; - cachedUint8ArrayMemory0 = null; - - - wasm.__wbindgen_start(); - return wasm; -} - -function initSync(module) { - if (wasm !== undefined) return wasm; - - - if (typeof module !== 'undefined') { - if (Object.getPrototypeOf(module) === Object.prototype) { - ({module} = module) - } else { - console.warn('using deprecated parameters for `initSync()`; pass a single object instead') - } - } - - const imports = __wbg_get_imports(); - - __wbg_init_memory(imports); - - if (!(module instanceof WebAssembly.Module)) { - module = new WebAssembly.Module(module); - } - - const instance = new WebAssembly.Instance(module, imports); - - return __wbg_finalize_init(instance, module); -} - -async function __wbg_init(module_or_path) { - if (wasm !== undefined) return wasm; - - - if (typeof module_or_path !== 'undefined') { - if (Object.getPrototypeOf(module_or_path) === Object.prototype) { - ({module_or_path} = module_or_path) - } else { - console.warn('using deprecated parameters for the initialization function; pass a single object instead') - } - } - - if (typeof module_or_path === 'undefined') { - module_or_path = new URL('ecs_wasm_core_bg.wasm', import.meta.url); - } - const imports = __wbg_get_imports(); - - if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) { - module_or_path = fetch(module_or_path); - } - - __wbg_init_memory(imports); - - const { instance, module } = await __wbg_load(await module_or_path, imports); - - return __wbg_finalize_init(instance, module); -} - -export { initSync }; -export default __wbg_init; diff --git a/wasm-release/ecs_wasm_core_bg.wasm b/wasm-release/ecs_wasm_core_bg.wasm deleted file mode 100644 index 77cfdc3f..00000000 Binary files a/wasm-release/ecs_wasm_core_bg.wasm and /dev/null differ diff --git a/wasm-release/ecs_wasm_core_bg.wasm.d.ts b/wasm-release/ecs_wasm_core_bg.wasm.d.ts deleted file mode 100644 index 6b0a5bde..00000000 --- a/wasm-release/ecs_wasm_core_bg.wasm.d.ts +++ /dev/null @@ -1,29 +0,0 @@ -/* tslint:disable */ -/* eslint-disable */ -export const memory: WebAssembly.Memory; -export const __wbg_ecscore_free: (a: number, b: number) => void; -export const ecscore_new: () => number; -export const ecscore_create_entity: (a: number) => number; -export const ecscore_destroy_entity: (a: number, b: number) => number; -export const ecscore_update_entity_mask: (a: number, b: number, c: bigint) => void; -export const ecscore_batch_update_masks: (a: number, b: number, c: number, d: number, e: number) => void; -export const ecscore_query_entities: (a: number, b: bigint, c: number) => number; -export const ecscore_get_query_result_count: (a: number) => number; -export const ecscore_query_cached: (a: number, b: bigint) => number; -export const ecscore_get_cached_query_count: (a: number, b: bigint) => number; -export const ecscore_query_multiple_components: (a: number, b: number, c: number, d: number) => number; -export const ecscore_query_with_exclusion: (a: number, b: bigint, c: bigint, d: number) => number; -export const ecscore_get_entity_mask: (a: number, b: number) => bigint; -export const ecscore_entity_exists: (a: number, b: number) => number; -export const ecscore_get_entity_count: (a: number) => number; -export const ecscore_get_performance_stats: (a: number) => any; -export const ecscore_clear: (a: number) => void; -export const ecscore_rebuild_query_cache: (a: number) => void; -export const create_component_mask: (a: number, b: number) => bigint; -export const mask_contains_component: (a: bigint, b: number) => number; -export const main: () => void; -export const __wbindgen_exn_store: (a: number) => void; -export const __externref_table_alloc: () => number; -export const __wbindgen_export_2: WebAssembly.Table; -export const __wbindgen_malloc: (a: number, b: number) => number; -export const __wbindgen_start: () => void; diff --git a/wasm-release/package.json b/wasm-release/package.json deleted file mode 100644 index 5f459750..00000000 --- a/wasm-release/package.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "name": "@esengine/ecs-framework-wasm", - "version": "1.0.0", - "description": "ECS Framework WASM 加速模块", - "main": "ecs_wasm_core.js", - "files": [ - "ecs_wasm_core.js", - "ecs_wasm_core_bg.wasm", - "*.d.ts", - "README.md" - ], - "keywords": [ - "ecs", - "wasm", - "game-engine", - "performance" - ], - "author": "ESEngine Team", - "license": "MIT", - "peerDependencies": { - "@esengine/ecs-framework": "^1.0.0" - } -} \ No newline at end of file