the obsolte[sic] comment is correct. you can do stuff like:
byte[] combined = [.. arr1, .. arr2];
byte[] reversed = [.. arr2, .. arr1];
in recent c# instead of even making that method call at all or using system.array.
i almost think obsolete was misspelled deliberately so it wouldn't cause warnings at every call site the method is used. the comment is also further unhelpful in that if you don't know about spread operator, you're going to have a hell of a time googling for [.. xxx], so it might have been better to fix the codebase rather than leave that comment in a drive-by.
To be fair, a good compiler will probably identify that the reversedOrder flag is always a constant, and generate two specialized variants of the function.
It does make the function harder to read and understand though.
Never rely on your compiler to fix stupid code when you can fix that stupid code yourself quite easily. Both because of readability, and because you can't be sure the compiler actually does optimize the mess.
Absolutely. I do think readability is the way more important point though. Unless you've actually measured the performance, optimization is kinda pointless.
Such large files in C# is something really-really rare, because you need to get out of your way to do this. C# specifically rewards separating code into namespaces by usage, and it's very easy to do, unlike some other languages, plus it's the practice adopted by language itself and developers on it, so you do it just because others do it.
...Is it bad that my first thought about how to rewrite this without duplicating everything was to split the logic out into a lambda or local function?
public static byte[] ConcatByteArrays(byte[] arr1, byte[] arr2, bool reversedOrder = false)
{
return reversedOrder ? Inner(arr2, arr1) : Inner(arr1, arr2);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static byte[] Inner(byte[] left, byte[] right)
{
byte[] ret = new byte[left.Length + right.Length];
for (int i = 0; i < left.Length; i++)
{
ret[i] = left[i];
}
int offset = left.Length;
for (int i = 0; i < right.Length); i++)
{
ret[i + offset] = right[i];
}
return ret;
}
}
84
u/__nohope Aug 23 '26
33
u/veritron Aug 23 '26
the obsolte[sic] comment is correct. you can do stuff like:
byte[] combined = [.. arr1, .. arr2];
byte[] reversed = [.. arr2, .. arr1];
in recent c# instead of even making that method call at all or using system.array.
i almost think obsolete was misspelled deliberately so it wouldn't cause warnings at every call site the method is used. the comment is also further unhelpful in that if you don't know about spread operator, you're going to have a hell of a time googling for [.. xxx], so it might have been better to fix the codebase rather than leave that comment in a drive-by.
25
u/KatAirlines Aug 23 '26
where color
47
u/TheBigGambling Aug 23 '26
System.array is your friend. 100times faster, no loops , and its one call only to copy froma a to b
6
u/Last8Exile Aug 23 '26
Not using Array.Copy is bonkers
1
u/Big-Nebula4004 Aug 24 '26
people is this C# or Java?
3
u/my_new_accoun1 Aug 24 '26
am i the only one who can't read code without syntax highlighting 😭
2
u/Loading_M_ Aug 23 '26
To be fair, a good compiler will probably identify that the
reversedOrderflag is always a constant, and generate two specialized variants of the function.It does make the function harder to read and understand though.
2
u/letmehaveanameyoudum Aug 23 '26
1
u/TechnoByte_ Aug 23 '26
Amazing syntax highlighting too
1
u/GoddammitDontShootMe [ $[ $RANDOM % 6 ] == 0 ] && rm -rf / || echo “You live” Aug 24 '26
Is that supposed to be some kind of annotation? Wouldn't it need to be spelled properly to work?
1
u/RegisteredJustToSay Aug 25 '26
People just can't help but reply with better implementations. We all know it sucks, that's why we're here. lol
1
u/conundorum Sep 06 '26
...Is it bad that my first thought about how to rewrite this without duplicating everything was to split the logic out into a lambda or local function?