PHP Performance: Use your framework & tools properly
Imagine you start a new job and you find out that the Framework they are using is unknown to you. What will you do?
Most devs I know, do not read instructions, even IKEA ones. We tend to learn by trial & error & stack-overflow. We just follow what other devs did in the past in the project, or we try to do it as we are used to doing it.
But, what happens if the first developers on the project didn’t know the framework either? You will have a project that is not using the framework properly. But, is this wrong? Let’s see.
Normally, your framework will incorporate time-saving functions, but also optimized for performance.
Let’s put an example where you want to retrieve all the Ids of all the Delivery Companies from your DataBase that has any delay.
Most new developers on Yii2 will do this:
public function delayedCompanyIds(): array
{
$delayedCompanies = DeliveryCompany::find()->andWhere(['estimated_delay' > 0])->all();
foreach ($delayedCompanies as $delayedCompany) {
$delayedCompanyId[] = $delayedCompany->id;
}
return $delayedCompanyId;
}
This code has 2 problems:
- Bad Performance
- Has code quality problems
If we left this code in our system as is, and other developers come after, in one year you can have your application full of crap.
Les’t see both problems.
Performance:
Why is wrong this code?
- Retrieves the whole data, even columns that I won’t use (Network usage)
- The framework will create all the objects to handle the Active Record pattern over the data (memory and CPU)
Yii2 has methods for retrieving just a column, so this code can be refactored with:
public function delayedCompanyIds2(): array
{
return DeliveryCompany::find()->andWhere(['estimated_delay' > 0])->column('id');
}
Just one line, clean, easy, but it can be optimized even more for Mysql with the function group_concat:
public function delayedCompanyIds3(): array
{
return Json::decode(DeliveryCompany::find()->select('group_concat(id)')->andWhere(['estimated_delay' > 0])->scalar());
}
This version has less CPU & Network usage. But it’s a bit tricky, some developers won’t like it, and if you have a lot of Ids, is not the optimal solution, but exists.
Code Quality:
Imagine one year later: We decide to use PHPInspections, Sonarqube, PHPStan, etc… you will find that the code is not good enough:
- You must declare $delayedCompanyId:
- You must specify the type of $delayedCompanies array
The correct version may be something like this:
public function delayedCompanyIds4(): array
{
$delayedCompanyId = [];
/** @var DeliveryCompany[] $delayedCompanies */
$delayedCompanies = DeliveryCompany::find()->andWhere(['estimated_delay' > 0])->all();
foreach ($delayedCompanies as $delayedCompany) {
$delayedCompanyId[] = $delayedCompany->id;
}
return $delayedCompanyId;
}
So, you introduced bad code, other people copy&pasted you, and now you have all your application with wrong code.
Summary:
Using your framework & tools functionality saves you time coding, maintaining your code, and often can improve performance.
Please: study what your framework offers, you can build clean and fast code easier.