I’m debugging a codeigniter app that intermittently crashes (500 error) when showing a list of blog posts.
The crash happens 20-30% of page reloads – and seems to be related to either a memory issue of server misconfiguration.
First I went down the controller by doing print_r($array); exit; just below each array that is sent to the view page. No problems if you simply output the array (without passing through view) — there is never a timeout.
So I don’t think the query logic is faulty. Also CI’s profiler shows query times <.001 when the page loads correctly (when it crashes I get 500 error and cannot read the profiler’s info).
Next I moved on to print_r the view’s HTML which has loops for the arrays sent by controller. And that is where there’s a bottleneck that occasionally will cause the 500 error.
The app is designed in such a way that a single view handles all of the arrays sent by the controller and builds an HTML page with the final output.
Because of this, the view has a lot of PHP logic. Several array splits to insert ad units before running foreach loops, conditionals testing for post parameters, nested foreach loops and such.
It is pretty complex but perfectly readable. However I wonder if it’s too much logic, and if the load of this logic should be separated into smaller fragments (multiple views) and then merged together into a final HTML string.
The view has
- 30-50
if / elseif, some nested - 6-7
foreach, some nested - 5
array_splicenumerousemptyandisset
What’s your take on this issue? Do you typically avoid logic heavy views? Any advice?
Please note I’m not asking about “cleaning” views for the sake of webdesigners etc as in other SO discussions. My point here is if this structure could be causing my timeouts and what would be the potential solution.
Firstly, it’s not a question of how readable the template code is – there’s a strong correlation between complexity and the presence of bugs. Cyclomatic complexity is one way to measure complexity; from what you’ve posted, this template has a cyclomatic complexity of at least 60. In all our projects, I aim for a cyclomatic complexity of < 8, and get panicky when we get to 12.
I wouldn’t be surprised if your view code contains a bug – not just a time-out thing, but some bizarre route through the logic tree that you can’t quite picture by looking at the code.
The Williams template library gives you several tools to do this – for instance, you can define alternate views to deal with the “if question, render like this, if post render like that” logic. You can also use regions to your benefit here.
There’s an assumption that because views deal with the user interface, they are less important – but that’s simply not true.
So, let’s assume you refactor your view into a simpler structure. Will this affect processor load? Well – uh – depends.
Usually, “maintainability” and “performance” have to be traded off against eachother. The classic example is assembler code versus interpreted languages – for most people, assembler is hard to maintain, whereas interpreted scripting languages are far more tractable; on the other hand, there’s no doubt which is faster.
In your case, I think the total amount of code will increase – “extract method” as a refactoring, for instance, would increase the amount of code by a few lines – but it should be easier to cache. It should also be easier to identify exactly where the bug occurs.