X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/522d7fd117285e4e4ce3ea5ec9b5a1722521b681..b961d7cdc59213673c3e4f86d3dae99daf0a1935:/build_queue.go diff --git a/build_queue.go b/build_queue.go index 7db24be..825277c 100644 --- a/build_queue.go +++ b/build_queue.go @@ -14,14 +14,6 @@ const ( buildLeafWindow = 8 ) -func newBuildQueue() *BuildQueue { - return &BuildQueue{ - queued: make(map[NodeKey]bool), - running: make(map[NodeKey]bool), - failed: make(map[NodeKey]bool), - } -} - // BuildQueue holds the nodes that wait for a model build. A leaf joins it when // its message is too long to be its own node. A merge joins it when both of its // children are built and their texts do not fit in one node. So the core never @@ -42,6 +34,17 @@ type BuildQueue struct { retry []NodeKey } +// BuildMergeHeap holds the merges that can start, by the end of their context. +type BuildMergeHeap []NodeKey + +func newBuildQueue() *BuildQueue { + return &BuildQueue{ + queued: make(map[NodeKey]bool), + running: make(map[NodeKey]bool), + failed: make(map[NodeKey]bool), + } +} + // AddLeaf adds the leaf of message id. The IDs must come in increasing order. func (q *BuildQueue) AddLeaf(id int) { q.leaves = append(q.leaves, id) @@ -79,6 +82,18 @@ func (q *BuildQueue) Next() (NodeKey, bool) { return leaf, found } +// nextLeaf returns the oldest leaf in the window that can start. +func (q *BuildQueue) nextLeaf() (NodeKey, bool) { + for _, id := range q.leaves[:min(len(q.leaves), buildLeafWindow)] { + key := NodeKey{0, id} + if !q.running[key] && !q.failed[key] { + return key, true + } + } + + return NodeKey{}, false +} + // Done removes a built node from the queue. func (q *BuildQueue) Done(key NodeKey) { delete(q.running, key) @@ -123,21 +138,6 @@ func (q *BuildQueue) Running() int { return len(q.running) } -// nextLeaf returns the oldest leaf in the window that can start. -func (q *BuildQueue) nextLeaf() (NodeKey, bool) { - for _, id := range q.leaves[:min(len(q.leaves), buildLeafWindow)] { - key := NodeKey{0, id} - if !q.running[key] && !q.failed[key] { - return key, true - } - } - - return NodeKey{}, false -} - -// BuildMergeHeap holds the merges that can start, by the end of their context. -type BuildMergeHeap []NodeKey - func (h BuildMergeHeap) Len() int { return len(h) }