Hi
A few weeks ago I wrote about checking for a valid ID value before running the byID method all the way to the Database Layer (i.e. if the ID is zero then there is really no point in doing the DB call).
I now have another question about speeding up Silverstripe. I noticed that if I write the following in my template:
$MyMethod
$MyMethod
$MyMethod
$MyMethod
$MyMethod
and place a debug::log in the MyMethod method the Page_Controller that I get a bunch of entries in my debug file (five entries). I am wondering - is it really necessary for the template to collect the value from the method five times.
I realise that collecting the value only once can have significant consequences (i.e. it is a 4.0 or 5.0 sort of change) and that it may be very hard to change the framework core. This is exactly the reason it would be good to think about this now.
I am particular thinking here where you do stuff like
<% if MyDemandingMethod %>
<% loop DemandingMethod %>
I presume the DemandingMethod would also be called twice.
Both the byID and the template one here will save milliseconds if that, but they are also extremely common and that is where it is going to make a difference - perhaps?
Nicolaas
PS a fascinating thing that happened in running the test for this little write up was that the debug log actually showed a ton more calls, made over several seconds, than the expected five... I am like - wow - someone is accessing my localhost and viewing the same page ;-) before I realised that on my localhost there were about ten images that returned 404. Thus, a call to a 404 image also called the same $MyMethod the same five times for each missing jpg file.