r/ProgrammerHumor • u/Bright-Historian-216 • 1d ago
Meme iReplacedItWithABuiltinFunction
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
22
32
13
u/CelestialFury 17h ago
"How long does the script take to run through?"
"Overnight, around six hours!"
"Ahhh... okay then."
5
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.”
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
20
u/shrodikan 18h ago
Wait. Actually this code is dead. We now call process_nightly() not nightly_process()!
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 one1
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
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.
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
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
-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
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