MattKetmo / EmailChecker

Disposable email detection library for PHP
MIT License
264 stars 79 forks source link

performance issue found using xhprof #78

Closed williamdes closed 2 years ago

williamdes commented 2 years ago

Hi, While debugging my app with xhprof I did see that email checker is consuming quite a bit of ressources on the request

it seems like the adapter will fire a new ThrowawayDomains class and so load all the file

image

image

Function Name Calls Calls% Incl. Wall Time(microsec) IWall% Incl. CPU(microsecs) ICpu% Incl.MemUse(bytes) IMemUse% Incl.PeakMemUse(bytes) IPeakMemUse%

Current Function EmailChecker\Adapter\BuiltInAdapter::construct | 1 | 4.8% | 13,161 | 4.6% | 12,000 | 4.3% | 240,704 | 2.2% | 405,664 | 3.7% Exclusive Metrics for Current Function |   |   | 9 | 0.1% | 0 | 0.0% | 480 | 0.2% | 360 | 0.1% Parent function EmailChecker\EmailChecker::construct | 1 | 100.0% | 13,161 | 100.0% | 12,000 | 100.0% | 240,704 | 100.0% | 405,664 | 100.0% Child functions EmailChecker\ThrowawayDomains::construct | 1 | 25.0% | 13,137 | 99.8% | 12,000 | 100.0% | 238,112 | 98.9% | 405,104 | 99.9% spl_autoload_call | 1 | 25.0% | 13 | 0.1% | 0 | 0.0% | 912 | 0.4% | 200 | 0.0% EmailChecker\Adapter\ArrayAdapter::construct | 1 | 25.0% | 1 | 0.0% | 0 | 0.0% | 600 | 0.2% | 0 | 0.0% EmailChecker\ThrowawayDomains::toArray | 1 | 25.0% | 1 | 0.0% | 0 | 0.0% | 600 | 0.2% | 0 | 0.0%

williamdes commented 2 years ago

Solution (temporary ?), register a custom validator that will build the object only when needed

This is the code, a bit custom for some internal reasons, but you get it

<?php

namespace App\Validator;

use EmailChecker\EmailChecker;

class NotThrowAwayValidator
{
    public const DESCRIPTION = 'The :attribute domain is invalid.';
    public const NAME = 'not_throw_away';
    public const MAIN_FUNCTION = 'validateNotThrowAway';

    public function validateNotThrowAway(string $attributeName, $value)
    {
        $emailChecker = new EmailChecker();// Not in a constructor (https://github.com/MattKetmo/EmailChecker/issues/78)

        return $emailChecker->isValid($value);
    }
}

Important

Disable package from discovery

    "extra": {
        "laravel": {
            "dont-discover": [
                "mattketmo/email-checker"
            ]
        }
    },
MattKetmo commented 2 years ago

Hello, thanks for your report, I fixed directly the BuiltInAdapter ;)

williamdes commented 2 years ago

Hello, thanks for your report, I fixed directly the BuiltInAdapter ;)

Thanks a lot, let me know when it is released :)

MattKetmo commented 2 years ago

now it is ;)