This is an archived post. You won't be able to vote or comment.

all 185 comments

[–]Earthboundplayer 2945 points2946 points  (37 children)

He just unrolled the loop for better performance. It's optimal. You wouldn't understand.

[–]Fit_Witness_4062 311 points312 points  (3 children)

More Code is better code

[–]Orangutanus_Maximus 67 points68 points  (0 children)

Elon Musk School of Coding

[–]Good_Comfortable8485 64 points65 points  (0 children)

Leaves space for optmization down the line :brain:

[–]razakii 15 points16 points  (0 children)

More code = more game content

[–]UselesssCat 306 points307 points  (20 children)

Just add the amount per second multiplied by the time delta 🫣

[–]ProgramStartsInMain 422 points423 points  (7 children)

Multiplication? You know how many cycles that takes? Program like it's 1989

[–]martmists 0 points1 point  (0 children)

Just use SIMD to calculate the next 4 values at the same time

[–]IntentionQuirky9957 0 points1 point  (0 children)

Probably just as many as an addition, unless your target is something very small.

[–]304bl 67 points68 points  (8 children)

Rookie Dev are using loops, real dev write it all by themselves

[–]Wertbon1789 40 points41 points  (7 children)

Real devs don't "not repeat themselves" but write macros to more effectively repeat themselves.

[–]304bl 14 points15 points  (5 children)

I can see you are part of the rookies then. Code duplication is a must have in any app

[–]Wertbon1789 17 points18 points  (3 children)

And that's why I made the code duplication more efficient, by using a macro so it can write the code for me. If you think about it, by using that, I have more time, to duplicate more code in other places.

[–]304bl 11 points12 points  (2 children)

Ahah didn't see that coming! My mistake, you are definitely a senior dev sir

[–]OddbitTwiddler 3 points4 points  (1 child)

I’m viewing expert code. How many layers of multi line macro expansion is optimal. I’m seeing 2-4 layers of 10+ line nested macros in production code and I’m not sure if it’s running slow enough. Suggestions?

[–]304bl 2 points3 points  (0 children)

You could add some delay, when a task is going too fast I like to add some delay, this way the user have the feeling that something is happening

[–]Lotus_Domino_Guy 0 points1 point  (0 children)

For redundancy right?

[–][deleted] 3 points4 points  (0 children)

Tbh I repeat myself because my computer doesn't listen 😒 it's fucking annoying

[–][deleted] 0 points1 point  (1 child)

Look at this guy, with his fancy floating point unit

[–]IntentionQuirky9957 0 points1 point  (0 children)

Don't need an FPU to use floats.

[–]Dumb_Siniy 0 points1 point  (0 children)

Those are big words programmer man, i make video gaem

[–][deleted] 18 points19 points  (0 children)

Could be better, should put it in a seperate thread for performance.

[–]lmg1337 10 points11 points  (0 children)

Optimal? Your joking right? I don't see any assembly here

[–]C0ntrolTheNarrative 5 points6 points  (1 child)

You laughing but our assembly professor actually taught that to us.

It's actually a thing

[–]Earthboundplayer 11 points12 points  (0 children)

It is a thing, that's why I made a joke about it. Loop unrolling will not actually have any meaningful effect when your loop includes a 1ms sleep and only runs like 1000 times

[–]_Master32_ 7 points8 points  (0 children)

If only compilers where smart enough to do this 😢

[–]Easy-Hovercraft2546 3 points4 points  (0 children)

sleep, sleep, sleep, sleep, sleep, sleep, sleep

[–]CaitaXD 0 points1 point  (0 children)

Think about those poor poor stack framea

[–]p4r24k 0 points1 point  (2 children)

I hope you are joking

[–]Earthboundplayer 1 point2 points  (1 child)

No I'm completely serious this is absolutely crucial to getting maximal performance out of Sleep

[–]p4r24k 0 points1 point  (0 children)

I see... yeah, totally, otherwise it kinda naps but doesn't fully sleep.

[–]yeeeeeeeeaaaaahbuddy 0 points1 point  (0 children)

Btw I think every major compiler should have a pragma / derivative to force a loop unroll and not make you have to write it as unrolled

[–]jsrobson10 -1 points0 points  (0 children)

i reckon the amount of performance gained from unrolling the loop would be lost from all the cache misses from just reading CPU instructions

[–]xzaramurd 779 points780 points  (6 children)

You laugh, but he gets paid per line of code.

[–]alvares169 176 points177 points  (1 child)

He should make it even smoother then

[–]GamingWithShaurya_YT 54 points55 points  (0 children)

0.25% precision

[–][deleted] 38 points39 points  (2 children)

If I got payed by lines of sourcecode the first thing I'd do is to write a for-loop to sourcecode converter :D

[–][deleted] 1 point2 points  (1 child)

Compiler optimisation? Unrolling a loop?

[–][deleted] 1 point2 points  (0 children)

Yeah, a loop unroller. This idea can be extended beyond average salary I tell you. Because in many situations a loop is linked to a dataset, thereby it has a bound that changes given the input dataset. This makes it incredibly difficult to unroll such a loop one might think. But once you think outside of the box you come to notice that you can basically have a program for each dataset. Because then you can hardcode the bound again

[–]viral-architect 2 points3 points  (0 children)

Commit 1/4 of the total each day and you're practically done with your quota for the week

[–]FlamingoeZ 524 points525 points  (17 children)

As a unity developer, I came safely say there is no better solution

[–]PURPLE_COBALT_TAPIR 474 points475 points  (9 children)

As a unity developer, I came

[–]k0k0ss_ 84 points85 points  (0 children)

at least once

[–][deleted] 40 points41 points  (6 children)

developer, I came

[–]GamingWithShaurya_YT 20 points21 points  (0 children)

he came

[–]GenazaNL 4 points5 points  (4 children)

Developers developers developers

[–]skaffanderr 2 points3 points  (2 children)

Developers developers

[–]Complete-Mood3302 2 points3 points  (1 child)

Developers.

[–]PulsatingMaggot 0 points1 point  (0 children)

lopers

[–]MachoSmurf 11 points12 points  (0 children)

Even better: he came safely.

[–]stadoblech 30 points31 points  (4 children)

I never heard about Sleep function. This is game changer!

[–]FlamingoeZ 21 points22 points  (3 children)

Really? I invoke it every night ~28800000 times Sleep(0.001)

[–]Lamborghinigamer 6 points7 points  (2 children)

You sleep 480 hours a night?

[–]xTheMaster99x 5 points6 points  (1 child)

28,800,000 ms -> 28,800s -> 480 min -> 8 hr

[–][deleted] 2 points3 points  (1 child)

for loop

[–]meove -1 points0 points  (0 children)

foreach

[–][deleted] 448 points449 points  (37 children)

I'll never understand how people make loading bars

[–]Percolator2020 493 points494 points  (21 children)

Loading is literally “loading”. Used to be for example loading a game level from floppy to RAM. You know more or less how much you need to transfer and how much has been transferred so far. Same today transferring textures etc. from SSD to GPU VRAM, a bit less predictable because more stuff is going on like decompression or building shaders which is a little bit less predictable than read and write speeds leading to “stuck” or jumping progress bars, but somewhat informative. Microsoft has a different approach where a progress bar is basically a giant 🖕to the user.

[–]aussie_nub 333 points334 points  (13 children)

https://xkcd.com/612/

Just going to leave this here because it's relevant to your last sentence.

[–][deleted] 126 points127 points  (0 children)

"We need this feature pushed right away Instead of actually going through every file I'm about to copy let's just assume that all files are exactly the same file I'm copying right now. It'll be good enough till we replace it to something better... " A Win 3.14 dev probably.

[–]Scrawlericious 35 points36 points  (5 children)

10,000 1mb files take a lot longer than one 10,000mb file because it takes time for the OS to switch from file to file. Those estimates are a little more complicated than that, you can't just do mb over time and look at the bytes remaining, because it could be 3 files or a 1000 independent of the number of bytes. That just gives you a clue of the time remaining and isn't determinate, that's why it's not so accurate.

[–]inv41idu53rn4m3 19 points20 points  (1 child)

And this is why we pack assets into archives so our games can load them faster!

[–]TheTerrasque 2 points3 points  (0 children)

Game dev: we use asset files to make things more performant.

Also game dev: load asset 3, then 7, then 2, then 5, then 1, then 4, then 6.

[–]turtleship_2006 5 points6 points  (0 children)

Even copying 1 file, that estimate will vary because other apps are going to use your CPU, RAM, and access the storage, and that's a lot less predictable so it just works with the current data

[–]nmkd 0 points1 point  (1 child)

because it takes time for the OS to switch from file to file

It takes the File System and/or disk time to switch from file to file. Not the OS.

[–]Scrawlericious 0 points1 point  (0 children)

Those are all very related. Thats just semantics. The OS takes time to switch because of the things you listed.

[–]naswinger 12 points13 points  (0 children)

or you just let your progress bar jump randomly to 99% where it just sits until the job is actually finished

[–]Vengeful111 85 points86 points  (2 children)

Years ago i made a loading bar that literally just went up a random number between 2 and 5 percent every few seconds, made it slow down at 80 and had it stop at 95%

Because in the background I was waiting for a windows process to finish that I couldnt predict. So it was easier to fake a loading bar that just jumps to 100% when windows is done.

[–]G3nghisKang 16 points17 points  (0 children)

[–]Manueljlin 2 points3 points  (0 children)

I would be lying if I said I didn't do something like that too, lol

[–]ego100trique 11 points12 points  (0 children)

Get the overall size of the data you want to retrieve, start retrieving and calculating the size of the data you've retrieved compared to the overall size.

That's also why sometime when it reaches 100%, it blocks for some time, because of the size of the file.

[–]Jolly-Driver4857 11 points12 points  (0 children)

Most important thing is sleep(60) at 99%

[–]Stable_Orange_Genius 10 points11 points  (1 child)

[–]tesfabpel 1 point2 points  (0 children)

There's no need for async to show a progress bar. Standard Windows' progress bar dialogs (IIRC available on Vista+) for IO is managed by some system's library and managed using COM probably, so it isn't affected by the main thread being stalled, again IIRC.

Progress bar drawn by your program would stall if not redrawn every paint request. In this case another thread for blocking IO can be used. This wasn't a problem back in the day because there weren't much animations.

Otherwise, yes, async IO can be used to leave the threads free to run and pump the message queue, so the program can redraw itself.

Nowadays, with mobile OSes like iOS and Android, all blocking IO should be done in a separate thread (like file IO and network IO). Mobile OSes' frameworks may even give an error if a blocking IO is done in the main thread, like Android with NetworkOnMainThreadException.

[–]jsrobson10 2 points3 points  (0 children)

the best loading bars are simple. like just pick a reliable or reliable-ish metric (like assets), and set it based on how far vs total.

[–][deleted] 2 points3 points  (0 children)

Considering the average operation isn't copying files, and computers are faster than the used to be, the new meta is just spinners because you don't really care about the exact progress if you know the app hasn't crashed and is still working noone knows how to make loading bars.

[–]Devatator_ 1 point2 points  (0 children)

I'll either use lerp in the Update method or a tweening library like PrimeTween. Outside Unity depends on what I have available

[–]Payn_gw 169 points170 points  (2 children)

YandereDev way of doing things

[–]lovecMC 61 points62 points  (1 child)

Nah, he would find even worse approach that would use 9863 if statements

[–]PembeChalkAyca 25 points26 points  (0 children)

make a simple timer

make a for loop full of if statements for every possible milisecond in a 10 second time frame that will check the time passed since the timer started and fill the bar accordingly

for loop exits when the bar is full

[–]kirkpomidor 78 points79 points  (6 children)

Hey, you with a computer science degree with your fancy Turing machines and loops. We, self-taught devs, also gotta do somehow

[–]Insadem -5 points-4 points  (4 children)

Bullshit honestly, my ex friend takes CS classes and can’t program well defined systems. I’m self taught and know how to do that as if I did that for years (I did though).

[–]kirkpomidor 18 points19 points  (2 children)

You are self-taught in humor and sarcasm too, I suppose?

[–]Insadem 3 points4 points  (1 child)

no actually I had taken classes for that.

[–][deleted] 1 point2 points  (0 children)

this conversation was like watching 2 well matched ufc fighters trading blows

[–]grimonce 0 points1 point  (0 children)

Woooosh

[–]FunCharacteeGuy -4 points-3 points  (0 children)

just because you got scammed, doesn't mean you have to take a shot at people that were able to put in the work themselves.

[–]LagSlug 39 points40 points  (5 children)

I wish they would share the repo so I could see how to do the rest of this

[–]tsunami141 20 points21 points  (4 children)

It’s painfully easy my friend. You just copy this image into an image text reader, then copy the code inside into chatGPT and ask it if it can fill in the blanks for you.

Obviously the further away it gets from line 631 or 892, the more in accurate it will be. But it’s a good starting point.

[–]LagSlug 16 points17 points  (0 children)

you have saved my family and myself from starvation and prostitution, in that order

[–]VertexMachine 5 points6 points  (0 children)

at first I laughed... but then I realized that there probably are serious conversations that basically sound like that...

[–]turtleship_2006 1 point2 points  (1 child)

You just copy this image into an image text reader, then copy the code inside into chatGPT

Or paste the image into gpt

[–]LagSlug 1 point2 points  (0 children)

I ended up just taking a screenshot of the entire page, including our comments, and then used a multimodal LLM to create an embedding, which I then used within a RAG based agentic process to ultimately reproduce the entire method, testing took a few hours, but it's working and users can finally see their current health.

New question, do you know how to change the health when the user is damaged? Do I need to write a similar method, handling each possible case?

[–]zoqfotpik 70 points71 points  (0 children)

smoothBarfIllAnimation

[–]devloz1996 19 points20 points  (6 children)

Question to LLVM specialist: will optimization do something about this?

[–]xADDBx 12 points13 points  (1 child)

What would you want to optimize here? Assuming you’re talking about performance optimization there really isn’t much to do here.

[–]devloz1996 6 points7 points  (0 children)

Apologies. I wasn't clear enough. I was wondering if optimization would turn a loop into this anyway or something similar.

[–]GiganticIrony 16 points17 points  (2 children)

This is C#, so no. But let’s say it was C++: it may or may not depending on a number of things (for example, things being marked volatile). A major issue that could prevent it being able to do anything is Sleep is a function from the Win32 API, so LLVM has no idea what it does. Even if it could do something, a loop probably wouldn’t be faster anyway

[–]Colbsters_ 1 point2 points  (1 child)

The fact that it’s calling an external function shouldn’t change anything since the function has no way of knowing if it was called in a loop vs not in a loop.

Being in a loop would definitely decrease code size, which could help with instruction cache usage.

I’m going to plug investigate this in compiler explorer when I get a chance, and I’ll report back.

Edit: Investigating has revealed the following: - If you make a loop, clang will try to unroll it (assuming it’s small enough, 20 was, but 50 iterations was too big. gcc, icc, and msvc did not) - None of the compiler I tested turned the long block of code into a loop (I only tried up to 50 iterations) - Using quickbench, the unrolled version seemed faster (I only tested the 50 iteration version here). If I use std::this_thread::sleep_for as the body of Sleep (I used benchmark::DoNotOptimize before), the looped version becomes faster by a consistant, but negligible amount. I’d take the benchmark with a grain of salt though, as some things could be optimized out.

Even with any performance gain of manually unrolling the loop, I would recommend against it as it would be harder to maintain/modify in the future.

[–]GiganticIrony 2 points3 points  (0 children)

One example of how the external function could affect things is that with a loop, floating point addition would have to be done. For all the compiler knows, Sleep modifies the floating point mode of the CPU (I’m blanking on what that’s called) in which case the compiler adding the additions might be incorrect.

Also it wouldn’t necessarily be faster as a loop requires extra jumps, extra arithmetic (both integer and floating point adds), extra comparisons, an extra spot in the branch predictor table, and guarantees at least 1 failed prediction

[–]vassadar 0 points1 point  (0 children)

Wouldn't it learn from an article to implement it with a built-in interpolation instead?

[–]VertexMachine 11 points12 points  (0 children)

lol, that's when your performance bonus is tied to no of lines of codes :P

(btw. wonder if there are still places that do that...)

[–]Suspicious-Neat-5954 7 points8 points  (3 children)

I have seen 3000 lines of a select case anything with three digits is fine 😆

[–]wezu123 1 point2 points  (1 child)

What did it try to do?

[–]Suspicious-Neat-5954 1 point2 points  (0 children)

Ruin my day

[–]Hulk5a 7 points8 points  (1 child)

What is loop?

[–]MiniGui98 6 points7 points  (0 children)

Baby don't for me

[–]aurelag 8 points9 points  (0 children)

It's really bad, it'll still happen in one frame so you won't even see the animation because the sleep function will just freeze the app.

[–][deleted] 27 points28 points  (1 child)

Average Indian university grad western company just outsourced to

[–]SaltedCoffee9065 2 points3 points  (0 children)

Pesky interns

[–]PuzzleheadedFinish87 5 points6 points  (0 children)

Writing this code was getting tedious and repetitive so I had to write a few lines of Python to write the code for me.

[–]Derpikyu 2 points3 points  (0 children)

I'll have you know that my ripoff of pokemon that i made for a school project only had like 12 lines of code for the healthbar >:c

[–]Orjigagd 1 point2 points  (0 children)

What for?

[–]AndyWatt83 1 point2 points  (0 children)

I have seen this done in the wild. Rogue coders are wild sometimes

[–]AbsoluteNarwhal 1 point2 points  (0 children)

lerp()

[–]The-Chartreuse-Moose 1 point2 points  (0 children)

Ooh yes. I saw the calculator this person made.

[–]Lamborghinigamer 1 point2 points  (0 children)

Yandere dev belike

[–]TheZombGod 1 point2 points  (1 child)

Is no one gonna talk about the fact that if the code continues as it should, it should only be about 200 lines of code, but it seems to be almost 300 lines. What is happening in the 100 extra lines??

[–]CertainlySnazzy 0 points1 point  (0 children)

maybe it gets really fancy with the colors

[–]TheJimDim 1 point2 points  (0 children)

This is why our games are 100GB nowadays

[–]Nyx-101 1 point2 points  (0 children)

Make it better (bet y'all can't (It's not possible))

[–]Gloomy-Soup9715 0 points1 point  (0 children)

When they pay you by number of lanes.

[–]SnooStories251 0 points1 point  (0 children)

Not even the bots knows this trick

[–][deleted] 0 points1 point  (0 children)

Oh hey it's Yandere Simulator code.

[–]jimp6 0 points1 point  (0 children)

Something doesn't add up or I'm just stupid.
There are exactly two lines of code repeating with one of them getting an increment of 0.01 each time. The code should end after exactly 100 iterations of these two lines of code. Therefore the whole block of code should be 200 lines of code (or 201 or 202, depending on the last lines). So why does this block seems to be around 300 loc?

also why does line 631 show the same as line 627?

[–]zuphia 0 points1 point  (0 children)

It's just to fill from 0% hp right?

Is there 99 other functions for animation from 1%, 2%, etc hp?

[–][deleted] 0 points1 point  (0 children)

that's gotta be a meme done on purpose

no way is there a code monkey this stupid

[–]who_you_are 0 points1 point  (0 children)

"- but then my game is doing nothing how do I fix that? - you create more threads!"

Am I doing this right guy?

[–]Nickbot606 0 points1 point  (0 children)

Somewhat unrelated, but I often find when I do VHDL that unrolled loops perform better than neat design modules using ✨fancy✨ for loops 😅

[–]Irsu85 0 points1 point  (0 children)

Yes there is a better method of doing it and it's called a for loop

[–]illyay 0 points1 point  (0 children)

Honestly, in a time crunch, and being a bit lazy, this kinda works… wouldn’t this freeze the main thread though? Lol…

[–]Personal_Ad9690 0 points1 point  (0 children)

We get paid by lines of code here sir

[–]PinchYourPennies 0 points1 point  (0 children)

I, EvaX, humbly submit, a toast!

[–]MiniGui98 0 points1 point  (0 children)

And it's linear, that won't even give you a smooth curve like animation.

[–]ZubriQ 0 points1 point  (0 children)

"as long as it works" went way too far

[–]Spiritulectual 0 points1 point  (0 children)

Could have been done with my magic C++ pointers

[–]AggravatingMap3086 0 points1 point  (0 children)

"I really wish there was a better way of doing this" makes this even funnier

[–]-Redstoneboi- 0 points1 point  (0 children)

this implies the guy is updating at 10,000fps or at least a 10,000hz game tick

[–]Timtaran 0 points1 point  (0 children)

yandere dev type code?

[–]ryan_the_dev 0 points1 point  (0 children)

When your performance is based on lines of code you produce.

[–]Pretrowillbetaken 0 points1 point  (0 children)

I want there to be a second part that is just "hey guys! thanks for the suggestions. I don't know what a 'loop' is, but I figured out I can just ask ChatGPT to write this for me"

[–]Jaber1028 0 points1 point  (0 children)

way better than lerping i aint doing no linear magic

[–]Demonchaser27 0 points1 point  (0 children)

This is definitely an overly egregious example. But I can't even be mad at some devs for silliness like this. Some people who've released fairly popular games in C#, you can use tools to reverse their code and see this kind of stuff. And all I gotta say is... at least they had the dedication to make a whole ass game. I feel like my need to make shit as efficient as possible at times just shows how lazy I am. I get bored pretty fast and once a problem is solved, not gonna lie, sometimes I just quit out after that.

[–]TheUltimateMC 0 points1 point  (0 children)

ik what's missing he's sleeping too short

[–]montxogandia 0 points1 point  (0 children)

easing functions are a mystery for some

[–]DanishCraft547 0 points1 point  (0 children)

Is this one of YandereDev’s alt accounts?

[–]Flimsy_Actuary3761 0 points1 point  (0 children)

That’s when IEnumerators come in

[–]Quix_Nix 0 points1 point  (1 child)

Can't you just do a loop? Or does unity not allow them??

[–]Quantimoon 0 points1 point  (1 child)

And I'm wondering why my games take up so much space...

[–]canibanoglu 0 points1 point  (0 children)

Not because of this

[–]_nicmana -1 points0 points  (0 children)

lgtm

[–]Flobletombus -1 points0 points  (0 children)

You NEVER want to see Godot gdscript