C
C#•4mo ago
Jasonnn

merge into pattern

So I've read about patterns a bit I have :
if (!(i is >= 5 and <= 7))
{
inputData2[0].XB1[i] = 1.0f;
}
if (!(i is >= 5 and <= 7))
{
inputData2[0].XB1[i] = 1.0f;
}
Should I really reformat that as (my IDE tells me to, cf. screenshot)
if (i is < 5 or > 7)
{
inputData2[0].XB1[i] = 1.0f;
}
if (i is < 5 or > 7)
{
inputData2[0].XB1[i] = 1.0f;
}
why is my IDE so adament on me using them, is there really beneficial about it?
No description
29 Replies
Jimmacle
Jimmacle•4mo ago
i find the version the IDE is telling you to use much easier to read at minimum, i would de morgan your logic so you don't need to invert the result (which the second version already did for you) (https://en.wikipedia.org/wiki/De_Morgan%27s_laws if you aren't familiar)
Jasonnn
JasonnnOP•4mo ago
yeah that's fair enough but between i < 5 || i > 7
Jimmacle
Jimmacle•4mo ago
the pattern is technically less redundant since you only have to specify i once but at that point it's more personal preference imo
Jasonnn
JasonnnOP•4mo ago
I guess I was wondering the compiled code won't change right It is purely aesthetics? (which is fine i'm just curious)
Jimmacle
Jimmacle•4mo ago
i can't say that off the top of my head but the result of both will 100% be the same
Jasonnn
JasonnnOP•4mo ago
I just checked, the decompiled compiled code goes from num < 5 || num > 7 to (num < 5 || num > 7) ? true : false (when using patterns) so I guess the same lol
Jimmacle
Jimmacle•4mo ago
the decompiler is just guessing at what the source code might be
Jimmacle
Jimmacle•4mo ago
No description
Jasonnn
JasonnnOP•4mo ago
ah Yeah I don't know how to read that 😦 ah there is a xor thing in the second one weirdly enough
Jimmacle
Jimmacle•4mo ago
they are essentially the same thing, though the non-pattern one managed to JIT to a branchless solution not sure why, you'd need some brains from #allow-unsafe-blocks
TheBoxyBear
TheBoxyBear•4mo ago
A neat advantage though is the compiler can better detect if a pattern is contradictory
Jimmacle
Jimmacle•4mo ago
yeah, based on this the pattern might be negligibly slower but i think the readability is more important in 99.999% of cases
Jasonnn
JasonnnOP•4mo ago
in the sense that it would never get to True for example i is > 5 and < 2? Almost unrelated but is there a thing where you give more time to the compiler and it finds a better compiled code? Im new to compiled languages been doing Python for years so there might not be but I feel like it could make sense?
Jimmacle
Jimmacle•4mo ago
yes, if a method is called enough the JIT can recompile it with more optimization
Jasonnn
JasonnnOP•4mo ago
Ah but that’s not possible « beforehand »?
Jimmacle
Jimmacle•4mo ago
i mean, maybe with AOT but that's a different can of worms
Jasonnn
JasonnnOP•4mo ago
I see that’s good though and that persists in between call to the software?
Jimmacle
Jimmacle•4mo ago
C# doesn't turn directly into assembly code
Jasonnn
JasonnnOP•4mo ago
AOT?
Jimmacle
Jimmacle•4mo ago
when you compile it turns into IL, then when you run the program that's turned into assembly as needed which allows it to do smart things like re-optimize code that gets called a lot
Jasonnn
JasonnnOP•4mo ago
The reoptimization is at the IL level? Or at the assembly level?
Jimmacle
Jimmacle•4mo ago
no, the assembly level the IL never changes ahead of time compilation, it's a way to compile a C# program directly into assembly without the JIT or IL in the end result but it has some limitations that could be a problem depending on the C#/.NET features you use in the program it also means the assembly you compiled to is the assembly you get, it can't be progressively optimized at runtime
Jasonnn
JasonnnOP•4mo ago
I see But if I run the IL on Monday And I turn off my computer And I run it on Thursday Will the optimizations made on Monday Persist
Jimmacle
Jimmacle•4mo ago
the assembly code isn't stored so no
Jasonnn
JasonnnOP•4mo ago
Ah i see (Sorry if it’s a naive question I’m really no expert) Is there an easy way to keep the optimizations it made? Or maybe it wouldn’t even be worth it to?
Jimmacle
Jimmacle•4mo ago
it's not really needed, though afaik there are ways to pre-generate a profile that can inform the JIT what needs to be optimized more but that's already getting out of my level of expertise and would be a question for #allow-unsafe-blocks
Jasonnn
JasonnnOP•4mo ago
I see, I'll ask later because I'm still a beginner but I'll keep in mind it might be possible, especially since my team transitioned from Python to C# partially for the added benefit of faster calculations Thanks!
Jimmacle
Jimmacle•4mo ago
you'll already get plenty of speed increase from switching to C# without doing anything special, assuming you weren't just using python as glue between native libraries
Jasonnn
JasonnnOP•4mo ago
Yep
Want results from more Discord servers?
Add your server