|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #63772 Improve BitSet::length() performance
Submitted: 2012-12-14 20:07 UTC Modified: 2012-12-17 17:07 UTC
From: harroyo at hangar18 dot cc Assigned: willfitch (profile)
Status: Closed Package: Bitset (PECL)
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: harroyo at hangar18 dot cc
New email:
PHP Version: OS:


 [2012-12-14 20:07 UTC] harroyo at hangar18 dot cc
Currently it always traverses the bitset from bit 0 to the last one, but I think 
it could 
be rewritten to traverse the bitset backwards and stop and return the first bit 
set found 
from the end. Something like this:

PHP_METHOD(BitSet, length)
	php_bitset_object *intern;
	long highest_bit = -1, i = 0, total_bits = 0;

	intern = bitset_get_intern_object(getThis() TSRMLS_CC);
	total_bits = intern->bitset_len * CHAR_BIT;

        i = total_bits;

	while(i > 0)
		if (intern->bitset_val[i / CHAR_BIT] & (1 << (i % CHAR_BIT))) {
			highest_bit = i;

	RETURN_LONG(highest_bit + 1);

I think it could also be further optimized by checking if all bits are zero one 
word at a 
time. Changing the code inside the while loop to something like this?:


                if (intern->bitset_val[i / CHAR_BIT] == (unsigned char) 0x0) {
			i -= CHAR_BIT;

		if (intern->bitset_val[i / CHAR_BIT] & (1 << (i % CHAR_BIT))) {
			highest_bit = i;


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2012-12-14 20:28 UTC]
-Assigned To: +Assigned To: willfitch
 [2012-12-17 17:07 UTC]
-Status: Assigned +Status: Closed
 [2012-12-17 17:07 UTC]
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at

 For Windows:
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Jul 14 21:01:27 2024 UTC