Commit 91e47b04 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

perform the ring buffer search without copying the buffer

parent 2cc45d36
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -99,13 +99,13 @@ class Metric(key: MetricKey) {
    // we may have to load one more value from the swapfile instead from
    // the in memory ring buffer
    val rbuf_snap_len = rbuf.size
    val rbuf_snap_pos = rbuf.position
    val rbuf_snap_pos = rbuf.cursor

    // search the ring buffer backwards without synchronization. the basic
    // assumption here is that the system time will only progress forward.
    // if the system time should jump backwards this would race
    while (rbuf_pos >= 0 && rbuf_pos < rbuf_snap.size) {
      val cur = rbuf_snap(rbuf_pos)
    while (rbuf_pos >= 0 && rbuf_pos < rbuf_snap_len) {
      val cur = rbuf.at(rbuf_snap_pos, rbuf_pos)

      // since this is not synchronized, we need to check if we hit the
      // rbuf wrapping point and exit if so. this code would race if the
+11 −1
Original line number Diff line number Diff line
@@ -51,6 +51,12 @@ class RingBuffer[T: Manifest](capacity: Int) {
    lst.toList
  }

  // retrieves one item from the ring buffer at offset by walking the ring
  // buffer starting at position in reverse chronological order (from most
  // recent to oldest)
  def at(position: Int, offset: Int) : T =
    backend((((position - offset) % capacity) + capacity) % capacity)

  // removes the first num items from the start of the ring buffer (oldest
  // items get removed first)
  def seek(num: Int) = {
@@ -62,4 +68,8 @@ class RingBuffer[T: Manifest](capacity: Int) {
  def remaining : Int =
    capacity - size

  // returns a curser to the current end position of the ring buffer
  def cursor : Int =
    end

}