C
C#•3mo 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•3mo 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•3mo ago
yeah that's fair enough but between i < 5 || i > 7
Jimmacle
Jimmacle•3mo 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•3mo 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•3mo ago
i can't say that off the top of my head but the result of both will 100% be the same
Jasonnn
JasonnnOP•3mo 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•3mo ago
the decompiler is just guessing at what the source code might be
Jimmacle
Jimmacle•3mo ago
No description
Jasonnn
JasonnnOP•3mo 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•3mo 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•3mo ago
A neat advantage though is the compiler can better detect if a pattern is contradictory
Jimmacle
Jimmacle•3mo 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•3mo 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•3mo ago
yes, if a method is called enough the JIT can recompile it with more optimization
Jasonnn
JasonnnOP•3mo ago
Ah but that’s not possible « beforehand »?
Jimmacle
Jimmacle•3mo ago
i mean, maybe with AOT but that's a different can of worms
Jasonnn
JasonnnOP•3mo ago
I see that’s good though and that persists in between call to the software?
Jimmacle
Jimmacle•3mo ago
C# doesn't turn directly into assembly code
Jasonnn
JasonnnOP•3mo ago
AOT?
Jimmacle
Jimmacle•3mo 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•3mo ago
The reoptimization is at the IL level? Or at the assembly level?
Jimmacle
Jimmacle•3mo 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•3mo 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•3mo ago
the assembly code isn't stored so no
Jasonnn
JasonnnOP•3mo 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•3mo 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•3mo 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•3mo 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•3mo ago
Yep
Want results from more Discord servers?
Add your server