Jaslabs: High performance Software

High Performance Software

Archive for July, 2006

Optimizing PHP objects

By Justin Silverton 

The following tips can help in optimizing object-orientated PHP.

1. Initialise all variables before use.

2. Dereference all global/property variables that are frequently used in a method and put the values in local variables if you plan to access the value more than twice.

3. Try placing frequently used methods in the derived classes.

Warning: as PHP is going through a continuous improvement process, things might change in the future.

More Details

I have found that calling object methods (functions defined in a class) are about twice as slow as a normal function calls. To me that’s quite acceptable and comparable to other OOP languages.

Inside a method (the following ratios are approximate only):

1. Incrementing a local variable in a method is the fastest. Nearly the same as calling a local variable in a function.
2. Incrementing a global variable is 2 times slow than a local var.
3. Incrementing a object property (eg. $this->prop++) is 3 times slower than a local variable.
4. Incrementing an undefined local variable is 9-10 times slower than a pre-initialized one.
5. Just declaring a global variable without using it in a function also slows things down (by about the same amount as incrementing a local var). PHP probably does a check to see if the global exists.
6. Method invocation appears to be independent of the number of methods defined in the class because I added 10 more methods to the test class (before and after the test method) with no change in performance.
7. Methods in derived classes run faster than ones defined in the base class.
8. A function call with one parameter and an empty function body takes about the same time as doing 7-8 $localvar++ operations. A similar method call is of course about 15 $localvar++ operations.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
20 comments

Top 25 PHP template engines

By Justin Silverton

After searching the Internet for a good template engine, I have compiled the following list:

Smarty
Smarty is a template engine that compiles the templates into PHP scripts, then executes those scripts. Very fast, very flexible.

Heyes Template Class
A very easy to use, yet powerful and quick template engine that enables you to separate your page layout and design from your code.

FastTemplate
A simple variable interpolation template class that parses your templates for variables and spits out HTML with their values

ShellPage
A simple and easy to use class that lets you make whole websites based on template files for layouts. Change the template and your whole site changes.

STP Simple Template Parser
A simple, light weight and easy to use template parser class. It can assemble a page from several templates, output result pages to the browser or write them to the filesystem.

OO Template Class
An object oriented template class you can use in your own programs.

SimpleTemplate
A template engine to create and structure websites and applications. It can translate and compile the templates.

bTemplate
A small and fast template class that allows you to separate your PHP logic from your HTML presentation code.

Savant
A powerful but lightweight PEAR-compliant template system. It is non-compiling, and uses PHP itself as its template language.

ETS - easy template system
A template system that allows you to reshuffle templates with exactly the same data.

EasyTemplatePHP
A simple, yet powerful templating system for your site.

vlibTemplate
A fast, full featured template system that includes a caching and debugging class.

AvanTemplate
A template engine that is multi-byte safe and consumes little computing resource. It supports variable replacement and content blocks that can be set to hidden or shown.

Grafx Software’s Fast Template
A modification of the popular Fast Template system, this includes a cache function, debug console, and silent removal of unassigned dynamic blocks.

TemplatePower
A fast, simple and powerful template class. Features nested dynamic block support, block/file include support and show/hide unassigned variables.

TagTemplate
This library function was designed for use with template files and allows you to retrieve info from HTML files.

htmltmpl: templating engine
A templating engine for Python and PHP. Targeted to web application developers, who want to separate program code and design of their projects.

PHP Class for Parsing Dreamweaver templates
A simple class to parse a Dreamweaver template for use in custom mods for a Gallery 2 and a WordPress blog.

MiniTemplator (Template Engine)
A compact template engine for HTML files. It features a simple syntax for template variables and blocks. Blocks can be nested.

Layout Solution
Simplifies website development and maintenance. It holds commonly used variables and page elements so you don’t need to duplicate common layouts over and over.

Cached Fast Template
This inclusion into FastTemplate allows for caching of the template files, and can even cache with different specifications on separate blocks of content.

TinyButStrong
A template engine that supports MySQL, Odbc, Sql-Server and ADODB. It includes seven methods and two properties.

Brian Lozier’s php based template engine
Only 2k in size, very fast and object-orientated.

WACT
a template engine that separates code from design.

PHPTAL
a XML/XHTML template library for PHP.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
74 comments

10 PHPbb SEO tips

By Justin Silverton

The following are 10 tips that will help you get better search engine placement with PHPbb(available for free here: http://www.phpbb.com/).

1) Remove the “View Latest Post” icon and link on index.php (Craven de Kere)

This is a page rank drain issue. The link is restored but the image is removed because it makes the page too “busy” and because the images are not as attractive as the text link we will subsequently create using the last post date.

2) Make the date of the last post link to the last post like the icon did on index.php

3) Unlink the username of the last post on index.php (Craven de Kere)As explained above the latest post link is now accessed from thelatestt post date. There are a few reasons for this. First of all a text link is almost always better than an image link. Secondly the text link will change (as the dates change). This makes the link more attractive to search engines but most importantly it provides a slight chance at more keyword traffic by branding the link with a date and time as a keyword.

This is purely for page rank drain and to direct the search engine spiders to the content. Pages with too many links are penalized by some search engialgorithmsyms. The links to the profiles are also not the focus of the site. Most users are not searching the internet for information in your users’ profiles. So this helps direct the search engines to the content on the topics instead of the many other links search engines could follow.
4) Remove listing of moderators on index.php

This is a MOD by John Abela. It removes the listing of moderators on the index page. This helps make the page less “busy” and also helps direct the spiders toward the content and not the profiles. It reduces the index page’s queries by 2, so it also helps improve forum speed a bit.

5) Remove session IDs for all search engine spiders

This is a crucial part of this mod. It is based on code that was posted by a user named Darlantan but it is unclear who wrote the original snippet.

This code removsessionion IDs for all guests. This means ALL spiders can spider your forum without session IDs in the URLs.

NOTE: This means you can’t enable guest posting to your forums. It won’t hamper guests in any way other than disallowing them to post. phpBB recently improved security with their session IDs so this code strikes a perfect balance between session ID integrity and search engine spidering.

6) Unlink the username of the last post on viewforum.php

This is purely for page rank drain and to direct the search engine spiders to the content. Pages with too many links are penalized by some search engialgorithmsyms. The links to the profiles are also not the focus of the site. Most users are not searching the internet for information in your users’ profiles. So this helps direct the search engines to the content on the topics instead of the many other links search engines could follow.
7) Remove the “View Latest Post” icon and link on viewforum.php

This is a page rank drain issue. The link is restored but the image is removed because it makes the page too “busy” and because the images are not as attractive as the text link we will subsequently create using the last post date.

8) Make the date of the last post link to the last post like the icon did on viewforum.php

As explained above the latest post link is now accessed from thelatestt post date. There are a few reasons for this. First of all a text link is almost always better than an image link. Secondly the text link will change (as the dates change). This makes the link more attractive to search engines but most importantly it provides a slight chance at more keyword traffic by branding the link with a date and time as a keyword.

9) Unlink the author username on viewforum.php

This is purely for page rank drain and to direct the search engine spiders to the content. Pages with too many links are penalized by some search enginalgorithmsms. The links to the profiles are also not the focus of the site. Most users are not searching the internet for information in your users’ profiles. So this helps direct the search engines to the content on the topics instead of the many other links search engines could follow.

10) Remove signatures for guest views

This modification removes repetitive text from the topic pages. As signatures are text that is not usually relevant to the topic they distract search engines dilutelute the key words. This makes the signatuinvisibleible for guests so that search engines (and guests!) focus on the content of the topic. it also helps if you allow users to put links in their signatures as you will not lose any page rank through these links anymore.

The above tips were originally found in an a forum post here: http://www.able2know.com/forums/about15132.html

A commercial plugin can also be found here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
4 comments

protecting your PHP code

By Justin Silverton

Introduction

A client of mine approached me today and was interested in releasing a PHP based product, but didn’t want his source code to be viewed, in plaintext, by the people purchasing it (mainly because competitors can could easily just purchase a copy and integrate his source code into their product). So, I researched the different options available to protect source code.

What doesn’t work

The various encoders available do not work. These companies/products should just release these products as accelerators (which can improve speed by up to 10X) and not a secure and reliable way of hiding source code.

http://www.phprecovery.com is a website that charges money to decode the following types of encoded files (it is just an example site that I found. There are many more just like it):

  • Zend
  • Ioncube
  • SourceGuardian
  • TurckMM
  • SourceCop
  • ScopBin
  • Zend (Gaspra)
  • Ioncube (last)
  • CodeLock

This site has been tested and it does work. Most people would not bother with the hassle of paying someone to decode your application, but if you offer a more expensive version that includes the full source (and the price is more than it would cost to decode it), then it might just be a better solution.

What works

The best solution is code obfuscation. It may not be perfect, and in some instances, you may have to change your code around a little bit, but it will make it very difficult to re-use your source code.

I prefer a free program called POBS, available Here

How it works:

Replace namesPOBS replaces user-defined (NOT predefined) functions, constants and variables with a MD5 key of 8 characters. (It doesn’t use MD5 keys of 32 bytes, which is standard, since that would increase the size of your sourcecode). 8 bytes seems enough to give each functions or variable its unique name. MD5 is not reversible.POBS replaces user-defined (NOT predefined) functions, constants and variables with a MD5 key of 8 characters. (It doesn’t use MD5 keys of 32 bytes, which is standard, since that would increase the size of your sourcecode). 8 bytes seems enough to give each functions or variable its unique name. MD5 is not reversible.The first letter of the new functionname is a “F”, of a variable a “V” and of a constant a “C”POBS replaces user-defined (NOT predefined) functions, constants and variables with a MD5 key of 8 characters. (It doesn’t use MD5 keys of 32 bytes, which is standard, since that would increase the size of your sourcecode). 8 bytes seems enough to give each functions or variable its unique name. MD5 is not reversible.The first letter of the new functionname is a “F”, of a variable a “V” and of a constant a “C”The function with name MakeImageHtml is replaced by Fee2c1bdc
The variable $ImgText is replaced by $V1d9d94a6
The constant USERDIR is replaced by C389a367e

POBS replaces user-defined (NOT predefined) functions, constants and variables with a MD5 key of 8 characters. (It doesn’t use MD5 keys of 32 bytes, which is standard, since that would increase the size of your sourcecode). 8 bytes seems enough to give each functions or variable its unique name. MD5 is not reversible.The first letter of the new functionname is a “F”, of a variable a “V” and of a constant a “C”The function with name MakeImageHtml is replaced by Fee2c1bdcThe variable $ImgText is replaced by $V1d9d94a6The constant USERDIR is replaced by C389a367eFuther obscuring

In addition, POBS can be instructed to concatenate lines and remove comments and indents. These are not irreversible since a person can write a program to add indents and returns. But it really makes a mess of your code and therefore furtherly discourages many wouldbe hackers from trying to reverse-engineer your code.

Exclude stuff

POBS allows you to indicate which user-defined variables, constants and functions need to be excluded from replacing. In the settings file “pobs-ini.inc” you can add these names to the arrays $UdExVarArray, $UdExcConstArray and $UdExcFuncArray. Do NOT use dollarsigns here.

In $UdExVarArray you are allowed to use wildcards in the form of an asterix (*) at the end of each variablename. I.e. params_* will exclude params_type, params_address and params_name. So if you name your variables to a certain convention you can easily and securely exclude them by group. This way you don’t have to be afraid you forgot to add it to the array in case you added a new variable to your code.

ProcessPOBS consists of 2 major processes.POBS consists of 2 major processes.1. POBS first scans all the files with the file-extensions allowed in the sourcedirectory. While scanning, it makes a list of userdefined variables, functions and constants it has located in your sourcecodePOBS consists of 2 major processes.1. POBS first scans all the files with the file-extensions allowed in the sourcedirectory. While scanning, it makes a list of userdefined variables, functions and constants it has located in your sourcecode2. POBS now knows which ones it should replace and starts writing new files in the target directory

POBS consists of 2 major processes.1. POBS first scans all the files with the file-extensions allowed in the sourcedirectory. While scanning, it makes a list of userdefined variables, functions and constants it has located in your sourcecode2. POBS now knows which ones it should replace and starts writing new files in the target directory

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
10 comments

PHP speed hacks

By Justin Silverton

A French Translation can be found here: http://www.vecteur-renaissance.com/vhoen/index.php/2006/04/15/27-10-conseils-pour-accelerer-des-scripts-php

  1. A PHP script will be served at least 5-10 times slower than a static HTML page by Apache. Try to use more static HTML pages and fewer scripts. 
  2. Enable the compression of HTML by putting in your php.ini:output_handler = ob_gzhandler 
  3. Install a PHP caching suite. I have personally used zend (commercial), turck mmcache, and ioncube, and they all work very well. 
  4. Switch from file based sessions to shared memory sessions. Compile PHP with the –with-mm option and set session.save_handler=mm in php.ini. This can drastically improve session management performance.
  5. Another caching technique that can be used when you have pages that don’t change too often is to cache the HTML output of your PHP pages. (a list of template solutions is posted within one of my previous articles).
  6. Use output buffering (See ob_start). This will speed up your PHP code by 5-15% if you frequently print or echo in your code. 
  7. On Windows, FastCGI is the highest performance way of running PHP with Apache. 
  8. In PHP4, objects and arrays should be passed to functions by reference (with &), and everything else by value. In PHP5, objects are already passed by reference.
  9. Don’t use images when text will do. Reduce your image sizes with a software like MacroMedia Fireworks or imagemagick. 
  10. If possible, Run your database server on a different machine. If all static content is served from another server, then you can turn off KeepAlives in httpd.conf to speed up client disconnects. 
Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati
8 comments

Next Page »