Skip to content
This repository has been archived by the owner on Jan 31, 2020. It is now read-only.

Added the FastPriorityQueue implementation #19

Merged

Conversation

ezimuel
Copy link
Contributor

@ezimuel ezimuel commented Sep 15, 2015

I added a new Stdlib's priority queue implementation based on my FastPriorityQueue project.

The class Zend\Stdlib\FastrPriorityQueue implements an efficient integer priority queue in pure PHP. This class acts like a queue, removing the elements using the extract() function, and it acts like an Iterator, without removing the elements using a loop, i.e. foreach.

The performance of this new class are very good, compared with Zend\Stdlib\SplPriorityQueue and Zend\Stdlib\PriorityQueue. I added a benchmark test in the /benchmark folder, using athletic.

Here the results of a test using an Intel Core i5-2500 at 3.30GHz with 8 Gb of RAM running Ubuntu Linux 14.04 and PHP 5.5.9:

ZendBench\Stdlib\PriorityQueue
    Method Name                Iterations    Average Time      Ops/second
    ------------------------  ------------  --------------    -------------
    insertSplPriorityQueue  : [5,000     ] [0.0000020447731] [489,051.81661]
    extractSplPriorityQueue : [5,000     ] [0.0000041113853] [243,227.01863]
    insertPriorityQueue     : [5,000     ] [0.0000032622814] [306,533.94723]
    extractPriorityQueue    : [5,000     ] [0.0000047439575] [210,794.46767]
    insertFastPriorityQueue : [5,000     ] [0.0000010054588] [994,570.80527]
    extractFastPriorityQueue: [5,000     ] [0.0000011510849] [868,745.65037]

@akrabat
Copy link
Contributor

akrabat commented Sep 15, 2015

Does this have the same functionality as Zend\Stdlib\PriorityQueue?

@weierophinney
Copy link
Member

@akrabat Yes; they both follow the same signature as SplPriorityQueue, and FastPriorityQueue acts like PriorityQueue in that it has predictable ordering of items with the same priority, and iteration does not remove items.

@akrabat
Copy link
Contributor

akrabat commented Sep 15, 2015

Why not replace PriorityQueue with this one?

@weierophinney
Copy link
Member

PriorityQueue implements IteratorAggregate vs Iterator, which means there's a slight API difference; additionally, it exposes a method for altering the internal queue class used. For BC purposes, it makes sense to keep both, though we can likely deprecate PriorityQueue when we release 2.6 with the new implementation.

public function insert($value, $priority)
{
if (!is_int($priority) || $priority < 1) {
throw new Exception\InvalidArgumentException("The priority must be a positive integer");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a BC break, and won't work with the current MVC; we often use negative priorities to indicate items that should run later (recall: high positive priority === run earlier, low negative priority === run later). We need to accommodate that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I already fixed this.

@weierophinney weierophinney merged commit e00eae3 into zendframework:develop Sep 17, 2015
weierophinney added a commit that referenced this pull request Sep 17, 2015
Added the FastPriorityQueue implementation
weierophinney added a commit that referenced this pull request Sep 17, 2015
Added the FastPriorityQueue implementation
weierophinney added a commit that referenced this pull request Sep 17, 2015
weierophinney added a commit that referenced this pull request Sep 17, 2015
{
const EXTR_DATA = 0x00000001;
const EXTR_PRIORITY = 0x00000002;
const EXTR_BOTH = 0x00000003;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Values are hexadecimal but as far as I see any scalar value could be valid, there is no binary operations and not its a bitmask.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These were written to match the SplPriorityQueue constants. They should
reflect those exactly.
On Sep 18, 2015 6:33 AM, "Maks3w" notifications@github.com wrote:

In src/FastPriorityQueue.php
#19 (comment)
:

+use Countable;
+use Serializable;
+
+/**

  • * This is an efficient implementation of an integer priority queue in PHP
  • * This class acts like a queue with insert() and extract(), removing the
  • * elements from the queue and it also acts like an Iterator without removing
  • * the elements. This behaviour can be used in mixed scenarios with high
  • * performance boost.
  • */
    +class FastPriorityQueue implements Iterator, Countable, Serializable
    +{
  • const EXTR_DATA = 0x00000001;
  • const EXTR_PRIORITY = 0x00000002;
  • const EXTR_BOTH = 0x00000003;

Values are hexadecimal but as far as I see any scalar value could be
valid, there is no binary operations and not its a bitmask.


Reply to this email directly or view it on GitHub
https://github.com/zendframework/zend-stdlib/pull/19/files#r39845972.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use the constant then?

const EXTR_DATA = SplPriorityQueue::EXTR_DATA;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense; open a pull request for the change, please.
On Sep 18, 2015 7:39 AM, "Maks3w" notifications@github.com wrote:

In src/FastPriorityQueue.php
#19 (comment)
:

+use Countable;
+use Serializable;
+
+/**

  • * This is an efficient implementation of an integer priority queue in PHP
  • * This class acts like a queue with insert() and extract(), removing the
  • * elements from the queue and it also acts like an Iterator without removing
  • * the elements. This behaviour can be used in mixed scenarios with high
  • * performance boost.
  • */
    +class FastPriorityQueue implements Iterator, Countable, Serializable
    +{
  • const EXTR_DATA = 0x00000001;
  • const EXTR_PRIORITY = 0x00000002;
  • const EXTR_BOTH = 0x00000003;

Why not use the constant then?

const EXTR_DATA = SplPriorityQueue::EXTR_DATA;


Reply to this email directly or view it on GitHub
https://github.com/zendframework/zend-stdlib/pull/19/files#r39850085.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll, we could even go more far and remove the constants and use directly SplPriorityQueue constants

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants