Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ Core » DarkThemeCached
DarkThemeCached [message #57072] Thu, 20 May 2021 23:11 Go to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hello,

Looking at Pradip's code I saw this DarkThemeCached()
What is it supposed to do ?

Especially this line:
cache.ii = (cache.ii + 1) & (N - 1);
Re: DarkThemeCached [message #57073 is a reply to message #57072] Thu, 20 May 2021 23:44 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Hello Didier,


That line is supposed to return the remainder of (cache.ii + 1) / N operation, namely (cache.ii + 1) % N (=8) Its function is to clamp the index between 0-7.
A known microptimization trick, if the ii is a positive integer and the N is a power of 2.

(Apparently, colors are cached in thread_local slots))


Best regards,
Oblivion


[Updated on: Fri, 21 May 2021 00:44]

Report message to a moderator

Re: DarkThemeCached [message #57074 is a reply to message #57073] Fri, 21 May 2021 01:34 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Ok for the line, I did'n know this trick.

But since there are only 8 slots .... it seems to me there is a risk of overlap letting this beeing used freely ?
I still don't uderstand how this function can be used ?

I am really curius
Re: DarkThemeCached [message #57075 is a reply to message #57074] Fri, 21 May 2021 01:52 Go to previous messageGo to next message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
To my understanding,

It keeps a little map (icolors = input colors, ocolors = output colors, total: 8 colors);

When you call DarkThemeCached(c)

1) It looks up c in icolors.(keys)
2) If it finds it, it returns the corresponding alternate (dark) theme color, ocolor (values).
3) If it couldn't find it in the keys, then calls DarkTheme(c) to calculate the dark theme color (ocolor). (relatively expensive)
4) Then it puts the new dark theme color variant into the map value and returns it.

Now, probably the confusing part is the index: "cache" structure seems to keep track of the current position, so that it wouldn't overwrite the last cached and used color unless it is necessary. That line with modulo operation is apparently for this purpose.
It will start over from index position 0 only if it is iterated enough ( >= 8 ), i, e. previous lookups failed to find their respective color 'c' value among the keys.

Keep in mind that this is what I see in this piece of code. Not the official explanation. Smile

Best regards,
Oblivion



[Updated on: Fri, 21 May 2021 02:06]

Report message to a moderator

Re: DarkThemeCached [message #57078 is a reply to message #57075] Fri, 21 May 2021 09:21 Go to previous messageGo to next message
Didier is currently offline  Didier
Messages: 680
Registered: November 2008
Location: France
Contributor
Hello Oblivion,

Thanks for the explanations.

If you are using many colors, I don't think using this is a good idea : the cache will be overlapping all the time
Re: DarkThemeCached [message #57121 is a reply to message #57078] Thu, 27 May 2021 09:21 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Didier wrote on Fri, 21 May 2021 09:21
Hello Oblivion,

Thanks for the explanations.

If you are using many colors, I don't think using this is a good idea : the cache will be overlapping all the time


That is why this is separate function from DarkTheme(Color)... You are supposed to use it only where there is a chance of speedup. Actually, the only proper use in the whole uppsrc is in the RichText, where it is used to convert the text color. Usually you do not have that many text colors...

All that said, it is entirely possible that this could be improved somehow. But I just wanted tiny simple cache for RichText...

Mirek
Re: DarkThemeCached [message #57122 is a reply to message #57121] Thu, 27 May 2021 09:59 Go to previous message
Oblivion is currently offline  Oblivion
Messages: 1091
Registered: August 2007
Senior Contributor
Well it is also a perfect fit for situations where the app/widget mainly uses the standard 8 ANSI colors and their brighter counterparts, such as TerminalCtrl. Smile
For such cases this function seems like an an optimal solution.

Best regards,
Oblivion


Previous Topic: Deprecating THISBACK
Next Topic: Small optimization
Goto Forum:
  


Current Time: Thu Mar 28 16:04:25 CET 2024

Total time taken to generate the page: 0.00988 seconds