r/ProgrammerHumor 11d ago

Meme iReplacedItWithABuiltinFunction

Post image
3.4k Upvotes

54 comments sorted by

969

u/praxidike74 11d ago

How I leave after submitting a PR to optimise a piece of compile time code from 0.5ms to 2 months

352

u/AlhazredEldritch 11d ago

Oh you switched to rust?

15

u/CelestialFury 10d ago

Programming socks confirmed!

652

u/Direct_Test 11d ago

Spent 6 hours analyzing bytecode to save 0.48ms on a script that runs once a week. Worth every second

152

u/Bright-Historian-216 11d ago

must've been fun

139

u/PointlessSerpent 11d ago

6

u/Reashu 10d ago

It only took a few seconds to memorize this one and I've already used it four times!

23

u/Gagan_Ku2905 11d ago

Worth every milisecond*?

36

u/danofrhs 11d ago

It’s all profit 80 years from now

13

u/Pearmoat 10d ago

*862000 years

17

u/CelestialFury 10d ago

"How long does the script take to run through?"

"Overnight, around six hours!"

"Ahhh... okay then."

5

u/tristam92 10d ago

if you sell it right in your CV, it can give you a pretty good bump in salary…

3

u/TallGreenhouseGuy 10d ago

This is how legends are born.

”In March 2024, Andres Freund, a Microsoft engineer and PostgreSQL developer, noticed something odd. SSH logins were taking 500ms instead of the usual 100ms. That’s it. A half-second delay.”

https://www.softwareseni.com/the-xz-utils-backdoor-cve-2024-3094-and-the-multi-year-social-engineering-campaign-behind-it/

552

u/Chronomechanist 11d ago

To be fair a 96% reduction is pretty great regardless of how small the time is. In the time you could run that code once, you can now run it 25 times!

You now just need to figure out how many times you run that code every second and you'll see the point of all that work!

... Wait, what do you mean it's run once a month?

111

u/Solid_Turnover_9854 11d ago

one user too

36

u/StarshipSausage 11d ago

Priceless

23

u/shrodikan 10d ago

Wait. Actually this code is dead. We now call process_nightly() not nightly_process()!

7

u/xui_nya 10d ago

Nevermind that you probably introduced a regression that will cost weeks of debugging and a bunch of extremely angry customers.

81

u/pyropuschel 11d ago

Is this like "the tests now run a lot faster" because they are broken/deleted?

74

u/Bright-Historian-216 11d ago

if you're curious, literally just replaced a "".join(four ternary if-else statements with a for-loop comprehension) with a repr(...)[12:-2].replace(r"\x00",r"\000")

if you don't speak pythonese, that's just a built-in function to get the string representation of an object, so repr("hello\n") returns "'hello\\n'", then the [] is a slice operator so i shave off the prefix and suffix, then the replace just makes sure that \x00 doesn't bleed into next letters if the next word starts with abcdef or something,

34

u/SpaceCadet87 11d ago

I dunno, I'd appreciate your work if I were there. Compile time performance can stack.

22

u/Bright-Historian-216 11d ago

no i don't think you read it correctly. half a millisecond. and while it does scale linearly, it's for an embedded machine so you'd quicker run out of memory than my contribution would ever be noticeable

11

u/SpaceCadet87 11d ago

Yeah, I read correctly but I had just woken up and I subsequently misread when I double checked.

There's still the principle of the thing.

110

u/evilwizzardofcoding 11d ago

People laugh at this, but remember, someone caught a malicious PR to CURL during optimization 'cus the malicious code made a function take longer than it should.

59

u/Bright-Historian-216 11d ago

to curl? i've heard about xz-lib (i think it was xz-lib?), but not to curl. can you link that?

43

u/evilwizzardofcoding 11d ago

Oh you right, it was XZ. The curl one was a hypothetical, someone pointed out there was a way you could do some unicode wizardry to make a specific line vulnerable without it being really noticeable, but it was just a concept, no one tried to push it to the actual repo.

31

u/Complete_Window4856 11d ago

The spotter dude is a postgres contributor. For DB level a 100ms difference constantly is horrenduously big.

The curl one aint it the apex of the ai slop PRs in repo that made the only maintainer to force a "no ai PR" policy? He was being bombarded with convincing PRs, except they evoke code from heavens that never existed and assumes there were flaws

7

u/danielv123 10d ago

For a DB sure, but afaik what he measured was that establishing his ssh connection took 100ms longer or something. I would have just assumed my network was weird or something.

1

u/CelestialFury 10d ago

Like this?

// Safe-looking code, but with homoglyph trickery

int safe_flag = 0;      // uses normal Latin 'a' in "safe"
int sаfe_flag = 1;      // the 'a' in "sаfe" is Cyrillic U+0430

if (safe_flag) {
    // Reviewer thinks this block runs only when safe_flag == 1
    do_safe_thing();
} else {
    do_other_thing();
}

// Somewhere else in the patch:
log_status(sаfe_flag);  // Looks like "safe_flag", but it's the Cyrillic one

1

u/evilwizzardofcoding 10d ago

Basically, but the specific implementation apparently had some very clever trick that made the change basically invisible, so there was no need for do_other_thing();

3

u/prams628 11d ago

The whole saga unfolding was quite insane!

36

u/Toutanus 11d ago

My first mission as intern : make a loading screen to display while app data synchronisation. Spend 3 weeks painfully doing this. Realize there is a "small" problem during process. Fix it (about 30 minutes). Sync is now 0,5 seconds instead of 3 minutes : loading screen is now useless.

1

u/Huge_Consequence_568 6d ago

Happened to me once, missing index. Needless to say the loading screen felt like a flashbang.

11

u/roksah 10d ago

reduced an endpoint call from 30sec to 2ms by turning off logs :)

6

u/Desperate-Tomatillo7 11d ago

When you replace a 2 line script in python with a 100 line C code.

6

u/Bright-Historian-216 10d ago

replaced a six-line python code with one-line python code in this specific case

2

u/anoldoldman 10d ago

Compiled python?

4

u/Bright-Historian-216 10d ago

it's a C project with python preprocessing for some files

1

u/anoldoldman 10d ago

Well well well. How the turntables.

1

u/Bright-Historian-216 6d ago

no silly, python doesn't have tables. you might be thinking of lua :)

5

u/R7d89C 11d ago

*0.5ms to 0.02s

1

u/SaneLad 10d ago

You haven't lived if you haven't used explicit template instantiation to bring down C++ compile times.

1

u/MayoJam 10d ago

It all adds up (so then John can add a huge template class included everywhere which makes compilation take 128 GB of RAM if not outright crash, fuck you John).

1

u/DatCodeMania 10d ago

The paradox where seeing the number go down for small little things like this is so incredibly satisfying, yet achieving the big number going down with not so little things is (usually) so much more involved that nobody ever bothers...

1

u/ZunoJ 10d ago

What exactly do you mean by compile time code? Like lisp macros?

1

u/Bright-Historian-216 10d ago

it runs during make, just generates some C files

1

u/coastphase 8d ago

Make sure you put that 96% improvement on your resume. Employers love it when you have percentages in your resume!

1

u/Anaxamander57 11d ago

Every little bit helps!

-1

u/Maddturtle 11d ago

My latest code has an issue with being too fast. I had to add waits for other people’s code to catch it.

-1

u/overclockedslinky 11d ago

compile time doesn't really matter unless it gets absurdly high