IfModule checks in htaccess - performance impact?

Do IfModule checks in your htaccess file actually result in a performance hit?

It doesn’t seem to make a difference on the servers I maintain (granted, they don’t get a very high amount of traffic), and I haven’t run any heavy analysis other than browser load times.

I’ve seen folks who say that it does result in a significant performance hit, and also folks that say there’s no hit, and still others that say it’s so miniscule that it doesn’t really matter. However, I haven’t seen any solid data to back this up to show which school of though is actually correct.

Has anyone seen any solid data/testing on the subject?

There are a couple ways we can measure. The first is with a simple PHP loop.

$url = 'http://localhost/some/path';
$nRequests = 100;

$start = microtime(true);
for ($i = 0; $i < $nRequests; $i++) {
    file_get_contents($url);
}
$stop = microtime(true);

$timePerRequestInSeconds = ($stop - $start) / $nRequests;
$timePerRequestInMilliseconds = $timePerRequestInSeconds * 1000;

And the second is with an HTTP stress tester, such as siege or [url=http://linux.die.net/man/1/httperf]httperf.

I tested response times with the following two htaccess files. One with an <IfModule> test:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Rewrite non-file requests to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php
</IfModule>

And one without:

RewriteEngine On

# Rewrite non-file requests to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php

Here are the results I got on my machine:

[FONT=Courier New]PHP loop:
With <IfModule>: 9.7802 ms/req +/- 0.58
Without <IfModule>: 9.74254 ms/req +/- 0.64

siege (siege -b -t10s http://localhost/some/path):
With: 1,454.244 reqs/sec +/- 8.165
Without: 1,460.032 reqs/sec +/- 8.135[/FONT]

The difference between the two is within each other’s margin or error, and even then, the difference is measured in microseconds. No doubt an “if” check executes some number of additional instructions, but the performance difference is so miniscule that it doesn’t really matter.

FF,

I’ve seen a couple members who’ve had a several <IfModule> wrapped blocks in their .htaccess. Depending upon the number of matches (and redirections) made by the .htaccess mod_rewrite code, each test will be run multiple times for every file request. Why? What does that accomplish?

Okay, I’m sure that each one takes only a few dozen machine cycles but that time is simply wasted - as is the time to load that useless code each time the .htaccess file is read. Cumulatively, the number of wasted cycles must add-up into significant delays. If you’re on a dedicated server, you’re only delaying your own visitors; if you’re on a shared server, the number of useless tests is multiplied by the number of accounts on that server. Very quickly, you’re talking about real time … and abuse of the server.

Any professional webmaster should KNOW what modules are enabled on his/her server so it is ridiculous (and an abusive of the server) to run the same test over and over and over and … As I have reminded members in my Standard Rant #4, the definition of an idiot is someone who makes the same test over and over and expects different answers each time. I’ve always reminded members to test ONCE then remove the <IfModule> blocks. I consider it a matter of professionalism and have been shocked at the number of newbies who don’t understand this … until pointed out. To have anyone argue to continue testing, well, Standard Rant #4 identifies the problem.

Regards,

DK

It’s worth noting that the reason CMSs, frameworks, etc, write the <IfModule> checks in the first place is because they want the htaccess file to be portable. It’s meant to add features to your site if a module is available, but not crash your site if a module isn’t available. Though, once the htaccess file has a permanent home on a server with a known configuration, then I agree it’s safe to remove those “if” checks.

However, if the only motivation to do so is performance, then there are much more important things we could focus on. Reduce the number of database queries, or make a sprite. Those are things that will actually matter. There’s nothing wrong with removing the “if” checks when it’s safe, but we shouldn’t exaggerate its performance impact (which is virtually nil).

Thanks for performing the test. Those were the results that I was expecting would probably turn up.

Unfortunately, JM’s test failed to account for multiple <IfModule> wrappers and multiple redirections … on a shared server. I would have expected nothing else (from a few machine cycles). I’ll bet, if he ran that test multiple times, he’d come up with the same answer! :lol:

That leaves me at the same point: Running the same test repeatedly accomplishes nothing but a waste of resources. However, I’ve changed my Standard Rant #4 as the definition I was using was for insanity, not an idiot, therefore,

[standard rant #4][indent]The definition of insanity is to repeatedly do the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T ACT INSANE! If you don’t know whether a module is enabled, run the test ONCE (without the wrapper) then delete it permanently as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/standard rant #4]

Regards,

DK

And just like that, your standard rant becomes your standard misinformation. The impact of an <IfModule> wrapper is so small that it’s barely even measurable. To call it “extremely wasteful” is just flat wrong.

Would you prefer “just plain stupid” because it accomplishes nothing but wastes machine cycles?

There’s nothing wrong with removing the “if” checks when it’s safe, but we shouldn’t exaggerate its performance impact (which is virtually nil).

By apache’s own recommendation, put a directive in httpd.conf rather than in a .htacess file if you want a (very small) increase in performance. For the vast majority of people the convenience of using .htaccess is a lot more important though.

There’s a few things to take into account:
Performance (how much of an impact will it have? In this case, very little)
Convenience (It’s convenient just to leave the “If” checks in, rather than to make all the necessary edits)
Portability (If I move the site to a new server or reuse the htaccess file, by leaving in the “If” checks, I don’t have to worry about hard failures if the module doesn’t exist).

I agree that the “best practice” is to remove the “If” checks on a server where you know that the modules are installed. For portability, this is not a best practice (And htaccess code and files tend to be ported all over the place).

However, the point was was to see how detrimental the “if” checks were to performance if they were left in (as they typically are because of portability). For a site like slashdot, milliseconds will add up. For smaller sites…not so much.

Bottom line–it doesn’t matter nearly as much as your “rants” would lead folks to believe.

I’ll add, as a guy who has done some serious web scalability stunts here and there, that micro optimizations such as this IfModule thing really don’t move the needle – we are talking nanoseconds at most, none of which will be blocking your actual precious resources which are disk and network I/O. If you are looking at real scale you start looking at farms of machines with intelligent, aggressive caching. In fact your typical requests when not under load will often get slower as you are building to scale out fast and serve everyone under some bar, not micro optimize your personal blog about banna slugs.