Welcome Guest, Not a member yet? Register   Sign In
A possible code rewrite for the Loader load library method
#21

[eluser]Jaketoolson[/eluser]
You need to fork this @ github so I can better track changes... do you mind doing so? I liked the commentary.
#22

[eluser]Basketcasesoftware[/eluser]
Sure. As soon as I take the time to learn how to do the fork. Smile I'm very new to GitHub. But it would be a lot better than trying to upload it as text file!
#23

[eluser]Basketcasesoftware[/eluser]
I'm not using GitHub but BitBucket instead which is what CodeIgniter uses. I've done a fork off of the CI Reactor project there. Already had an account anyways after I submitted a document bug for the User Guide. Downloaded and installed Tortoise and getting going. I'll hopefully have the fork finished by the end of this weekend.
#24

[eluser]InsiteFX[/eluser]
Try to replace the while loops those are the bottle neck!

InsiteFX
#25

[eluser]Basketcasesoftware[/eluser]
[quote author="InsiteFX" date="1298117066"]Try to replace the while loops those are the bottle neck!

InsiteFX[/quote]
Ok. Thanks for that input. I really didn't care for them either. Any specific suggestions? Right now I'm partial to foreach loops but I'm looking for a faster alternative. The Map_Array function is nice, but there was too much overhead setting up the arguments in my previous version of this.
#26

[eluser]Basketcasesoftware[/eluser]
Here's my alternative (this is just one instance of this kind of looping)
Code:
// First break the set of names for the library objects into subsets that map one-to-
// one with the list of library items.
$object_name=array_chunk($object_name,count($library));
// The last subset is possible shorter in size that our our library list so we'll pad
// the rest of it with NULLs.
$last=count($object_name)-1;    // Just a handy pointer to the last subset.
// Pad out the last subset with enough NULLs, if any, to make it match the size of the
// library array.
$object_name[$last]=array_pad($object_name[$last],count($library),NULL);
                        
// We iterate through each of the subsets.
foreach($object_name as $chunk)
{    // Start of the subset loop.
    // Make sure we start with the beginning of the library array for the inner loop.
    $reset($library);
                    
    // We iterate through each member of the current subset.
    foreach($chunk as $name)
    {    // Start of the name loop.
        // It would be nice to do something like foreach($library as $lib,$chunk as
        // $name)... but we can't. Bummer! Just have to step through the library names
        // inside the loop.
        $lib=$next($library);
                                    
        // Pass the current library item with the parameters and the name to the worker
        // function.
        $this->_ci_load_class($lib,$params,$name);
    }    // End of the name loop.
}    // End of the subset loop.

Oh, the code the above replaces went like this:
Code:
// Make sure we start with the beginning of the library array for the first loop.
reset($library);

// Iterate through the array of names.                
foreach($object_name as $name)
{    // Start of the name loop.

    // Get the next library member. If we've reached end of the list reset to the
    // beggining.
    $lib=(current($library))?next($library):reset($library);
                                
    // Pass the current library item with the parameters and the name to the worker
    // function.
    $this->_ci_load_class($lib,$params,$name);
}    // End of the name loop.
                        
// Have we exhausted all of the library members?
if(current($library))
{    // No? Loop through the remaining library members.
    while($lib=next($library))
    {    // Beginning of library loop.
        // Pass the current library item with the parameters only to the worker
        // function.
        this->_ci_load_class($lib,$params,NULL);
    }    // End of the library loop.
}    // End of library exhaustion decision.
#27

[eluser]Basketcasesoftware[/eluser]
I'm not thrilled with my previous optimization and I realized a potential feature bug shortly after I made the posting. Just now have the time to make the update and corrections.
Code:
// First pad the object_name array out with NULLs if it is smaller than the array of library objects to make it the same size.
$object_name=array_pad($object_name,count($library),NULL);
// If the two arrays are the same size our size we only have to worry about one loop.
if(count($library)==count($object_name))
{   // Start of the true conditional block for equal size arrays.
    // Make sure when we step through the list of names that we start at the beginning.
    reset($object_name);

    // Iterate through the list of library items.
    foreach($library as $lib)
    {   // Start of our library item loop.
        // Fetch the next object name.
        $name=next($object_name);

        // Pass the current library item, parameters, and name to our worker method.
        $this->_ci_load_class($lib,$params,$name);
    }   // End of our library item loop.

    return;    // We're done here. Get the heck out of here.
}   // End of the true conditional block for equal size arrays.

// Well, if we've reached this point the array of names is larger than our array of
// library objects. We know this because we've already made sure that it can't be smaller
// and we've already eliminated the possibility it's of equal size. So we do some more
// pre-processing with it so it consistently maps to our library object array.
// First break the set of names for the library objects into subsets that map one-to-
// one with the list of library items.
$object_name=array_chunk($object_name,count($library));
                        
// We iterate through each of the subsets.
foreach($object_name as $chunk)
{   // Start of the subset loop.
    // Make sure we start with the beginning of the library array for the inner loop.
    $reset($library);
                    
    // We iterate through each member of the current subset.
    foreach($chunk as $name)
    {    // Start of the name loop.
        // It would be nice to do something like foreach($library as $lib,$chunk as
        // $name)... but we can't. Bummer! Just have to step through the library names
        // inside the loop.
        $lib=$next($library);
                                    
        // Pass the current library item with the parameters and the name to the worker
        // function.
        $this->_ci_load_class($lib,$params,$name);
    }    // End of the name loop.
}    // End of the subset loop.
#28

[eluser]Basketcasesoftware[/eluser]
Ok. I have a Reactor fork over at BitBucket.

I guess this is the correct URL:
https://bitbucket.org/Basketcasesoftware...r/overview
#29

[eluser]InsiteFX[/eluser]
Hey Basketcase,

I was up most of the night and did some researching on the while loops etc.
It doe's not make that much of a speed difference so don't worry about it.

For loops can slow you down but just make sure that that the count variable
is initalizied before it it used in the for loop big speed difference!

I bet you are wondering how I got the software box in the basket LOL!

InsiteFX
#30

[eluser]Basketcasesoftware[/eluser]
That question did occur to me. Smile

And I went ahead and did those rewrites anyways. I think I got the speed on par with the original method. I probably get most of my overhead from all the comments. :-P

Are you going from my code fork? I did other optimizations with regards to the conditionals. Got rid of all the else statements and minimized the function calls for the conditionals.

I need to find a decent way to automate the profiling of the code.

Overall, what did you think of the effort?




Theme © iAndrew 2016 - Forum software by © MyBB