|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53213 Adler32 algorithm is very slow
Submitted: 2010-10-31 18:22 UTC Modified: 2010-11-08 10:35 UTC
From: zavasek at yandex dot ru Assigned: aharvey (profile)
Status: Closed Package: hash related
PHP Version: 5.3.3 OS: Any
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: zavasek at yandex dot ru
New email:
PHP Version: OS:


 [2010-10-31 18:22 UTC] zavasek at yandex dot ru
Now adler32 algorithm for each byte uses two operations " % 65521". It is very slow.
I propose to change the function PHP_ADLER32Update in the file hash_adler32.c
	php_hash_uint32 i, s[2];
	s[0] = context->state & 0xffff;
	s[1] = (context->state >> 16) & 0xffff;
	for (i = 0; i < len; ++i) {
		s[0] += input[i];
		s[1] += s[0];
		if (s[1]>0x7fffffff)
			s[0] = s[0] % 65521;
			s[1] = s[1] % 65521;
	s[0] = s[0] % 65521;
	s[1] = s[1] % 65521;
	context->state = s[0] + (s[1] << 16);

Test script:
$t = microtime(true);
echo hash_file('adler32','/home/user/testfile')."\n"; // I used testfile size 400 MB with random content
echo sprintf('%0.3d',(microtime(true)-$t)*1000)." ms\n\n";

Actual result:
PHP 5.3.3 showed:
2174 ms

After modification the file:
664 ms

Speed was higher than about 3 times


patch.txt (last revision 2010-11-01 18:30 UTC by zavasek at yandex dot ru)

Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-10-31 19:14 UTC]
-Status: Open +Status: Feedback
 [2010-10-31 19:14 UTC]
Can you provide a patch and attach it to this bug please ("Add a patch")?
 [2010-10-31 20:40 UTC] zavasek at yandex dot ru
-Status: Feedback +Status: Open
 [2010-10-31 20:40 UTC] zavasek at yandex dot ru
I've never used SVN and do not know how to create patches.
 [2010-10-31 20:45 UTC]
-Status: Open +Status: Feedback
 [2010-10-31 20:45 UTC]
svn diff > mypatch.txt

and upload mypatch.txt here using the Add Patch link.
 [2010-11-02 04:05 UTC]
-Status: Feedback +Status: Open
 [2010-11-08 10:35 UTC]
Automatic comment from SVN on behalf of aharvey
Log: Implemented FR #53213 (Adler32 algorithm is very slow). Patch by zavasek at
yandex dot ru.
 [2010-11-08 10:35 UTC]
-Status: Open +Status: Closed -Assigned To: +Assigned To: aharvey
 [2010-11-08 10:35 UTC]
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

Patch committed to trunk. Thanks very much!
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Sep 17 08:03:36 2021 UTC