Skip to content

Commit

Permalink
feat: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
Wroud committed Nov 25, 2024
1 parent 310a66c commit 03afb24
Show file tree
Hide file tree
Showing 96 changed files with 2,007 additions and 295 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"explorer.fileNesting.enabled": true,
"explorer.fileNesting.expand": false,
"explorer.fileNesting.patterns": {
"*.ts": "${capture}.js, ${capture}.test.tsx, ${capture}.test.ts, ${capture}.*.ts, ${capture}.d.ts",
"*.ts": "${capture}.js, ${capture}.test.tsx, ${capture}.test.ts, ${capture}.bench.ts, ${capture}.*.ts, ${capture}.d.ts",
"*.d.ts": "${capture}.d.ts.map",
"*.js": "${capture}.js.map, ${capture}.min.js, ${capture}.d.ts, ${capture}.d.ts.map",
"*.jsx": "${capture}.js, ${capture}.jsx.map",
Expand Down
3 changes: 2 additions & 1 deletion packages/@wroud/di-react/src/useServiceIterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export function useServiceIterator<T>(type: IResolverServiceType<any, T>): T {
const iterator = ServiceProvider.internalGetService(
provider,
type,
new Set(),
null,
{ next: null, value: null },
"async",
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ServiceCollectionProxy {
},
getDescriptors<T>(
service: SingleServiceType<T>,
): IServiceDescriptor<T>[] {
): readonly IServiceDescriptor<T>[] {
return collection.getDescriptors(service);
},
addScoped<T>(
Expand Down
3 changes: 2 additions & 1 deletion packages/@wroud/di-tools-analyzer/src/loadImplementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export async function getDeps<T>(descriptor: IServiceDescriptor<T>) {
return null as any;
},
descriptor,
new Set(),
null,
{ next: null, value: null },
"async",
),
);
Expand Down
1 change: 1 addition & 0 deletions packages/@wroud/di-tools-benchmark/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# @wroud/di-tools-benchmark
48 changes: 48 additions & 0 deletions packages/@wroud/di-tools-benchmark/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
{
"name": "@wroud/di-tools-benchmark",
"type": "module",
"private": true,
"sideEffects": [],
"exports": {
".": "./lib/index.js",
"./*": "./lib/*.js"
},
"scripts": {
"build:legacy-decorators": "tsc --build tsconfig.legacy-decorators.json",
"build:modern": "tsc --build tsconfig.modern.json",
"build:benchmark": "tsc --build tsconfig.json",
"build": "yarn build:modern && yarn build:legacy-decorators && yarn build:benchmark",
"bench": "benchmark run",
"clear": "rimraf lib"
},
"files": [
"package.json",
"LICENSE",
"README.md",
"CHANGELOG.md",
"lib",
"!lib/**/*.d.ts.map",
"!lib/**/*.test.js",
"!lib/**/*.test.d.ts",
"!lib/**/*.test.d.ts.map",
"!lib/**/*.test.js.map",
"!lib/tests",
"!.tsbuildinfo"
],
"packageManager": "yarn@4.5.0",
"devDependencies": {
"@wroud/tsconfig": "workspace:^",
"rimraf": "^6",
"typescript": "^5"
},
"dependencies": {
"@wroud/di": "workspace:^",
"@wroud/tests-runner": "workspace:^",
"brandi": "^5",
"inversify": "^6",
"reflect-metadata": "^0",
"tslib": "^2",
"tsyringe": "^4",
"vitest": "^2"
}
}
47 changes: 47 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/createRegisterGet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { bench } from "vitest";
import { ServiceContainerBuilder, single } from "@wroud/di";
import {
createServicesTreeWroudDi,
type ServicePair,
} from "./tests/createServicesTreeWroudDi.js";

const singletonServices: ServicePair[] = [];
const rootSingleton = createServicesTreeWroudDi(8, 1, singletonServices)!;

const transientServices: ServicePair[] = [];
const rootTransient = createServicesTreeWroudDi(8, 1, transientServices)!;

const scopedServices: ServicePair[] = [];
const rootScoped = createServicesTreeWroudDi(8, 1, scopedServices)!;

const builder = new ServiceContainerBuilder();

for (const { service, impl } of singletonServices) {
builder.addSingleton(service, impl);
}

for (const { service, impl } of transientServices) {
builder.addTransient(service, impl);
}

for (const { service, impl } of scopedServices) {
builder.addScoped(service, impl);
}

const singletonResolver = single(rootSingleton.service);
const transientResolver = single(rootTransient.service);
const scopedResolver = single(rootScoped.service);

bench(
"[@wroud/di]",
() => {
const serviceProvider = builder.build();
serviceProvider.getService(singletonResolver);
serviceProvider.getService(transientResolver);
serviceProvider.createScope().serviceProvider.getService(scopedResolver);
},
{
time: 5000,
warmupTime: 1000,
},
);
17 changes: 17 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/deep_10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { bench } from "vitest";
import { ServiceContainerBuilder } from "@wroud/di";
import { createDeepServices } from "./tests/createDeepServices.js";

const { lastService, services } = createDeepServices(10);

const builder = new ServiceContainerBuilder();

for (const { service, impl } of services) {
builder.addTransient(service, impl);
}

const serviceProvider = builder.build();

bench("[@wroud/di]", () => {
serviceProvider.getService(lastService);
});
17 changes: 17 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/deep_100.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { bench } from "vitest";
import { ServiceContainerBuilder } from "@wroud/di";
import { createDeepServices } from "./tests/createDeepServices.js";

const { lastService, services } = createDeepServices(100);

const builder = new ServiceContainerBuilder();

for (const { service, impl } of services) {
builder.addTransient(service, impl);
}

const serviceProvider = builder.build();

bench("[@wroud/di]", () => {
serviceProvider.getService(lastService);
});
97 changes: 97 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/flat_10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { bench } from "vitest";
import {
createService,
injectable,
ServiceContainerBuilder,
single,
} from "@wroud/di";

const a0 = createService("A0");
const a1 = createService("A1");
const a2 = createService("A2");
const a3 = createService("A3");
const a4 = createService("A4");
const a5 = createService("A5");
const a6 = createService("A6");
const a7 = createService("A7");
const a8 = createService("A8");
const a9 = createService("A9");
@injectable(({ single }) => [
single(a0),
single(a1),
single(a2),
single(a3),
single(a4),
single(a5),
single(a6),
single(a7),
single(a8),
])
class A9 {
constructor(
a0: any,
a1: any,
a2: any,
a3: any,
a4: any,
a5: any,
a6: any,
a7: any,
a8: any,
) {}
}

const builder = new ServiceContainerBuilder()
.addTransient(
a0,
@injectable()
class A0 {},
)
.addTransient(
a1,
@injectable()
class A1 {},
)
.addTransient(
a2,
@injectable()
class A2 {},
)
.addTransient(
a3,
@injectable()
class A3 {},
)
.addTransient(
a4,
@injectable()
class A4 {},
)
.addTransient(
a5,
@injectable()
class A5 {},
)
.addTransient(
a6,
@injectable()
class A6 {},
)
.addTransient(
a7,
@injectable()
class A7 {},
)
.addTransient(
a8,
@injectable()
class A8 {},
)
.addTransient(a9, A9);

const serviceProvider = builder.build();

const a9Resolver = single(a9);
bench("[@wroud/di]", () => {
serviceProvider.getService(a9Resolver);
});
23 changes: 23 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/get_scoped.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { bench } from "vitest";
import {
createService,
injectable,
ServiceContainerBuilder,
single,
} from "@wroud/di";

const serviceA = createService("serviceA");
@injectable()
class implA {}

const scopedProvider = new ServiceContainerBuilder()
.addScoped(serviceA, implA)
.build()
.createScope().serviceProvider;
scopedProvider.getService(serviceA);

const serviceAResolver = single(serviceA);

bench("[@wroud/di]", () => {
scopedProvider.getService(serviceAResolver);
});
22 changes: 22 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/get_singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { bench } from "vitest";
import {
createService,
injectable,
ServiceContainerBuilder,
single,
} from "@wroud/di";

const serviceA = createService("serviceA");
@injectable()
class implA {}

const singletonProvider = new ServiceContainerBuilder()
.addSingleton(serviceA, implA)
.build();

singletonProvider.getService(serviceA);

const serviceAResolver = single(serviceA);
bench("[@wroud/di]", () => {
singletonProvider.getService(serviceAResolver);
});
20 changes: 20 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/get_transient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { bench } from "vitest";
import {
createService,
injectable,
ServiceContainerBuilder,
single,
} from "@wroud/di";

const serviceA = createService("serviceA");
@injectable()
class implA {}

const serviceProvider = new ServiceContainerBuilder()
.addTransient(serviceA, implA)
.build();

const serviceAResolver = single(serviceA);
bench("[@wroud/di]", () => {
serviceProvider.getService(serviceAResolver);
});
21 changes: 21 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/register.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { bench } from "vitest";
import { createService, injectable, ServiceContainerBuilder } from "@wroud/di";

const service = createService("service");
@injectable()
class impl {}

bench("[@wroud/di] singleton", () => {
const builder = new ServiceContainerBuilder();
builder.addSingleton(service, impl);
});

bench("[@wroud/di] transient", () => {
const builder = new ServiceContainerBuilder();
builder.addTransient(service, impl);
});

bench("[@wroud/di] scoped", () => {
const builder = new ServiceContainerBuilder();
builder.addScoped(service, impl);
});
33 changes: 33 additions & 0 deletions packages/@wroud/di-tools-benchmark/src/@wroud/register_1000.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { bench } from "vitest";
import { createService, injectable, ServiceContainerBuilder } from "@wroud/di";

const services: { service: any; impl: any }[] = [];

for (let i = 0; i < 1000; i++) {
@injectable()
class impl {}
const service = createService("service");

services.push({ service, impl });
}

bench("[@wroud/di] singleton", () => {
const builder = new ServiceContainerBuilder();
for (const { service, impl } of services) {
builder.addSingleton(service, impl);
}
});

bench("[@wroud/di] transient", () => {
const builder = new ServiceContainerBuilder();
for (const { service, impl } of services) {
builder.addTransient(service, impl);
}
});

bench("[@wroud/di] scoped", () => {
const builder = new ServiceContainerBuilder();
for (const { service, impl } of services) {
builder.addScoped(service, impl);
}
});
Loading

0 comments on commit 03afb24

Please sign in to comment.