r/ProgrammerHumor 1d ago

Meme iReplacedItWithABuiltinFunction

Post image
3.1k Upvotes

51 comments sorted by

885

u/praxidike74 1d ago

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

316

u/AlhazredEldritch 1d ago

Oh you switched to rust?

10

u/CelestialFury 17h ago

Programming socks confirmed!

11

u/DryerLobes 17h ago

Optimized straight into production downtime

615

u/Direct_Test 1d ago

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

146

u/Bright-Historian-216 1d ago

must've been fun

123

u/PointlessSerpent 1d ago

4

u/Reashu 3h ago

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

22

u/Gagan_Ku2905 1d ago

Worth every milisecond*?

32

u/danofrhs 1d ago

It’s all profit 80 years from now

13

u/Pearmoat 16h ago

*862000 years

13

u/CelestialFury 17h ago

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

"Overnight, around six hours!"

"Ahhh... okay then."

5

u/tristam92 20h ago

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

1

u/TallGreenhouseGuy 8h 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/

527

u/Chronomechanist 1d 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?

101

u/Solid_Turnover_9854 22h ago

one user too

32

u/StarshipSausage 21h ago

Priceless

20

u/shrodikan 18h ago

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

6

u/xui_nya 17h ago

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

78

u/pyropuschel 1d ago

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

76

u/Bright-Historian-216 1d 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,

30

u/SpaceCadet87 1d ago

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

23

u/Bright-Historian-216 1d 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 1d 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.

107

u/evilwizzardofcoding 1d 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 1d ago

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

40

u/evilwizzardofcoding 1d 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 1d 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 16h 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 17h 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 10h 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 1d ago

The whole saga unfolding was quite insane!

35

u/Toutanus 1d 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.

12

u/roksah 20h ago

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

6

u/Desperate-Tomatillo7 21h ago

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

5

u/Bright-Historian-216 18h ago

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

2

u/anoldoldman 16h ago

Compiled python?

4

u/Bright-Historian-216 16h ago

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

1

u/anoldoldman 16h ago

Well well well. How the turntables.

4

u/R7d89C 22h ago

*0.5ms to 0.02s

1

u/SaneLad 17h ago

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

1

u/MayoJam 15h 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 10h 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 5h ago

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

1

u/Bright-Historian-216 5h ago

it runs during make, just generates some C files

1

u/Anaxamander57 1d ago

Every little bit helps!

-1

u/Maddturtle 1d ago

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

-2

u/overclockedslinky 23h ago

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