GithubHelp home page GithubHelp logo

queue's Introduction

Queue

GoDoc Release Software License

Lightweight, tested, performant, thread-safe, blocking FIFO queue based on auto-resizing circular buffer.

Usage

package main

import (
  "fmt"
  "sync"
  "time"

  "github.com/sheerun/queue"
)

func main() {
  q := queue.New()
  var wg sync.WaitGroup
  wg.Add(2)

  // Worker 1
  go func() {
    for i := 0; i < 500; i++ {
      item := q.Pop()
      fmt.Printf("%v\n", item)
      time.Sleep(10 * time.Millisecond)
    }
    wg.Done()
  }()

  // Worker 2
  go func() {
    for i := 0; i < 500; i++ {
      item := q.Pop()
      fmt.Printf("%v\n", item)
      time.Sleep(10 * time.Millisecond)
    }
    wg.Done()
  }()

  for i := 0; i < 1000; i++ {
    q.Append(i)
  }

  wg.Wait()
}

License

MIT

queue's People

Contributors

gronxb avatar sheerun avatar tlqaksqhr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

queue's Issues

Memory leak when using method Remove()

While using this queue implementation in one of my projects, i noticed that the memory consumption of my process was growing indefinitely.
While doing some research by profilling my application, i realized that a call to Remove() only deletes the entries of the maps Queue.items, Queue.ids for the removed element , but Queue.count value is not decreased.
The effect of this is that when the Append() function is called again, Queue.count is increased, and if Queue.count >= len(Queue.buf), the buffer is increased by twice it's size.

In other words, the next code will indefinitely grow the internal buffer and consume uneeded heap memory```

q := queue.New()
for {
   q.Append(0)
   q.Remove(0)
}

I suggest an alternative implementation which fix this problem and updates Queue.buf and Queue.count properly (but with O(n) complexity).

func (q *Queue) Remove(elem interface{}) bool {
	q.mutex.Lock()
	defer q.mutex.Unlock()

	id, ok := q.ids[elem]
	if !ok {
		return false
	}
	q.moveToFront(id)

	for {
		id := q.pop()

		item, ok := q.items[id]

		if ok {
			delete(q.ids, item)
			delete(q.items, id)
			q.onChanged()
			return true
		}
	}
}

func (q *Queue) moveToFront(id int64) {
	// Precondition: id is the id of an item in the queue. 
	// Find position in buf where item with id is located
	i := q.head
	for {
		if q.buf[i] == id {
			break
		}
		i += 1
	}
	// Move elements from J=head .. i-1 to j+1
	if i == q.head {
		// Already in the front of the queue
		return
	}

	for j := i; j > q.head; j-- {
			q.buf[j] = q.buf[j-1]
	}
	
	// Place ith element into head
	q.buf[q.head] = id
}

Wrote also a unit test:


func TestRemove(t *testing.T) {
	q := New()
	k := 500
	for  i := 0; i < k; i++ {
		q.Append(i)
	}
	for i := 0; i < k; i++ {
		value := k-i-1
		q.Remove(value)

		// q.count remains consistent
		require.Equal(t, q.count, k-i-1)

		// The rest of values remains in the queue
		for j := 0; j < value; j++ {
			require.True(t, q.Contains(j))
		}
		// Queue does not contain deleted values anymore
		for j := value; j < k; j ++ {
			require.False(t, q.Contains(j))
		}
	}

	// Queue is now empty
	require.True(t, q.Empty())
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.