On Nov 2, 2007, at 12:33 , mike wrote:

that adds one array iteration on the interpreted level. my general
rule of thumb is the more that is in compiled code the faster and more
efficient it will be.

I can somewhat understand what you're saying, but what you call a rule of thumb, many would call ``premature optimization.'' You're wanting to rewrite small loops between network service calls in C and add complexity to an API because you are afraid that it *might* be too slow in some of the least likely cases.

Could you at least provide more than a rule of thumb before suggesting API changes?

This is a bit off topic, but I'm pretty sure PHP supports parameter
binding.  I wouldn't trust any code that didn't use it.

For example, in your first case, are you *sure* there's no way to
execute that code with an arbitrary string?  Really?

Well, considering that I formulate the cache set/gets myself, yes. It
will always be key names controlled by myself.

I'd strongly recommend you to use bindings *before* you find out you're wrong about the control you're exercising. Someday you'll be entering data for Little Bobby Tables and everything will go terribly wrong. The price of protection is *really* low, and in many cases can make things much faster.

Actually the wrapper functions would be like this:

(modified to be my "ideal" prefix situation)

function user_get($keys) {
   list($hits, $misses) = cache_get('user:', $keys);
   if(count($misses) > 0) {
       $fetched = array();
       $q = db_query("SELECT * FROM users WHERE user_id
IN(".implode(',', $misses).")");
       while($r = db_rows_assoc($q)) {
            $fetched[] = $r;
       }
       db_free($q);
       $hits = array_merge($hits, $fetched);
   }
   return $hits;
}

I believe that is the cleanest you can get on the PHP level, and even
then it requires a couple tweaks to the memcached client.

You mean to say you have to copy and paste that entire thing for every different type of object you'll want to cache? Could you not achieve something closer to what I wrote? My function will work for all variations of cache objects without modification.

I am not sure, the multi-gets could be 1 key or 100. Or possibly if I
am pre-caching the data ahead of time, I could be sending batches of a
lot more. I want to design this in a manner where I do not care how
many keys are requested...

...but you're working really hard to optimize for a case that you haven't proven causes any issues in your application.

That is to say, if PHP is so incredibly bad at iterating a small array that you would work this hard to avoid it, why would anyone ever use it?

        Just try the easy thing, and measure.

--
Dustin Sallings


Reply via email to