Jun 18, 2006

How long is a piece of string

Sunday morning I was asked by an IRC regular: "Where does the engine parse quoted strings?". Being a sunday morning, I began to launch into a sermon on the distinction between CONSTANT_ENCAPSED_STRING and the problems which befall a single-pass compiler when you start to introduce interpolation. Not what he asked precisely, but an important component in answering his question. Unfortunately, at the time I was busy watching the Brasil-Australia game so I didn't go into the kind of detail I would have. Now, some 12 hours later, since Angela is off buying toe-socks in Santa Cruz, I'll bore anyone with little enough life to read my blog by explaining the pitfalls of using PHP's string interpolation without using an optimizer.

To start things off, let's take a page from my earlier discourse on Compiled Variables and look at the opcodes generated by a few simple PHP scripts:

<?php
echo "This is a constant string";
?>

Yields the nice, simple opcode:

ECHO            'This is a constant string'

No problem... Exactly what you'd expect... Now let's complicate the expressions a little:

<?php
echo "This is an interpolated $string";
?>

Yields the surprisingly messy instruction set:

INIT STRING  ~0
ADD_STRING ~0 ~0 'This'
ADD_STRING ~0 ~0 ' '
ADD_STRING ~0 ~0 'is'
ADD_STRING ~0 ~0 ' '
ADD_STRING ~0 ~0 'an'
ADD_STRING ~0 ~0 ' '
ADD_STRING ~0 ~0 'interpolated'
ADD_STRING ~0 ~0 ' '
ADD_VAR ~0 ~0 !0
ECHO ~0

Where !0 represents the compiled variable named $string. Looking at these opcodes: INIT_STRING allocates an IS_STRING variable of one byte (to hold the terminating NULL). Then it's realloc'd to five bytes by the first ADD_STRING ('This' plus the terminating NULL). Next it's realloc'd to six bytes in order to add a space, then again to eight bytes for 'is', then nine to add a space, and so on until the temporary string has the contents of the interpolated variable copied into its contents before being used by the echo statement and finally discarded. Now let's rewrite that line to avoid interpolation and use concatenation instead:

<?php
echo "This is a concatenated " . $string;
?>

Which yields the significantly shorter and simpler set of ops:

CONCAT       ~0 'This is a concatenated ' !0
ECHO ~0

A vast improvement already, but this version still creates a temporary IS_STRING variable to hold the combined string contents meaning that data is duplicated when it's being used in a const context anyway. Now let's try out this oft-overlooked use of the echo statement:

<?php
echo "This is a stacked echo " , $string;
?>

Look close, there is a meaningful difference from the last one. This time we're using a comma rather than a dot between the operands. If you don't know what the comma is doing there, ask the manual then check back here. Here's the resulting opcodes:

ECHO            'This is a stacked echo '
ECHO !0

Same number of opcodes, but this time no temporary variables are being created so there's no duplication and no pointless copying (unless of course $string wasn't of type IS_STRING, in which case it does have to be converted for output, but don't get picky now). Think this is bad? Consider the average heredoc string which spans several lines of prepared output embedding perhaps a handful of variables along the way. Here's one of several such blocks found in run-tests.php within the PHP distribution source tree:


<?php
echo <<NO_PCRE_ERROR

+-----------------------------------------------------------+
| ! ERROR ! |
| The test-suite requires that you have pcre extension |
| enabled. To enable this extension either compile your PHP |
| with --with-pcre-regex or if you've compiled pcre as a |
| shared module load it via php.ini. |
+-----------------------------------------------------------+

NO_PCRE_ERROR;
?>

Notice that we're not even embedding variables to be interpolated here, yet does this come out to a simple, single opcode? Nope, because the rules necessary to catch a heredoc's end token demand the same careful examination as double-quoted variable substitution and you wind up (in this case) with SEVENTY-EIGHT opcodes! One INIT_STRING, 76 ADD_STRINGs. and a final ECHO. That means a malloc, 76 reallocs, and a free which will be executed every time that code snippet comes along. Even the original contents take up more memory because they're stored in 76 distinct zval/IS_STRING structures.

Why does this happen? Because there are about a dozen ways that a variable can be hidden inside an interpolated string. Similarly, when looking for a heredoc end-token, the token can be an arbitrary length, containing any of the label characters, and may or may not sit on a line by itself. Put simply, it's too difficult to encompass in one regular expression.

The engine could perform a second-pass during compilation, however the time saved reassembling these strings will typically be about the same amount of time spent actually processing them during runtime (if one assumes that each instance will execute exactly once). Rather than complicate the build process (potentially slowing down overall run-times in the process), the compiler leaves this optimization step to opcode caches which can achieve exponentially greater advantage cleaning up this mess then caching the results and reusing the faster, leaner versions on all subsequent runs.

If you're using APC, you'll find just such an optimizer built in, but not enabled by default. To turn it on, you'll need to set apc.optimization=on in your php.ini. In addition to stitching these run-on opcodes back together, it'll also add run-time speed-ups like pre-resolving persistent constants to their actual values, folding static scalar expressions (like 1 + 1) to their fixed results (e.g. 2), and simpler stuff like avoiding the use of JMP when the target is the next opcode, or boolean casts when the original expression is known to be a boolean value. (It should be noted that these speed-ups also break some of the runtime-manipulation features of runkit, but that was stuff you....probably should have been doing anyway)

Can't use an optimizer because your webhost doesn't know how to set php.ini options? You can still avoid 90% of the INIT_STRING/ADD_STRING dilema by simply using single quotes and concatenation (or commas when dealing with echo statements). It's a simple trick and one which shouldn't harm maintainability too much, but on a large, complicated script, you just might see an extra request or two per second.


72 comments:

  1. Very interesting article. I'm interested to know the process PHP goes through when it uses the sprintf function. Due to the fact it's a function it would seem apparent that sprintf is slower, but it would be nice to see the figures and compare.

    ReplyDelete
  2. php training in chennaiJanuary 21, 2015 at 1:49:00 AM PST

    Hi this is Ganesh i am having 3 years of experience as a java developer and i am certified. i have knowledge on OOPS concepts in java but dont know indepth. After learning php will be enough to get a good career in IT with good package? and i crossed php training in chennai website where someone please help me to identity the syllabus covers everything or not??

    thanks,Ganesh.

    ReplyDelete
  3. php training in chennaiJanuary 21, 2015 at 1:51:00 AM PST


    Hi this is Ganesh i am having 3 years of experience as a java developer and i am certified. i have knowledge on OOPS concepts in java but dont know indepth. After learning php will be enough to get a good career in IT with good package? and i crossed php training in chennai website where someone please help me to identity the syllabus covers everything or not??

    thanks,Ganesh

    ReplyDelete
  4. Dot Net Training in chennaiJanuary 21, 2015 at 2:06:00 AM PST

    Hi this is Rajesh i am having 3 years of experience as a php developer and i am certified. i have knowledge on OOPS concepts in php but dont know indepth. After learning Dot Net will be enough to get a good career in IT with good package? and i crossed Dot Net training in chennai website where someone please help me to identity the syllabus covers everything or not??

    thanks, Rajesh

    ReplyDelete
  5. Dot Net Training In chennaiJanuary 21, 2015 at 2:08:00 AM PST

    Hi this is Rajesh i am having 3 years of experience as a php developer and i am certified. i have knowledge on OOPS concepts in php but dont know indepth. After learning Dot Net will be enough to get a good career in IT with good package? and i crossed Dot Net training in chennai website where someone please help me to identity the syllabus covers everything or not??

    thanks, Rajesh

    ReplyDelete
  6. Hi, this is Ganesh I am having 3 years of experience as a Dot Net developer and I am certified. I have Knowledge on OOPS Concepts in .NET indepth. After learning Salesforce will be enough to get a good career in IT with good Package? and i crossed Salesforce Training in Chennai website where someone please help me to identify the syllabus covers everything or not??

    Thanks,
    Ganesh

    ReplyDelete
  7. Hi, this is Ganesh I am having 3 years of experience as a Dot Net developer and I am certified. I have Knowledge on OOPS Concepts in .NET indepth. After learning Salesforce will be enough to get a good career in IT with good Package? and i crossed Salesforce Training in Chennai website where someone please help me to identify the syllabus covers everything or not??

    Thanks,
    Ganesh

    ReplyDelete
  8. hadoop training in chennaiJanuary 22, 2015 at 4:25:00 AM PST

    Hi this is raj i am having 3 years of experience as a php developer and i am certified. i have knowledge on OOPS concepts in php but dont know indepth. After learning hadoop will be enough to get a good career in IT with good package? and i crossed hadoop training in chennai website where someone please help me to identity the syllabus covers everything or not??
    Thanks,
    raj

    ReplyDelete
  9. Hi this is vignesh i am having 3 years of experience as a android developer and i am certified. i have knowledge on OOPS concepts in android but dont know indepth. After learning hadoop will be enough to get a good career in IT with good package? and i crossed hadoop training in chennai website where someone please help me to identity the syllabus covers everything or not??
    Thanks,
    vignesh

    ReplyDelete
  10. Hi this is Kathiresan i am having 3 years of experience as a dot net developer and i am certified. i have knowledge on OOPS concepts in .NET but dont know indepth. After learning android will be enough to get a good career in IT with good package? and i crossed Android Training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks,
    kathiresan

    ReplyDelete
  11. Hi this is Kathiresan i am having 3 years of experience as a dot net developer and i am certified. i have knowledge on OOPS concepts in .NET but dont know indepth. After learning android will be enough to get a good career in IT with good package? and i crossed Android Training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks,
    kathiresan

    ReplyDelete
  12. Hi this is dwarakesh i am having 3 years of experience as a php developer and i am certified. i have knowledge on OOPS concepts in php but dont know
    indepth. After learning java will be enough to get a good career in IT with good package? and i crossed
    java training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    thanks, dwarakesh

    ReplyDelete

  13. Hi this is dwarakesh i am having 3 years of experience as a php developer and i am certified. i have knowledge on OOPS concepts in php but dont know
    indepth. After learning java will be enough to get a good career in IT with good package? and i crossed
    java training in chennai website where someone please help me to identity the syllabus covers everything or not??

    thanks, dwarakesh

    ReplyDelete

  14. Hi this is abinaya i am having 3 years of experience as a java developer and i am certified. i have knowledge on OOPS concepts in java but dont know indepth. After learning oracle will be enough to get a good career in IT with good package? and i crossed Oracle Training in Chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks, abinaya

    ReplyDelete
  15. Hi this is abinaya i am having 3 years of experience as a java developer and i am certified. i have knowledge on OOPS concepts in java but dont know indepth. After learning oracle will be enough to get a good career in IT with good package? and i crossed oracle training chennai website where someone please help me to identity the syllabus covers everything or not??

    Thanks, abinaya

    ReplyDelete
  16. Thanks for the informative blog for best SAP Training in Chennai contact IICT Chromepet.

    ReplyDelete
  17. Hi this is ravi planned for SAP Training kindly please suggest me whether its a good syllabus for SAP Training in Chennai..Thanks in advance..

    ReplyDelete
  18. Thanks for the informative blog .... For details of iOS Training in Chennai please visit IICT Chromepet.

    ReplyDelete
  19. Excellent blog and thanks a lot for useful info. For informatica training in chennaiplease visit IICT Chromepet.

    ReplyDelete
  20. Hi, I am christina lives in Chennai. I am technology freak. I did Android mobile application development course in Chennai at reputed training institutes, this is very usful for me to make a bright carrer in IT industry. So If anyone want to get best Android Course in Chennai please visit fita academy which offers real time Android Training in Chennai at reasonable cost.







    ReplyDelete
  21. Your blog is really useful for me. Thanks for sharing this informative blog. If anyone wants to get real time Oracle Training in Chennai reach FITA Oracle Training Institutes in Chennai. They give professional and job oriented training for all students.

    ReplyDelete
  22. The information you posted here is useful to make my career better keep updates..I did Salesforce.com Training in Chennai at FITA academy. Its really useful for me to make bright future in IT industry.

    ReplyDelete
  23. I have read your blog and i got a very useful and knowledgeable information from your blog.its really a very nice article.You have done a great job . If anyone want to get Salesforce Course in Chennai, Please visit FITA academy located at Chennai Velachery.

    ReplyDelete
  24. The information you posted here is useful to make my career better keep updates...If anyone want to get Cloud Computing Training in Chennai, Please visit FITA academy located at Chennai Velachery which offer best Cloud Computing Course in Chennai.




    ReplyDelete
  25. Your posts is really helpful for me.Thanks for your wonderful post.It is really very helpful for us and I have gathered some important information from this blog.If anyone wants to get Dot Net Training in Velachery reach FITA, rated as No.1 Dot Net Training Institutes in Chennai.

    ReplyDelete
  26. Your blog is really useful for me. Thanks for sharing this useful blog..Suppose if anyone interested to learn Android Training Chennai please visit fita academy which offers best Android Course in Chennai at reasonable cost.

    ReplyDelete
  27. Your blog is really awesome. Thanks for sharing this blog. If anyone want to get PHP Training in Chennai, please visit Fita academy located at Chennai, Velachery.

    ReplyDelete
  28. I get a lot of great information from this blog. Recently I did oracle certification course at a leading academy. If you are looking for best Oracle Course in Chennai visit FITA IT training and placement academy which offer SQL Training in Chennai.

    ReplyDelete
  29. Hi I am Emi lives in Chennai. I am a technology freak. Recently I did Best Java Training in Chennai at a leading Java Training Institutes in Chennai. This is really helpful for me to make a bright carrer in IT industry.

    ReplyDelete

  30. I get a lot of great information from this blog. Thank you for your sharing this informative blog. Recently I did PHP course at a leading academy. If you are looking for best PHP Training Institute in Chennai visit FITA IT training and placement academy which offer real timePHP Training in Chennai.

    ReplyDelete

  31. The information you have posted here is really useful and interesting too & here, I had a chance to gather some useful tactics in programming, thanks for sharing and I have an expectation about your future blogs keep your updates please.
    sap courses in Chennai
    sap training institute in Chennai

    sap training in Chennai

    ReplyDelete
  32. Thanks for sharing this informative blog. FITA provides Salesforce Training in Chennai with years of experienced professionals and fully hands-on classes. Salesforce is a cloud based CRM software. Today's most of the IT industry use this software for customer relationship management. To know more details about salesforce reach FITA Academy. Rated as No.1 Salesforce Training Institutes in Chennai.

    ReplyDelete
  33. Salesforce Course in Chennai


    I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job . If anyone want to get Salesforce Training in Chennai, Please visit FITA academy located at Chennai Velachery.

    Salesforce Developer Training in Chennai

    Salesforce Admin Training in Chennai


    ReplyDelete
  34. Excellent post!!! Java is most popular and efficient programming language available in the market today. It helps developers to create stunning desktop/web applications loaded with stunning functionalities. JAVA J2EE Training in Chennai | Best JAVA Training institute in Chennai

    ReplyDelete
  35. Excellent blogs!!!!you have for sharing them effect information..we developer very learning to easy....oracle training in chennai

    ReplyDelete
  36. gi,i hope to really understand this information.. we have to sharing for excellent information..<a href='http://www.oracletrainingchennai.in/">oracle training in chennai</a>&
    <a href="http://www.oracletraininginchennai.in/">oracle training in chennai</a>

    ReplyDelete
  37. very nice blogs!!!oracle training in chennai& i hope to really understand and then very useful for this information..oracle training in chennai

    ReplyDelete
  38. hi,this is excellent information..we provide by very easy learning good information....selenium training in chennai

    ReplyDelete
  39. Excellent post!!! In this competitive market, customer relationship management plays a significant role in determining a business success. That too, cloud based CRM product offer more flexibility to business owners to main strong relationship with the consumers. Salesforce Training Institutes in Chennai | Salesforce Training in Chennai

    ReplyDelete
  40. Software Testing Training

    Hi, I have read your blog. Your information is really useful for me.Thanks for sharing this blog. I did QTP Training in Chennai at Fita training and placement academy which offer best Selenium Training in Chennai with years of experienced professionals. This is really useful for me to make a bright career.

    ReplyDelete
  41. SAP Institutes in Chennai

    Thanks for sharing this valuable information.and I gathered some information from this blog. I did SAP Course in Chennai, at FITA Academy which offer best SAP Training in Chennai with years of experienced professionals.

    SAP Training Institute in Chennai

    ReplyDelete
  42. Informatica training in Chennai

    Hello to all, I just got a information regarding new tech in major scale. This would be quite interesting. How could it be concentrated. Just try an concept in New feature. Big data training in Chennai

    ReplyDelete
  43. Its easy to understand about the php and can able to understand how to write the basic php programs..thnak you friends
    Software Testing Training in Chennai

    ReplyDelete
  44. Hadoop Training Chennai

    Hi, I am Christina lives in Chennai. I am technology freak. I did Hadoop Training in Chennai at FITA. This is useful for me to make a bright career in IT field.

    Hadoop Course in Chennai

    ReplyDelete
  45. HTML5 Training

    Hi, Thanks for sharing this valuable blog.I was really impressed by reading this blog. I did HTML5 Training in Chennai at reputed HTML5 Training Institutes in Chennai. This is really useful for me to make a bright future in designing field.

    HTML Training in Chennai

    ReplyDelete
  46. PHP Training Chennai

    I get a lot of great information from this blog. Thank you for your sharing this informative blog. Recently I did PHP course at a leading academy. If you are looking for best PHP Training Institute in Chennai visit FITA IT training academy which offer real timePHP Training in Chennai.

    PHP Course in Chennai

    ReplyDelete
  47. Just wanna catch up your blogs. Is this so quite interesting.

    ReplyDelete
  48. Big Data Training Chennai

    I get a lot of great information from this blog. Thank you for your sharing this informative blog. Just now I have completed hadoop certification course at a leading academy. If you are interested to learn Hadoop Training in Chennai visit FITA IT training and placement academy which offer Big Data Training in Chennai.

    ReplyDelete
  49. I have joined the oracle Trained in Training in Bangalore. Oracle Trainer will be teaching in practically Manner. So I can Easily able to understand the any Concept. This is very helpful for the interviews Purpose . I would refer my frnds to joined this institute. Visit Us, Oracle Training in Bangalore

    ReplyDelete
  50. Thanks for sharing this informative blog..Your blog is really useful for me to know some technical information. If anyone wants to get SAP ABAP Training in Chennai, please visit FITA Academy located at Chennai, rated as No.1 SAP Training Institute in Chennai.

    SAP Training in Chennai

    ReplyDelete
  51. Hi, I am Christina lives in Chennai. I am technology freak.
    I did Hadoop Training in Chennai at besant technology. This is useful for me to make a bright career in IT field. Php training in chennai

    ReplyDelete
  52. Nice to sharing to this php information details .now am looking php online traning in chennai Php training in chennai

    ReplyDelete
  53. Each string having the some specific meaning and set of information.For these we have to identify the string.
    and string length.
    Loadrunner Training in Chennai | Selenium Training in Chennai | QTP Training in Chennai

    ReplyDelete
  54. Each string having the some specific hugeness and set of information.For these we have to perceive the string and string length.

    hadoop training in chennai | hadoop training in chennai | oracle training in chennai | oracle dba training in chennai

    ReplyDelete
  55. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post.Android Training in chennai | Android Training chennai | Android course in chennai | Android course chennai

    ReplyDelete
  56. They are offer the best hadoop training for students.This training is very useful for you career.I have read you article very useful information hadoop taining. Thank you for sharing you article.Hadoop Courses in Chennai

    ReplyDelete
  57. It was Quite interesting post, keep sharing.......

    ReplyDelete
  58. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic.
    Regards,
    ccna course in Chennai

    ReplyDelete
  59. Creating Experts providing Android training with real time scenarios

    ReplyDelete
  60. IBM WebSphere touch on to a brand of computer software products in the Adventure Software Called as " INTERRATION & APPLICATION” middleware.It Allows element from separate server and midsize configurations to large dispose requiring web tier clustering over Several application server instances. These types of software products are used by end-users to create new applications and integrate the applications with several applications.
    ibm training in chennai adyar with placement

    ReplyDelete
  61. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    Salesforce training in Chennai|Salesforce training

    ReplyDelete
  62. Thanks for sharing this niche useful informative post to our knowledge, Actually PHP is an open source software that can be used in many companies for their day to day business activities it has great scope in future
    Regards,
    PHP Training in Chennai|PHP Training Chennai|Best PHP Training in Chennai

    ReplyDelete
  63. I ever had seen this information over the blog sites; actually I am looking forward for this information. Here I had an opportunity to read, it was crystal clear keep sharing because I have an expectation about your upcoming post.
    Web design training Chennai|Web design training institute in Chennai|Web design courses in Chennai

    ReplyDelete