Forum > General

"UTF8CodepointSizeFast" — question about existing optimization

(1/3) > >>

furious programming:
The UTF8CodepointSizeFast is implemented in this way (comments are original):


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function UTF8CodepointSizeFast(p: PChar): integer;begin  case p^ of    #0..#191   : Result := 1;    #192..#223 : Result := 2;    #224..#239 : Result := 3;    #240..#247 : Result := 4;    //#248..#255 : Result := 1;    // Theoretically UTF-8 supports length 1-7, but since 2003, RFC 3629 limits    // it to 1-4 bytes.    // This is an inline function, so keep the function short.    //#248..#251   : Result := 5;    //#252, #253   : Result := 6;    //#254         : Result := 7;     else Result := 1; // An optimization + prevents compiler warning about uninitialized Result.  end;end;
Can somebody explain me please why the else statement is an optimization, as the comment says?

Thaddy:
1. makes the case stay in order for most of the part. In order case generates efficient code.
2. unexpected result are treated as resolving to case 1, which means result is always valid
3. Prevents the Jonas' case without else warning in trunk (6060)

domasz:
Is the above any better than the below?


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    function UTF8CodepointSizeFast(p: PChar): integer;    begin  Result := 1;      case p^ of        #0..#191   : Result := 1;        #192..#223 : Result := 2;        #224..#239 : Result := 3;        #240..#247 : Result := 4;        //#248..#255 : Result := 1;        // Theoretically UTF-8 supports length 1-7, but since 2003, RFC 3629 limits        // it to 1-4 bytes.        // This is an inline function, so keep the function short.        //#248..#251   : Result := 5;        //#252, #253   : Result := 6;        //#254         : Result := 7;      end;    end;

Thaddy:
No it fails my points 2 and 3. At least 2 needs to be taken into account. Since it has a less likelihood, solving it in the else will result in faster code in the common case. Unless you want exception frames and these are dead slow.

domasz:
Thank you!

Navigation

[0] Message Index

[#] Next page

Go to full version