Git Product home page Git Product logo

Comments (6)

niden avatar niden commented on June 9, 2024

That should be normal to be honest.

The helper factory is just that, a factory that creates additional classes. When you instantiate the factory, you have one object. Once you call say camelize on it, it will create the Camelize object transparently (and the memory will increase by doing that) and then use it to produce the result. Any subsequent calls to camelize will not increase the memory since it is already instantiated.

from cphalcon.

niden avatar niden commented on June 9, 2024

Actually I take that back. That is what should be happening but it is not. The mapper is not reusing the object.

I will sort that out shortly

from cphalcon.

niden avatar niden commented on June 9, 2024

I run the same test with some variations.

In the test you have, you are creating the helper factory and then calling camelize which in turn will create a new Camelize object. This is in the loop so you are pretty much testing what is the memory consumption of two objects being created in a loop, again and again, so this does not represent a memory leak. The better tests are as follows (to see if there is a memory leak or not)

  • Create the factory outside the loop, run the loop with the camelize in it, and see if the memory keeps increasing
  • Create the factory and call camelize outside the loop, then call camelize in the loop.

After the change I made (PR will be coming shortly) I got these results

Your test as is:

beginning: 0.41312408447266
end: 35.912994384766

Create factory outside the loop

beginning: 0.41301727294922
end: 35.903160095215

Create the factory outside the loop and call camelize, then have camelize in the loop

beginning: 0.41147613525391
end: 35.903717041016

from cphalcon.

niden avatar niden commented on June 9, 2024

#16574

Resolved

from cphalcon.

maxgalbu avatar maxgalbu commented on June 9, 2024

In the test you have, you are creating the helper factory and then calling camelize which in turn will create a new Camelize object. This is in the loop so you are pretty much testing what is the memory consumption of two objects being created in a loop, again and again, so this does not represent a memory leak.

I'm creating the helper factory but I'm also storing every instance on the same variable $helper. Shouldn't this call the destructor on the old Factory instance every time I instantiate another Factory? Why is the old instance kept in memory?

With the following userland code mimicking HelperFactory from phalcon, the memory increases linearly just like using ucwords:

<?php

echo "beginning: ".memory_get_peak_usage() / 1024 / 1024;
echo "\n";

class Camelize
{
    public function __invoke(
        string $text,
        string $delimiters = null,
        bool $lowerFirst = false
    ): string {
        if (!$delimiters) {
            $delimiters = "\n\t";
        }
        return str_replace(str_split($delimiters), "", ucwords($text, $delimiters));
    }
}

class HelperFactory
{
    private array $services = [];

    public function __call(string $name, array $arguments)
    {
        $helper = $this->newInstance($name);
        return call_user_func_array([$helper, "__invoke"], $arguments);
    }

    public function newInstance(string $name)
    {
        if (!isset($this->services[$name])) {
            $instanceName = $this->getService($name);
            $this->services[$name] = new $instanceName;
        }

        return $this->services[$name];
    }

    protected function getService($name)
    {
        return [
            "camelize"      => "Camelize",
        ][$name];
    }
}

foreach (range(1,100000) as $num) {
    $helper = new HelperFactory();
    $helper->camelize("transaction_id");
}

echo "end: ".memory_get_peak_usage() / 1024 / 1024;
echo "\n";

Output:

beginning: 0.41677856445312
end: 4.3862762451172

After the change I made (PR will be coming shortly) I got these results

Looking at these results, there seems to be no improvements at all? At least with Create factory outside the loop there should be some improvements..

from cphalcon.

maxgalbu avatar maxgalbu commented on June 9, 2024

@niden did you look at my comment? maybe you just mis-copied results after your fix?

from cphalcon.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.