php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #58168 unnecessary allocations in mmc_consistent_add_server
Submitted: 2008-04-19 22:36 UTC Modified: 2008-09-11 15:26 UTC
From: askalski at gmail dot com Assigned:
Status: Closed Package: memcache (PECL)
PHP Version: Irrelevant OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: askalski at gmail dot com
New email:
PHP Version: OS:

 

 [2008-04-19 22:36 UTC] askalski at gmail dot com
Description:
------------
In mmc_consistent_add_server(), it would be more efficient to allocate the key buffer once and reuse it for the (weight * MMC_CONSISTENT_POINTS) iterations of the loop, rather than alloc/free it each time.



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-04-19 23:45 UTC] askalski at gmail dot com
It also seems wasteful to keep re-hashing the host:port portion of the key.  The hash itself should also be inlined because it's called so many times.

Roughly what I have in mind (using crc32):

unsigned int crc = ~0;
char *p;

/* use host:port for base hash value */
spprintf(&key, 0, "%s:%d", mmc->host, mmc->port);
for (p = mmc->host; *p; p++)
    CRC32(crc, *p);
efree(key);

/* hash each successive point by combining the previous hash with the round number */
for (i = 0; i < points; i++)
{
    /* hash the 4 least significant bytes of i */
    CRC32(crc, i >> 24);
    CRC32(crc, i >> 16);
    CRC32(crc, i >> 8);
    CRC32(crc, i);

    state->points[state->num_points + i].server = mmc;
    state->points[state->num_points + i] = ~crc;
}
 [2008-04-19 23:51 UTC] askalski at gmail dot com
Typo in previous comment:

for (p = mmc->host; *p; p++)

Should be:

for (p = key; *p; p++)
 [2008-09-11 15:26 UTC] mikael at synd dot info
This bug has been fixed in CVS.

In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on pecl.php.net.

In case this was a pecl.php.net website problem, the change will show
up on the website in short time.
 
Thank you for the report, and for helping us make PECL better.

I've moved the memory allocation outside the loop however changing the algorithm itself would introduce inconsistencies with people who have server farms and don't update their servers all at the same time. Other clients also use the host:port-number syntax
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun May 19 11:01:37 2024 UTC