Geek turned Fire Fighter
So, I am sitting here today hacking away and my wife says “The neighbor’s house is on fire.” I go out back to see what is up and its clear its not the house. The fire is beyond the trees, 4 houses down. Just past that house is a wooded area where no homes have been built yet. I look out behind the houses and see smoke in the brush behind the houses.
I should explain at this point that our house has a railroad that runs behind it. Separating us from the railroad is a wooded area about 30 feet deep. Three houses down, that wooded area is on fire. The flames are small there for now and there are people out there with extinguishers.
We are thinking at this point that someone was burning leaves or wood (common in emerging areas, aka “out in the country”). We live in a newly developed area that not too long ago was considered very rural. So, its no uncommon for people to have a burn pile.
But, then we see smoke in the other direction, two houses down, on the tracks. I stomp through the brush to investigate. Sure enough, there are flames on the track down there. Now I see what has happened. The train that came through a while ago must have leaked/dropped something out that was either on fire already or highly flamable. So, my wife brings me our extinguisher from the house and I put out that fire that is getting no attention.
The reason it is getting no attention is because they are trying to save the house 4 houses down that borders the heavily wooded area. Its still burning good.
Suddenly the wind shifts. The small fires that were 3 houses down now spread quickly through the dry, dead grass that is behind all the houses. The fire is now in my neighbor’s back yard. His dog is in its pen. He keeps it in a small, tall pen in his larger fenced in yard during the day to keep him from getting out and roaming. The dog is freaking out. I jump the fence and let him out of the pen. My wife gets our leash and takes him over to our deck.
Seeing that the fire trucks are all still working on the dense woods 4 houses down, I start grabbing my hoses from my house. I have several 50ft hoses for watering my yard. By this point, my retired neighbor whose house is in front of the small fire I put out earlier has come down and he starts helping me get the hose through the yard and out to the fire.
The volunteer fire fighters priorities keep changing as things flare here and there. I decide to be proactive and jump the fence and do what I can with my little hose. Luckily, my neighbor has cleared out behind his property. We are just fighting dead grass fires instead of burning brush. That makes it possible for me to put it out with a garden hose with a spray nozzle. I soaked the area behind the two houses next to me for about an hour. Small flames kept popping up, but it was nothing dangerous. We had to keep an eye out on the other side of the tracks. These wooden cross ties would be smoldering and would catch the grass on the other side of the tracks on fire from time to time. There was a lot more grass on that side of the tracks. A fire over there would be bad as I don’t think they have gotten hydrants into that new sub division yet.
One of the fire fighters told me that the volunteer fire fighters meeting is Thursday at 7. Since I don’t know when our small area will have the resources for a full time fire department, I am considering going. I am working at home most days and I don’t think dealnews would mind me going to fight a fire from time to time.
I took some photos of the aftermath. It was so surreal that neither I nor my wife thought to take pictures during the event. There is a description of what you are looking at on each photo, just click it.
Big arrays in PHP
Update: Terry Chay has answered my question about why this is happening. In a nutshell, PHP is using 33,160 opcodes and 33,157 regsters for the verbose code. In comparison, the serialized array only uses 5 opcodes and 2 registers. He used something called VLD, which I had not heard of, to figure all this out.
So, at dealnews, we have a category tree. To make life easy, we dump it to an array in a file that we can include on any page. It has 420 entries. Expanded, one entry may look like:
$CATEGORIES[202]['id'] = “202″; $CATEGORIES[202]['name'] = “clothing & accessories”; $CATEGORIES[202]['parent'] = “0″; $CATEGORIES[202]['standalone'] = “”; $CATEGORIES[202]['description'] = “clothing”; $CATEGORIES[202]['precedence'] = “0″; $CATEGORIES[202]['preferred'] = “0″; $CATEGORIES[202]['searchable'] = “1″; $CATEGORIES[202]['product'] = “1″; $CATEGORIES[202]['aliased_id'] = “0″; $CATEGORIES[202]['path'] = “clothing & accessories”; $CATEGORIES[202]['url_safe_name'] = “clothing-accessories”; $CATEGORIES[202]['child_count'] = “6″; $CATEGORIES[202]['childlist'][0] = 202; $CATEGORIES[202]['childlist'][1] = 2; $CATEGORIES[202]['childlist'][2] = 275; $CATEGORIES[202]['childlist'][3] = 4; $CATEGORIES[202]['childlist'][4] = 481; $CATEGORIES[202]['childlist'][5] = 446; $CATEGORIES[202]['childlist'][6] = 454; $CATEGORIES[202]['childlist'][7] = 436; $CATEGORIES[202]['childlist'][8] = 205; $CATEGORIES[202]['childlist'][9] = 227; $CATEGORIES[202]['childlist'][10] = 203; $CATEGORIES[202]['childlist'][11] = 280; $CATEGORIES[202]['childlist'][12] = 204; $CATEGORIES[202]['children'][2] = &$CATEGORIES[2]; $CATEGORIES[202]['children'][275] = &$CATEGORIES[275]; $CATEGORIES[202]['children'][4] = &$CATEGORIES[4]; $CATEGORIES[202]['children'][481] = &$CATEGORIES[481]; $CATEGORIES[202]['children'][446] = &$CATEGORIES[446]; $CATEGORIES[202]['children'][454] = &$CATEGORIES[454]; $CATEGORIES[202]['children'][436] = &$CATEGORIES[436]; $CATEGORIES[202]['children'][205] = &$CATEGORIES[205]; $CATEGORIES[202]['children'][227] = &$CATEGORIES[227]; $CATEGORIES[202]['children'][203] = &$CATEGORIES[203]; $CATEGORIES[202]['children'][280] = &$CATEGORIES[280]; $CATEGORIES[202]['children'][204] = &$CATEGORIES[204];
So, I was curious how efficient this was. I noticed that some code that was using this array was jumping in memory usage as soon as I ran the script. So, I devised a little piece of code:
<?phpecho "Memory used: ".number_format(memory_get_usage())." bytesnn"; include_once "./cat_code.php"; echo "type: ".gettype($CATEGORIES)."n"; echo "count: ".count($CATEGORIES)."nn"; echo "Memory used: ".number_format(memory_get_usage())." bytesnn"; ?>
The output was very surprising:
Memory used: 41,772 bytes
type: array count: 420 Memory used: 4,951,248 bytes
Um, whoa. 5MB of memory just for including this file. The file itself is just 326k. Needless to say, that is bad. We include that file quite liberally. I decided to see if other methods of storing that would be better. First I tried var_export format.
Memory used: 41,784 bytes
type: array count: 420 Memory used: 1,212,076 bytes
Well, that is much better. But, it took some fiddling to get it right. var_export does not export reference notation. The children arrays were being fully expanded and not made references. Without the references, the code was using 8MB of memory. That was much worse. Also, this is not really all that readable like the raw code version. If we can’t read it, it may as well be serialized. So, I tried it serialized.
Memory used: 41,764 bytes
type: array count: 420 Memory used: 907,668 bytes
That is by far the best result. FWIW, timing the code showed that the var_export format was fastest and serializing was slowest. However, it was just .04 seconds faster (.047 vs. .089) including the PHP start up time. I will take that for the memory savings and ease of creation.
I am going to pose a question about this on the internals list and see if this is expected behavior or if it is a shocker to them as well.