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 » Community » U++ community news and announcements » 2024rc1
Re: 2024rc1 [message #61033 is a reply to message #61032] Mon, 21 October 2024 09:05 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Lance wrote on Mon, 21 October 2024 02:27
with the u++ moving to c++17,

code like this (Core/Vcont.h line 13)
	void Malloc(size_t size) {
		if(std::is_trivially_destructible<T>::value)
			ptr = (T *)MemoryAlloc(size * sizeof(T));
		else {
			void *p = MemoryAlloc(size * sizeof(T) + 16);
			*(size_t *)p = size;
			ptr = (T *)((byte *)p + 16);
		}
	}

can benefit from constexpr-if compile time trimming to produce more compact and faster binary (theoretically).


How? I have noticed that some people tend to constexpr to everything, but I fail to see a reason. If that is supposed to perform the test only in compile time, then 30 years old compiler will do that anyway. But I might be missing something perhaps?
Re: 2024rc1 [message #61034 is a reply to message #61033] Mon, 21 October 2024 12:55 Go to previous messageGo to next message
Lance is currently offline  Lance
Messages: 656
Registered: March 2007
Contributor
mirek wrote on Mon, 21 October 2024 03:05

How? I have noticed that some people tend to constexpr to everything, but I fail to see a reason. If that is supposed to perform the test only in compile time, then 30 years old compiler will do that anyway. But I might be missing something perhaps?


On a second thought, you are right. A reasonably good compiler would perform the optimization anyways. I remeber a couple years ago when I dig into u++ memory allocation utilities, I saw this one
template <class T>
void memcpy_t(void *t, const T *s, size_t count)
{
	if((sizeof(T) & 15) == 0)
		memcpy128(t, s, count * (sizeof(T) >> 4));
	else
	if((sizeof(T) & 7) == 0)
		memcpy64(t, s, count * (sizeof(T) >> 3));
	else
	if((sizeof(T) & 3) == 0)
		memcpy32(t, s, count * (sizeof(T) >> 2));
	else
	if((sizeof(T) & 1) == 0)
		memcpy16(t, s, count * (sizeof(T) >> 1));
	else
		memcpy8(t, s, count * sizeof(T));
}


Now I know you already counted on the compile time optimization.

Then the question becomes: what constexpr-if has to offer? Maybe speak a programmer's intention more explicitly and thus possible compiler check, like override?

Re: 2024rc1 [message #61035 is a reply to message #61034] Mon, 21 October 2024 13:01 Go to previous messageGo to next message
Lance is currently offline  Lance
Messages: 656
Registered: March 2007
Contributor
I take back the hint about code size and efficiency difference. But personally I would use if constexpr () in this and similar cases.

Quote:

For optimization purposes, modern compilers will generally treat non-constexpr if-statements that have constexpr conditionals as if they were constexpr-if-statements. However, they are not required to do so.

A compiler that encounters a non-constexpr if-statement with a constexpr conditional may issue a warning advising you to use if constexpr instead. This will ensure that compile-time evaluation will occur (even if optimizations are disabled).
Re: 2024rc1 [message #61038 is a reply to message #61035] Mon, 21 October 2024 13:21 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Lance wrote on Mon, 21 October 2024 13:01
I take back the hint about code size and efficiency difference. But personally I would use if constexpr () in this and similar cases.

Quote:

For optimization purposes, modern compilers will generally treat non-constexpr if-statements that have constexpr conditionals as if they were constexpr-if-statements. However, they are not required to do so.

A compiler that encounters a non-constexpr if-statement with a constexpr conditional may issue a warning advising you to use if constexpr instead. This will ensure that compile-time evaluation will occur (even if optimizations are disabled).


IDK. I am afraid of another mass purge of all U++ sources adding constexpr everywhere for no good reason... Not in this release to be sure.
Re: 2024rc1 [message #61039 is a reply to message #61035] Mon, 21 October 2024 14:45 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 14261
Registered: November 2005
Ultimate Member
Lance wrote on Mon, 21 October 2024 13:01


A compiler that encounters a non-constexpr if-statement with a constexpr conditional may issue a warning advising you to use if constexpr instead. This will ensure that compile-time evaluation will occur (even if optimizations are disabled).


Well, thinking about it, I guess actually the real benefit for me would be something else: compiler issues an error if the expression you marked constexpr is not...
Re: 2024rc1 [message #61041 is a reply to message #61039] Mon, 21 October 2024 18:12 Go to previous message
Lance is currently offline  Lance
Messages: 656
Registered: March 2007
Contributor
mirek wrote on Mon, 21 October 2024 08:45
Lance wrote on Mon, 21 October 2024 13:01


A compiler that encounters a non-constexpr if-statement with a constexpr conditional may issue a warning advising you to use if constexpr instead. This will ensure that compile-time evaluation will occur (even if optimizations are disabled).


Well, thinking about it, I guess actually the real benefit for me would be something else: compiler issues an error if the expression you marked constexpr is not...


Agreed. Like "override", make a programmer's intention more explicit, and do, more important than but similar, compiler check when it doesn't going as claimed by the programmer.

Reminds me of a related case, where constexpr seems to be helpful or possibly necessary.

union Flags{
	int32 dummy;
	struct{
		byte	borderLeft  :3;
		byte	borderRight :3;
		byte	borderTop   :3;
		byte	borderBottom:3;
		byte	halign      :2;
		byte	valign      :2; //16th bit
	
		bool	faceNotNull     :1;
		bool	boldNotNull     :1;
		bool	heightNotNull   :1;
		bool	widthNotNull    :1;
		bool	underlineNotNull:1;
		bool	italicNotNull   :1;
		bool	strikeoutNotNull:1; //23rd bit
	};
	
	constexpr Flags() : dummy(0){ static_assert(sizeof(*this)==sizeof(dummy)); }
	
	
	static constexpr int32 FontMask()
	{
		Flags f;
		f.faceNotNull = true;
		f.boldNotNull = true;
		f.heightNotNull = true;
		f.widthNotNull = true;
		f.underlineNotNull = true;
		f.italicNotNull = true;
		f.strikeoutNotNull = true;
		return f.dummy;
	}
};


It's a somewhat contrived example. I am not sure ,for int32 FontMask(), if I don't it constexpr, will the code be compiled same as if I do. It's totally possible they do with todays smart and agressive as crazy compiler optimization.
Previous Topic: Win32 openssl updated to 3.2.1 version
Next Topic: SetRect "MegaRect" support...
Goto Forum:
  


Current Time: Wed Jun 11 08:19:35 CEST 2025

Total time taken to generate the page: 0.04527 seconds