Forum > General

"UTF8CodepointSizeFast" — question about existing optimization

<< < (2/3) > >>

Thaddy:
Note I believe the compiler can already optimize case loops in higher optimization settings: it is an "easy" one.

RayoGlauco:
I think originally the code was:


--- 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;      end;    end; 
Then, someone commented this
--- 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";}};} ---#248..#255 : Result := 1; and added this
--- 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";}};} ---else Result := 1;

RayoGlauco:
The final version seems faster, and avoids the compiler warning

Thaddy:
Less comparisons at the cost of one jump.
Usually this is such a case that compilers can optimize better than humans, well humans who do not take into account extra complexity. It is a kind of pre-AI type AI, well it is AI: making compilers smarter than people. :o

flowCRANE:
I still don't know what type of optimization would be used. This code might as well look like this:


--- 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;  end;end;
The order is correct, the full range of byte values has been used. A redundant compiler message about a possible missing result does not bother (although such a case does not exist for single-byte Char). So what advantage does the else version give in practice (at the asm level and various optimizations)?


--- 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;  else    Result := 1;  end;end;
Does it even matter?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version