-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from jsr-core/performance-fix
feat: performance fix
- Loading branch information
Showing
20 changed files
with
469 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* @internal | ||
*/ | ||
export class RawSemaphore { | ||
#resolves: (() => void)[] = []; | ||
#value: number; | ||
#size: number; | ||
|
||
/** | ||
* Creates a new semaphore with the specified limit. | ||
* | ||
* @param size The maximum number of times the semaphore can be acquired before blocking. | ||
* @throws {RangeError} if the size is not a positive safe integer. | ||
*/ | ||
constructor(size: number) { | ||
if (size <= 0 || !Number.isSafeInteger(size)) { | ||
throw new RangeError( | ||
`size must be a positive safe integer, got ${size}`, | ||
); | ||
} | ||
this.#value = size; | ||
this.#size = size; | ||
} | ||
|
||
/** | ||
* Returns true if the semaphore is currently locked. | ||
*/ | ||
get locked(): boolean { | ||
return this.#value === 0; | ||
} | ||
|
||
/** | ||
* Acquires the semaphore, blocking until the semaphore is available. | ||
*/ | ||
acquire(): Promise<void> { | ||
if (this.#value > 0) { | ||
this.#value -= 1; | ||
return Promise.resolve(); | ||
} else { | ||
const { promise, resolve } = Promise.withResolvers<void>(); | ||
this.#resolves.push(resolve); | ||
return promise; | ||
} | ||
} | ||
|
||
/** | ||
* Releases the semaphore, allowing the next waiting operation to proceed. | ||
*/ | ||
release(): void { | ||
const resolve = this.#resolves.shift(); | ||
if (resolve) { | ||
resolve(); | ||
} else if (this.#value < this.#size) { | ||
this.#value += 1; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Barrier as Barrier100 } from "jsr:@core/asyncutil@~1.0.0/barrier"; | ||
import { Barrier } from "./barrier.ts"; | ||
|
||
const length = 1_000; | ||
|
||
Deno.bench({ | ||
name: "current", | ||
fn: async () => { | ||
const barrier = new Barrier(length); | ||
await Promise.all(Array.from({ length }).map(() => barrier.wait())); | ||
}, | ||
group: "Barrier#wait", | ||
baseline: true, | ||
}); | ||
|
||
Deno.bench({ | ||
name: "v1.0.0", | ||
fn: async () => { | ||
const barrier = new Barrier100(length); | ||
await Promise.all(Array.from({ length }).map(() => barrier.wait())); | ||
}, | ||
group: "Barrier#wait", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ | |
], | ||
"exclude": [ | ||
"**/*_test.ts", | ||
"**/*_bench.ts", | ||
".*" | ||
] | ||
}, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Lock as Lock100 } from "jsr:@core/asyncutil@~1.0.0/lock"; | ||
import { Lock } from "./lock.ts"; | ||
|
||
const length = 1_000; | ||
|
||
Deno.bench({ | ||
name: "current", | ||
fn: async () => { | ||
const lock = new Lock(0); | ||
await Promise.all(Array.from({ length }).map(() => lock.lock(() => {}))); | ||
}, | ||
group: "Lock#lock", | ||
baseline: true, | ||
}); | ||
|
||
Deno.bench({ | ||
name: "v1.0.0", | ||
fn: async () => { | ||
const lock = new Lock100(0); | ||
await Promise.all(Array.from({ length }).map(() => lock.lock(() => {}))); | ||
}, | ||
group: "Lock#lock", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Mutex as Mutex100 } from "jsr:@core/asyncutil@~1.0.0/mutex"; | ||
import { Mutex } from "./mutex.ts"; | ||
|
||
const length = 1_000; | ||
|
||
Deno.bench({ | ||
name: "current", | ||
fn: async () => { | ||
const mutex = new Mutex(); | ||
await Promise.all( | ||
Array.from({ length }).map(async () => { | ||
const lock = await mutex.acquire(); | ||
lock[Symbol.dispose](); | ||
}), | ||
); | ||
}, | ||
group: "Mutex#wait", | ||
baseline: true, | ||
}); | ||
|
||
Deno.bench({ | ||
name: "v1.0.0", | ||
fn: async () => { | ||
const mutex = new Mutex100(); | ||
await Promise.all( | ||
Array.from({ length }).map(async () => { | ||
const lock = await mutex.acquire(); | ||
lock[Symbol.dispose](); | ||
}), | ||
); | ||
}, | ||
group: "Mutex#wait", | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.