-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Sort proving jobs by epoch number (#7844)
The proving jobs queue sorted jobs in FIFO order. When we added support for proving multiple blocks simultaneously, this led to an inefficiency: if jobs for two blocks were pushed into the queue, the merge and root jobs for the first block would not be computed until the base jobs for the second one were done. This PR changes the queue so it works by priority, where priority is based on an epoch number, defined as the block number for simplicity. This also renames the "fifo" export in foundation to "queue", since we now support priority in addition to fifo queues.
- Loading branch information
1 parent
65583e3
commit 95c14a9
Showing
24 changed files
with
251 additions
and
108 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
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
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
File renamed without changes.
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,39 @@ | ||
import { type DebugLogger } from '../log/logger.js'; | ||
import { BaseMemoryQueue } from './base_memory_queue.js'; | ||
|
||
/** | ||
* A simple fifo queue. It can grow unbounded. It can have multiple producers and consumers. | ||
* Putting an item onto the queue always succeeds, unless either end() or cancel() has been called in which case | ||
* the item being pushed is simply discarded. | ||
*/ | ||
export class FifoMemoryQueue<T> extends BaseMemoryQueue<T> { | ||
private container = new FifoQueue<T>(); | ||
|
||
constructor(log?: DebugLogger) { | ||
super(log); | ||
} | ||
|
||
protected override get items() { | ||
return this.container; | ||
} | ||
} | ||
|
||
class FifoQueue<T> { | ||
private items: T[] = []; | ||
|
||
public put(item: T): void { | ||
this.items.push(item); | ||
} | ||
|
||
public get(): T | undefined { | ||
return this.items.shift(); | ||
} | ||
|
||
public get length(): number { | ||
return this.items.length; | ||
} | ||
|
||
public clear() { | ||
this.items = []; | ||
} | ||
} |
3 changes: 2 additions & 1 deletion
3
yarn-project/foundation/src/fifo/index.ts → yarn-project/foundation/src/queue/index.ts
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './memory_fifo.js'; | ||
export * from './fifo_memory_queue.js'; | ||
export * from './priority_memory_queue.js'; | ||
export * from './serial_queue.js'; | ||
export * from './bounded_serial_queue.js'; | ||
export * from './semaphore.js'; |
25 changes: 25 additions & 0 deletions
25
yarn-project/foundation/src/queue/priority_memory_queue.test.ts
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,25 @@ | ||
import { PriorityMemoryQueue } from './priority_memory_queue.js'; | ||
|
||
describe('PriorityMemoryQueue', () => { | ||
let queue: PriorityMemoryQueue<number>; | ||
|
||
beforeEach(() => { | ||
queue = new PriorityMemoryQueue<number>((a, b) => a - b); | ||
}); | ||
|
||
it('returns items in the correct order', async () => { | ||
expect(queue.put(3)).toBeTruthy(); | ||
expect(queue.put(1)).toBeTruthy(); | ||
expect(queue.put(2)).toBeTruthy(); | ||
|
||
expect(queue.length()).toEqual(3); | ||
|
||
expect(await queue.get()).toBe(1); | ||
expect(await queue.get()).toBe(2); | ||
expect(await queue.get()).toBe(3); | ||
|
||
expect(queue.length()).toEqual(0); | ||
|
||
await expect(queue.get(1)).rejects.toThrow(/timeout/i); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
yarn-project/foundation/src/queue/priority_memory_queue.ts
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,20 @@ | ||
import { BaseMemoryQueue } from './base_memory_queue.js'; | ||
import { PriorityQueue } from './priority_queue.js'; | ||
|
||
/** | ||
* A priority queue. It can grow unbounded. It can have multiple producers and consumers. | ||
* Putting an item onto the queue always succeeds, unless either end() or cancel() has been called in which case | ||
* the item being pushed is simply discarded. | ||
*/ | ||
export class PriorityMemoryQueue<T> extends BaseMemoryQueue<T> { | ||
private container: PriorityQueue<T>; | ||
|
||
constructor(comparator: (a: T, b: T) => number) { | ||
super(); | ||
this.container = new PriorityQueue(comparator); | ||
} | ||
|
||
protected override get items() { | ||
return this.container; | ||
} | ||
} |
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,34 @@ | ||
/** | ||
* Priority queue implementation based on a custom comparator. | ||
*/ | ||
export class PriorityQueue<T> { | ||
private items: T[]; | ||
|
||
constructor(private comparator: (a: T, b: T) => number) { | ||
this.items = []; | ||
} | ||
|
||
public put(item: T): void { | ||
let i = 0; | ||
while (i < this.items.length && this.comparator(item, this.items[i]) >= 0) { | ||
i++; | ||
} | ||
this.items.splice(i, 0, item); | ||
} | ||
|
||
public get(): T | undefined { | ||
return this.items.shift(); | ||
} | ||
|
||
public peek(): T | undefined { | ||
return this.items[0]; | ||
} | ||
|
||
public clear() { | ||
this.items = []; | ||
} | ||
|
||
public get length(): number { | ||
return this.items.length; | ||
} | ||
} |
4 changes: 2 additions & 2 deletions
4
...-project/foundation/src/fifo/semaphore.ts → ...project/foundation/src/queue/semaphore.ts
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
4 changes: 2 additions & 2 deletions
4
...oject/foundation/src/fifo/serial_queue.ts → ...ject/foundation/src/queue/serial_queue.ts
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
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.