Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Repeat calls vs storage and reuse
Before I take to Stack Overflow and get flamed for asking the wrong type of question, I’m after some opinions. This is specifically related to the SLIR image library (but is generally applicable elsewhere). The library is littered with getter/setter methods, which are considered evil and I’ve learned a lot today about some of the bad “OO” (*cough*, half procedural) habits in Txp that we should ideally start to stamp out in Txp 5.
But sidestepping the get/set thing, a general principle of coding efficiency is that if you find yourself doing the same thing over and over, it’s better to do it once and refer to it. That is:
$this->info['width'] = imagesx($this->getImage());
$this->info['height'] = imagesy($this->getImage());
$this->info['mime'] = image_type_to_mime_type(exif_imagetype($this->getImage()));
...
could be rewritten as:
$img = $this->getImage();
$this->info['width'] = imagesx($img);
$this->info['height'] = imagesy($img);
$this->info['mime'] = image_type_to_mime_type(exif_imagetype($img));
...
One might think that the second version is better. Fewer (e.g.) JMP statements at the machine code level, no need to repeatedly maintain state of variables prior to branching. Fewer actions to continually pop on and off the return stack. The list goes on, especially taking into account that (in 2020 at least) different types of calls have different costs.
But in this case, what’s returned from getImage() isn’t a simple int or string. It’s an image; a fully-formed instance of a JPG or WEBP or whatever. So the object already exists in memory. It contains the image inside it, in its local variable, and is consuming RAM. If we ask for a local copy, we (the interpreter) presumably needs to create space for it in memory to assign anopther copy to the $img variable.
If the image is an icon, that might be less than 1KB. But it could be a 3, 4, 10, … MB picture, so storing a local variable of this size would (?) make the code slower and more memory-hungry than simply calling the same method multiple times to get the same object.
Now I don’t know how good the PHP interpreter is – the vanilla intepreter, before factoring OPCaches and stuff in. Maybe it will not actually allocate an entirely new lump of RAM to store the $img in. Maybe it’ll just store a pointer internally to the image inside the object and monitor if it changes and only then (when an evil setter() method pollutes the object) make a copy. Maybe it’ll notice that we’re calling the same thing multiple times and do some clever cacheing to avoid pushing/popping stuff on and off the stack.
Does it cache the response at all? Ultimately, it’s still got to return the image itself to the calling envrionment in order for it to be passed on to the other functions that use it, so does that create an internal copy each time anyway? i.e., in addition to continually pushing/popping return values on the stack, does it continually have to allocate and release N megabytes of RAM each time? If so, the overhead of storing it once would be worth taking.
I know there are code profilers, but they’re usually a faff to set up. And as asn aside, it might be cool if we could consider having such an environment available to us at the project level – even if we only routinely run some standard Unit Tests and see how performant they are.
I guess what I’m asking: is there a way to know, or figure out at coding time (or even runtime), if it’s likely to be more efficient to use multiple calls vs stash-and-reuse?
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline
Re: Repeat calls vs storage and reuse
Bloke wrote #342785:
Before I take to Stack Overflow and get flamed for asking the wrong type of question, I’m after some opinions.
I’ll leave this here: medium.com/google-cloud/stackoverflow-trends-2026-the-structural-shift-from-human-support-to-generative-ai-b921930ff29d
There will be far few active members to flame you, at least!
Offline
Re: Repeat calls vs storage and reuse
Ha yes, and ChatGPT is, somewhat ironically, trained on SO data, but often without necessary context as I’ve mentioned in passing ref Security there. The world actually needs those flamers.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Hire Txp Builders – finely-crafted code, design and Txp
Offline