Nao

  • Dadman with a boy
  • Posts: 16,082
timeformat annoys me...
« on April 3rd, 2012, 01:56 PM »
Here are a few things I'm looking into...

- Looks like passing an %e to strftime() will return " 1" in case you're on the first of a month, instead of "1". I have no idea why PHP is adding the leading space. It's okay in most cases, but in French the number comes first, so if you show the date (like this), you will get ( 1 avril) instead of (1 avril). (Well, actually it should say "(1er avril)" to be proper French, and that's actually in the Noisen codebase, but I never got around to find a *QUICK* way to do it that would also be usable by other languages, rather than just French....)

- Speaking of timeformat(), it's called every time a date is calculated on the page. Meaning it's called a lot on most pages...
I was thinking, wouldn't there be a way to simply cache *for the duration of the page load* (e.g. a static variable) as much data as we can...? Because redoing the calculations is a bit silly if you ask me. Heck, we could even store the last calculated date, determine whether it has the same characteristics, and refuse its time format...

- Generally speaking, timeformat() is a real mess to begin with. It has so much code... I'm pretty sure there are simpler ways to implement this.

- Pete, what about your changes to the admin area...? I've been postponing the moving of admin files to their own folders because you told me you had something on the backburner... (Not that it's urgent or anything, though.)

Pandos

  • Living on the edge of Wedge
  • Posts: 635
Re: timeformat annoys me...
« Reply #1, on April 3rd, 2012, 02:12 PM »
Did you try it with %-e ?
The "-" option requests no padding.
# dpkg-reconfigure brain
error: brain is not installed or configured

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: timeformat annoys me...
« Reply #2, on April 3rd, 2012, 04:23 PM »
Wouldn't work for users who already have a timeformat set up would it..?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: timeformat annoys me...
« Reply #3, on April 3rd, 2012, 04:38 PM »
What calculations, exactly, can you cache between calls for the life of the page?

In a typical page, you're calling it ~20 times, each with a different time each time. It's also potentially different for different users, further eroding the value of cache, no?
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: timeformat annoys me...
« Reply #4, on April 3rd, 2012, 06:08 PM »
Quote from Arantor on April 3rd, 2012, 04:38 PM
What calculations, exactly, can you cache between calls for the life of the page?

In a typical page, you're calling it ~20 times, each with a different time each time.
Except there are only two different time formats (or so), the regular one and the Today/Yesterday shortcut... (And the year-free version, too... Hmm.)
Quote
It's also potentially different for different users, further eroding the value of cache, no?
Life of the page, as we said. So it's once per user...
Posted: April 3rd, 2012, 06:08 PM

What do you think about the %e issue, Pete?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: timeformat annoys me...
« Reply #5, on April 3rd, 2012, 06:21 PM »
There are multiple different formats, sure, and to achieve one, timeformat is even re-called by itself to make that happen.

Now, I realise that there's a life-of-page duration, but I'm not clear what it is you can actually calculate and reuse for the life of page. I just can't think of anything right now :(

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: timeformat annoys me...
« Reply #6, on April 3rd, 2012, 09:22 PM »
What I was thinking was calculating the minimum date to have a full date, not a shortcut. Then the minimal date to have a year added. That's two milestones. We could cache date formats if a date is found to be in that threshold.

Maybe I'm wrong though...

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: timeformat annoys me...
« Reply #7, on April 3rd, 2012, 09:31 PM »
Hmm, I suppose you could do that.

The thing is timeformat does get called a lot but at the same time, it's still not the most expensive of the function calls, in the scheme of things, parse_bbc is of course the monster, but even ignoring that, timeformat is quite a way done the list in terms of time used.

But it is a reasonably low hanging fruit to optimise, so if you want to tackle it, by all means :)

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: timeformat annoys me...
« Reply #8, on April 3rd, 2012, 10:45 PM »
I couldn't do much to optimize, in the end... Until I found out that non-English installs may benefit from just skipping a replacement loop that makes the time string go through multiple strftime calls :)

Made 10.000 loops and the new version executed in .12s in average, versus .3s for the 'full' version.
Of course it's VERY fast in both cases... You're right, I probably wasted my time lol.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: timeformat annoys me...
« Reply #9, on April 3rd, 2012, 11:03 PM »
It's another case of Amdahl's Law, but ultimately it's every case where performance can be improved, it's worth doing, provided that it doesn't prevent other optimisations being possible.

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: timeformat annoys me...
« Reply #10, on April 4th, 2012, 10:57 AM »
Yup.
BTW the SVN version is buggy (it no longer supports Today/Yesterday), it's just a matter of one line to move down, I fixed it here yesterday but haven't yet got around to committing it...
Re: timeformat annoys me...
« Reply #11, on April 5th, 2012, 10:27 AM »
Quote from Pandos on April 3rd, 2012, 02:12 PM
Did you try it with %-e ?
The "-" option requests no padding.
I'm having trouble with %-e.
I updated the documentation to "blindly" say to use %-e instead of %e, and to use it by default at install time as well...
However, I can't for the life of me find any frigging documentation about this 'hidden feature' of strftime().
This is annoying to me because for what it's worth, it could very well be something that only works on Apache 2, or Windows, or whatever.

Right now I'm considering returning a trimmed string instead of doing it at strftime().
I don't think that trim() is a very expensive function, and it should solve problems when using parenthesis around the date.
What do you think, guys..?

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: timeformat annoys me...
« Reply #12, on April 5th, 2012, 10:44 AM »
%e doesn't work om Windows anyway, I doubt %-e would either, and I've never heard of - to strip padding.

trim isn't expensive, and AFAIK it isn't that expensive if you substitute another character, in this case I'm thinking ltrim() on %d rather than %e and pass in '0' as the second parameter so it left-trims leading zeros. It's likely to be as fast as using trim() would be with the bonus that it'll work consistently on all platforms, something that doesn't occur with %e (there are people out there who do use Windows hosting in production!)

Nao

  • Dadman with a boy
  • Posts: 16,082
Re: timeformat annoys me...
« Reply #13, on April 5th, 2012, 01:05 PM »
First sentence is okay but I'm afraid I didn't catch the rest.
How are you going to trim leading zeros on %d if it's in the middle of a string anyway...? Might as well use the trimmed version to begin with. SMF/Wedge already replace %e on the fly with %#d IIRC, if Windows is found.

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278