0

what is the meaning of closure in KCacheGrind? I have it with one of my functions and it is pointing out the spl_autoload_register() function, spl_autoload_call in KCacheGrind. And the self time of the function is 60+ so, of course, I want to optimize it, but I do not know where to start.

What is the closure in KCacheGrind?

What do I need to optimize the said function to lessen the self time?

Dumb Question
  • 365
  • 1
  • 3
  • 14

1 Answers1

0

A closure is a function that uses variables that are outside its local scope, but not global.

I'll use a language agnostic example since it's been forever since I've written PHP:

function someFunc() {
    var a = 0;

    return function() { // This is the closure
        a++;
        return a;
    } 
} 

var f = someFunc();

print(f()); // Prints 1
print(f()); // Prints 2
print(f()); // Prints 3

Note the first comment. That function being returned is a closure over the a variable.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • Uhm, I am not quite familiar with javascript, as seen on my tags it is PHP. Oh okay. – Dumb Question Oct 26 '16 at 11:53
  • @DumbQuestion Well, it's technically not Javascript. You must be semi-familiar with JS if you're able to recognize that this looks like. It. This should be general enough that knowledge of any language is sufficient. What part is tripping you up? – Carcigenicate Oct 26 '16 at 11:56
  • On your example I quite understand, however, how can I optimize my `spl_autload_register` to lessen the time of `self`? – Dumb Question Oct 26 '16 at 12:13
  • @DumbQuestion Unfortunately, that I can't answer directly as I've never used that function. If it's loading files in a tight loop though, I'd expect it to be slow if it's reading from the disk. – Carcigenicate Oct 26 '16 at 12:44
  • What do you mean by a tight loop? – Dumb Question Oct 26 '16 at 23:01
  • @DumbQuestion A loop that only executes a couple things. Say you have a loop, and the only thing inside that loop is reading a file, the file reading will execute much slower than the loop can run, so it will bottleneck. – Carcigenicate Oct 26 '16 at 23:18
  • Yeah, `spl_autoload_register()` calling all of my function files, is there any way to avoid the bottleneck? – Dumb Question Oct 26 '16 at 23:30
  • @DumbQuestion You could try parallelizing the reads by having them happen on separate threads. I'm sure PHP has some kind of abstraction for that. Right now for my Genetic Algorithm, I'm wrapping long disk writes in a `future`, which runs them potentially in parallel with the rest of the loop. Reads will be slightly harder because you actually need to get a result back, but I'm sure it could be done with some thinking. – Carcigenicate Oct 26 '16 at 23:38
  • It should be noted though that doing this has some overhead, so it would be a good idea to do some tests before going all in on this. – Carcigenicate Oct 26 '16 at 23:39
  • 1
    Thanks, this is noted. – Dumb Question Oct 26 '16 at 23:40