lazyqueue.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. // Copyright 2019 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package prque
  17. import (
  18. "container/heap"
  19. "time"
  20. "github.com/ethereum/go-ethereum/common/mclock"
  21. )
  22. // LazyQueue is a priority queue data structure where priorities can change over
  23. // time and are only evaluated on demand.
  24. // Two callbacks are required:
  25. // - priority evaluates the actual priority of an item
  26. // - maxPriority gives an upper estimate for the priority in any moment between
  27. // now and the given absolute time
  28. // If the upper estimate is exceeded then Update should be called for that item.
  29. // A global Refresh function should also be called periodically.
  30. type LazyQueue struct {
  31. clock mclock.Clock
  32. // Items are stored in one of two internal queues ordered by estimated max
  33. // priority until the next and the next-after-next refresh. Update and Refresh
  34. // always places items in queue[1].
  35. queue [2]*sstack
  36. popQueue *sstack
  37. period time.Duration
  38. maxUntil mclock.AbsTime
  39. indexOffset int
  40. setIndex SetIndexCallback
  41. priority PriorityCallback
  42. maxPriority MaxPriorityCallback
  43. lastRefresh1, lastRefresh2 mclock.AbsTime
  44. }
  45. type (
  46. PriorityCallback func(data interface{}) int64 // actual priority callback
  47. MaxPriorityCallback func(data interface{}, until mclock.AbsTime) int64 // estimated maximum priority callback
  48. )
  49. // NewLazyQueue creates a new lazy queue
  50. func NewLazyQueue(setIndex SetIndexCallback, priority PriorityCallback, maxPriority MaxPriorityCallback, clock mclock.Clock, refreshPeriod time.Duration) *LazyQueue {
  51. q := &LazyQueue{
  52. popQueue: newSstack(nil, false),
  53. setIndex: setIndex,
  54. priority: priority,
  55. maxPriority: maxPriority,
  56. clock: clock,
  57. period: refreshPeriod,
  58. lastRefresh1: clock.Now(),
  59. lastRefresh2: clock.Now(),
  60. }
  61. q.Reset()
  62. q.refresh(clock.Now())
  63. return q
  64. }
  65. // Reset clears the contents of the queue
  66. func (q *LazyQueue) Reset() {
  67. q.queue[0] = newSstack(q.setIndex0, false)
  68. q.queue[1] = newSstack(q.setIndex1, false)
  69. }
  70. // Refresh performs queue re-evaluation if necessary
  71. func (q *LazyQueue) Refresh() {
  72. now := q.clock.Now()
  73. for time.Duration(now-q.lastRefresh2) >= q.period*2 {
  74. q.refresh(now)
  75. q.lastRefresh2 = q.lastRefresh1
  76. q.lastRefresh1 = now
  77. }
  78. }
  79. // refresh re-evaluates items in the older queue and swaps the two queues
  80. func (q *LazyQueue) refresh(now mclock.AbsTime) {
  81. q.maxUntil = now + mclock.AbsTime(q.period)
  82. for q.queue[0].Len() != 0 {
  83. q.Push(heap.Pop(q.queue[0]).(*item).value)
  84. }
  85. q.queue[0], q.queue[1] = q.queue[1], q.queue[0]
  86. q.indexOffset = 1 - q.indexOffset
  87. q.maxUntil += mclock.AbsTime(q.period)
  88. }
  89. // Push adds an item to the queue
  90. func (q *LazyQueue) Push(data interface{}) {
  91. heap.Push(q.queue[1], &item{data, q.maxPriority(data, q.maxUntil)})
  92. }
  93. // Update updates the upper priority estimate for the item with the given queue index
  94. func (q *LazyQueue) Update(index int) {
  95. q.Push(q.Remove(index))
  96. }
  97. // Pop removes and returns the item with the greatest actual priority
  98. func (q *LazyQueue) Pop() (interface{}, int64) {
  99. var (
  100. resData interface{}
  101. resPri int64
  102. )
  103. q.MultiPop(func(data interface{}, priority int64) bool {
  104. resData = data
  105. resPri = priority
  106. return false
  107. })
  108. return resData, resPri
  109. }
  110. // peekIndex returns the index of the internal queue where the item with the
  111. // highest estimated priority is or -1 if both are empty
  112. func (q *LazyQueue) peekIndex() int {
  113. if q.queue[0].Len() != 0 {
  114. if q.queue[1].Len() != 0 && q.queue[1].blocks[0][0].priority > q.queue[0].blocks[0][0].priority {
  115. return 1
  116. }
  117. return 0
  118. }
  119. if q.queue[1].Len() != 0 {
  120. return 1
  121. }
  122. return -1
  123. }
  124. // MultiPop pops multiple items from the queue and is more efficient than calling
  125. // Pop multiple times. Popped items are passed to the callback. MultiPop returns
  126. // when the callback returns false or there are no more items to pop.
  127. func (q *LazyQueue) MultiPop(callback func(data interface{}, priority int64) bool) {
  128. nextIndex := q.peekIndex()
  129. for nextIndex != -1 {
  130. data := heap.Pop(q.queue[nextIndex]).(*item).value
  131. heap.Push(q.popQueue, &item{data, q.priority(data)})
  132. nextIndex = q.peekIndex()
  133. for q.popQueue.Len() != 0 && (nextIndex == -1 || q.queue[nextIndex].blocks[0][0].priority < q.popQueue.blocks[0][0].priority) {
  134. i := heap.Pop(q.popQueue).(*item)
  135. if !callback(i.value, i.priority) {
  136. for q.popQueue.Len() != 0 {
  137. q.Push(heap.Pop(q.popQueue).(*item).value)
  138. }
  139. return
  140. }
  141. nextIndex = q.peekIndex() // re-check because callback is allowed to push items back
  142. }
  143. }
  144. }
  145. // PopItem pops the item from the queue only, dropping the associated priority value.
  146. func (q *LazyQueue) PopItem() interface{} {
  147. i, _ := q.Pop()
  148. return i
  149. }
  150. // Remove removes removes the item with the given index.
  151. func (q *LazyQueue) Remove(index int) interface{} {
  152. if index < 0 {
  153. return nil
  154. }
  155. return heap.Remove(q.queue[index&1^q.indexOffset], index>>1).(*item).value
  156. }
  157. // Empty checks whether the priority queue is empty.
  158. func (q *LazyQueue) Empty() bool {
  159. return q.queue[0].Len() == 0 && q.queue[1].Len() == 0
  160. }
  161. // Size returns the number of items in the priority queue.
  162. func (q *LazyQueue) Size() int {
  163. return q.queue[0].Len() + q.queue[1].Len()
  164. }
  165. // setIndex0 translates internal queue item index to the virtual index space of LazyQueue
  166. func (q *LazyQueue) setIndex0(data interface{}, index int) {
  167. if index == -1 {
  168. q.setIndex(data, -1)
  169. } else {
  170. q.setIndex(data, index+index)
  171. }
  172. }
  173. // setIndex1 translates internal queue item index to the virtual index space of LazyQueue
  174. func (q *LazyQueue) setIndex1(data interface{}, index int) {
  175. q.setIndex(data, index+index+1)
  176. }