help-libidn
[Top][All Lists]

## Re: Maybe optimize adapt function in punycode.c (libind & libidn2)

 From: Igor V Belousov Subject: Re: Maybe optimize adapt function in punycode.c (libind & libidn2) Date: Tue, 19 Nov 2013 18:30:17 +0400

```I'm not tested benchmark in c code. I tested function 'adapt' on php. this
reduces the time of program execution.

I removed several math operation.

base, tmin and tmax it is constant.

base=36
tmin=1
tmax=26

delta =delta / ( base - tmin);
delta =delta / ( 36 - 1);
delta =delta / (35);

((base - tmin) * tmax) / 2

((36 - 1) * 26) / 2= 455

> Igor V Belousov <address@hidden> writes:
>
>>  Hi,
>>  I'm modify code in punycode.c for faster run.
>
> Hi.  Thanks for looking at the code.  Can you explain a bit more about:
>
> 1) How much faster is the code, in what kind of benchmark?
>
> 2) How the change is guaranteed to not change semantics?  Where do the
> magic values 455 and 35 come from?
>
> /Simon
>
>>  --- punycode_old.c      2013-10-29 11:13:31.000000000 +0400
>>  +++ punycode.c  2013-10-29 11:13:43.000000000 +0400
>>  @@ -146,12 +146,12 @@
>>     /* delta >> 1 is a faster way of doing delta / 2 */
>>     delta += delta / numpoints;
>>
>>  -  for (k = 0; delta > ((base - tmin) * tmax) / 2; k += base)
>>  +  for (k = 0; delta > 455; k += base) //faster way
>>       {
>>  -      delta /= base - tmin;
>>  +      delta /= 35;//faster way
>>       }
>>
>>  -  return k + (base - tmin + 1) * delta / (delta + skew);
>>  +  return k + base  * delta / (delta + skew);// base - tmin + 1 = base - 1
>> + 1
>>   }
>>
>>   /*** Main encode function ***/
>>