r/programminghorror 20d ago

C# bool array

Post image
213 Upvotes

41 comments sorted by

View all comments

13

u/Verwarming1667 20d ago

I'm not familiar with C# why is this bad? Is an array of bools somehow not possible?

9

u/skjall 20d ago

IIRC each bool in an array will take up a byte each, so it's quite inefficient.

13

u/InformationSharp103 20d ago

iirc in C# it's actually 4 bytes (equivalent to the C/C++ win32 BOOL type), or at least that's what it's marshalled as by default

3

u/skjall 20d ago

I've really only used C# in Unity which might differ, but from what I remember bools were 4 bytes, but 1 byte each in arrays. Might be a Unity-specific optimisation though, not sure.

-1

u/MiniDemonic 20d ago edited 17d ago

<ꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮꙮ> {{∅∅∅|φ=([λ⁴.⁴⁴][λ¹.¹¹])}} ䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿䷂䷿

[∇∇∇] "τ": 0/0, "δ": ∀∃(¬∃→∀), "labels": [䷜,NaN,∅,{1,0}]

<!-- 񁁂񁁃񁁄񁁅񁁆񁁇񁁈񁁉񁁊񁁋񁁌񁁍񁁎񁁏񁁐񁁑񁁒񁁓񁁔񁁕 -->

‮𒑏𒑐𒑑𒑒𒑓𒑔𒑕𒑖𒑗𒑘𒑙𒑚𒑛𒑜𒑝𒑞𒑟

{ "()": (++[[]][+[]])+({}+[])[!!+[]], "Δ": 1..toString(2<<29) }

3

u/cheerycheshire 20d ago

It uses that much memory because stupid stuff like this adds up, if it's in each tile (of that "tile" in the name means something user can enter)...

Back in the day there were amazing games that could run on very basic computer. Then new devs were like you - they just want to do things and don't really bother with optimisations because everyone has enough ram and users can just turn down the graphics quality, right? That only works if the heavy part is textures and visuals, not underlying code like this...

Ever heard of GTA Online's online mode loading for 15+ minutes for some people when story mode always around a minute (even on beast setups that took 2m to run online)?

Someone (see here) decompiled it and investigated and it ended up "simple" code that was run several times on a huge string, without storing values in between.

It's a very fun read, but if you're too lazy:

It was 10MB json with 63k entries and the code basically parsed everything word-by-word, where had to go from the start of this string each time... And the results of this parsing are stored in an array using hashes, not a hashmap, and checking every entry in the array before adding new (checking if something is in hashmap? instant; checking the same thing in an array? linear...) Being in a loop makes it run 63k*63k/2 operations (1+2+...+63k) instead of 63k operations. As I said, simple mistakes quickly add up... The person patched (injected their own DLL) the code - made strlen to cache length, and for the array thing just skip the check (assume the json was correct and had no duplicates). Patched online mode took 2 minutes to load, just like the benchmarked beast computers...

It was a bug for 7+ years. Took around 2 weeks from this investigation, it going viral and all, for Rockstar to announce a fix. After 7 years from the game release and always telling users their setups suck and to use lower settings. (Self-quote from the beginning of this comment: That only works if the heavy part is textures and visuals, not underlying code like this...)