Athari / YaLinqo

Yet Another LINQ to Objects for PHP [Simplified BSD]
https://athari.github.io/YaLinqo
BSD 2-Clause "Simplified" License
441 stars 39 forks source link

Memory consumption of create_function() #3

Closed cursedcoder closed 11 years ago

cursedcoder commented 11 years ago

I have noticed a lot of memory leaks if using the short syntax like

from($array)->select("explode(', ', $v)")->toArray();

But it's ok with closures

from($array)->select(function($var) { return explode(', ', $var); })->toArray();

proceeding first loop at one runtime with a big amount of data will cause to overhead of memory.

Not sure if this bottleneck is reason of lib implementation, or PHP's create_function(), but if you suggest me, I'll write a test samples of this issue.

Athari commented 11 years ago

Do you call the query within a loop? create_function should be called only once per query (in your case, only when you call select function). Calling the query in a loop will probably cause a serious overhead both in time (I'm afraid PHP will parse the function every time) and memory. If that is the case, you should refactor your code to move the query outside of the loop. If it's impossible, the functional syntax is the only option.

I hope to see PHP implement simple lambda syntax one day, to make proper functions more viable. Unfortunately, the proposal for new syntax was dropped (and laughed at).

cursedcoder commented 11 years ago

It's ok having functional syntax as the solution.

The goal of this very issue is to provide info to users.

Athari commented 9 years ago

Should be fixed by commit 243caef2d0f5dd8e423569e13cec34109712bde6 (Added lambda cache to Utils::createLambdaFromString). I feel stupid now. Not sure whether it will cause any issues, but seems to work correctly so far.