Skip to content

Commit

Permalink
perf
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed Sep 10, 2024
1 parent 7543277 commit 18d6b97
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 22 deletions.
5 changes: 5 additions & 0 deletions .changeset/nice-moons-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

expose .length property on MutableList
19 changes: 10 additions & 9 deletions packages/effect/src/MutableList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,15 @@ export type TypeId = typeof TypeId
*/
export interface MutableList<out A> extends Iterable<A>, Pipeable, Inspectable {
readonly [TypeId]: TypeId
readonly length: number

/** @internal */
head: LinkedListNode<A> | undefined
/** @internal */
tail: LinkedListNode<A> | undefined
}

const MutableListProto: Omit<MutableList<unknown>, "head" | "tail"> = {
const MutableListProto: Omit<MutableList<unknown>, "head" | "tail" | "length"> = {
[TypeId]: TypeId,
[Symbol.iterator](this: MutableList<unknown>): Iterator<unknown> {
let done = false
Expand Down Expand Up @@ -72,7 +73,7 @@ const MutableListProto: Omit<MutableList<unknown>, "head" | "tail"> = {
}

interface MutableListImpl<A> extends MutableList<A> {
_length: number
length: number
}

/** @internal */
Expand Down Expand Up @@ -101,7 +102,7 @@ export const empty = <A>(): MutableList<A> => {
const list = Object.create(MutableListProto)
list.head = undefined
list.tail = undefined
list._length = 0
list.length = 0
return list
}

Expand Down Expand Up @@ -141,7 +142,7 @@ export const isEmpty = <A>(self: MutableList<A>): boolean => length(self) === 0
* @since 2.0.0
* @category getters
*/
export const length = <A>(self: MutableList<A>): number => (self as MutableListImpl<A>)._length
export const length = <A>(self: MutableList<A>): number => (self as MutableListImpl<A>).length

/**
* Returns the last element of the list, if it exists.
Expand Down Expand Up @@ -185,7 +186,7 @@ export const forEach: {
* @since 2.0.0
*/
export const reset = <A>(self: MutableList<A>): MutableList<A> => {
;(self as MutableListImpl<A>)._length = 0
;(self as MutableListImpl<A>).length = 0
self.head = undefined
self.tail = undefined
return self
Expand Down Expand Up @@ -215,7 +216,7 @@ export const append: {
node.prev = self.tail
self.tail = node
}
;(self as MutableListImpl<A>)._length += 1
;(self as MutableListImpl<A>).length += 1
return self
})

Expand Down Expand Up @@ -269,7 +270,7 @@ export const prepend: {
if (self.tail === undefined) {
self.tail = node
}
;(self as MutableListImpl<A>)._length += 1
;(self as MutableListImpl<A>).length += 1
return self
})

Expand All @@ -291,7 +292,7 @@ const remove = <A>(self: MutableList<A>, node: LinkedListNode<A>): void => {
self.tail = undefined
self.head = undefined
}
if ((self as MutableListImpl<A>)._length > 0) {
;(self as MutableListImpl<A>)._length -= 1
if ((self as MutableListImpl<A>).length > 0) {
;(self as MutableListImpl<A>).length -= 1
}
}
24 changes: 11 additions & 13 deletions packages/effect/src/internal/mailbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ type MailboxState<A, E> = {
readonly exit: Exit<void, E>
}

const asDone = core.exitAs([[] as Array<never>, true] as const)

class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
readonly [TypeId]: Api.TypeId = TypeId
readonly [ReadonlyTypeId]: Api.ReadonlyTypeId = ReadonlyTypeId
Expand Down Expand Up @@ -145,8 +143,7 @@ class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
return core.suspend(() => {
if (this.state._tag !== "Open") {
return core.succeed(false)
}
if (MutableList.length(this.messages) >= this.capacity) {
} else if (this.messages.length >= this.capacity) {
return core.map(this.offerRemaining([message]), Arr.isEmptyArray)
}
MutableList.append(this.messages, message)
Expand All @@ -157,8 +154,7 @@ class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
unsafeOffer(message: A): boolean {
if (this.state._tag !== "Open") {
return false
}
if (MutableList.length(this.messages) >= this.capacity) {
} else if (this.messages.length >= this.capacity) {
return false
}
MutableList.append(this.messages, message)
Expand All @@ -181,7 +177,7 @@ class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
if (this.state._tag !== "Open") {
return Arr.fromIterable(messages)
}
const free = this.capacity - MutableList.length(this.messages)
const free = this.capacity - this.messages.length
if (free === 0) {
return Arr.fromIterable(messages)
}
Expand Down Expand Up @@ -234,7 +230,7 @@ class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
}
end = this.done(core.exitVoid)
takeAll: Effect<readonly [messages: Array<A>, done: boolean], E> = core.suspend(() => {
const len = MutableList.length(this.messages)
const len = this.messages.length
if (len > 0) {
const messages = listToArray(this.messages, len)
MutableList.reset(this.messages)
Expand All @@ -244,17 +240,19 @@ class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
this.releaseCapacity(len)
return core.succeed([messages, false])
} else if (this.state._tag === "Done") {
return asDone(this.state.exit)
return core.exitAs(this.state.exit, [[], true])
}
return core.zipRight(this.awaitTake, this.takeAll)
})
takeN(n: number): Effect<readonly [messages: Array<A>, done: boolean], E> {
return core.suspend(() => {
if (n <= 0) {
return this.state._tag === "Done" ? asDone(this.state.exit) : core.succeed([[], false])
return this.state._tag === "Done"
? core.exitAs(this.state.exit, [[], true])
: core.succeed([[], false])
}
n = Math.min(n, this.capacity)
if (n <= MutableList.length(this.messages)) {
if (n <= this.messages.length) {
const messages = new Array<A>(n)
for (let i = 0; i < n; i++) {
messages[i] = MutableList.shift(this.messages)!
Expand All @@ -269,7 +267,7 @@ class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
})
}
take: Effect<Option.Option<A>, E> = core.suspend(() => {
if (MutableList.length(this.messages) > 0) {
if (this.messages.length > 0) {
const message = MutableList.shift(this.messages)!
this.releaseCapacity(1)
return core.succeed(Option.some(message))
Expand All @@ -290,7 +288,7 @@ class MailboxImpl<A, E> implements Api.Mailbox<A, E> {
})
})
unsafeSize(): Option.Option<number> {
return this.state._tag !== "Done" ? Option.some(MutableList.length(this.messages)) : Option.none()
return this.state._tag !== "Done" ? Option.some(this.messages.length) : Option.none()
}
size = core.sync(() => this.unsafeSize())
}
Expand Down

0 comments on commit 18d6b97

Please sign in to comment.