242
Apr 11 '21
“I think it is a CPU bug” 🤡
68
u/TheBrainStone Apr 11 '21
I love that assuming it’s real they didn’t notice that it only happened for negative numbers. Or in their case only -1. Like that they never bothered to even try an even negative number during debugging is such an entertaining thought.
And let’s not even talk about the inefficient implementation, that’s inspired by actual common practice. I m in love with this post
235
u/Square789 int mystery=*((int*)rand()) Apr 11 '21
in C, -1 % 16 is -1; thus the return value for is_even would be in front of the lookup array and undefinedTM.
Also a polyglot is source code that is valid in different programming languages, this is more of a... very enthusiastic jit compiler?
139
15
71
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Apr 11 '21
Image Transcription: Code
def isEven__fast__(x):
# this is a performance critical operation
# so we are going to drop down to C
c_code = """
#include <stdlib.h>
int is_even(int x) {
// since this is performance critical
// we are going to keep a table of commonly
// used values and try to reduce into that case
int ix = x % 16;
const int lookup[16] = {
1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1
};
return lookup[ix];
}
int main(int argc, char **argv) {
return is_even(atoi(argv[1]));
}
"""
import subprocess, os # we only need these imports for this function
# only compile if we don't have the binary yet.
# otherwise it's cached
if not os.path.exists('tmp/a.out');
with open('/tmp/even.c', 'w') as f:
f.write(c_code)
# i know this works because i ported from bash
# and it works in the terminal
# also for performance, move it into /tmp so that the binary stays in me
mory
subprocess.run('''
cc -0fast /tmp/even.c
mv a.out /tmp
''', shell=True)
# ok now run it
subprocessCompletedProcess = subprocess.run(['/tmp/a.out', str(x)])
return_code = subprocessCompletedProcess.returncode
print(return_code)
if return_code == 0: # translate c boolean to python
return False
# originally i had checked for return code of 1 first
# and then did catch-all to return True
# but then sometimes to seemed the return code was 139 or 255 --
# but only on certain CPUs. i think it is a CPU bug.
# i didn't have time to track it down and it doesn't happen very
# often so it's not crucial to the performance
# so just put this in the catch all case
#elif return_code == 1:
else:
#print(return_code)
return True
if __name__ == '__main__':
print(isEven__fast__(-1))
print(isEven__fast__(0))
print(isEven__fast__(1))
print(isEven__fast__(2))
print(isEven__fast__(15))
print(isEven__fast__(1.5))I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
15
138
Apr 11 '21 edited Apr 11 '21
100% is satire
Love that this will execute any arbitrary a.out as well hahaha
17
Apr 11 '21 edited Apr 11 '21
If only they stored the hash of the compiled code and compared it before running it... I think I would have to get a picture of it framed, satire or not!
Edit: Or, even better—verified the code by comparing its disassembly lol... I think I may wanna try something like this in like Kotlin or Java hahaha
50
u/starryhound Apr 11 '21
"Performance"
58
u/_PM_ME_PANGOLINS_ Apr 11 '21
They know that’s the performance critical part because they measured and it takes up 95% of the run time.
70
67
45
40
35
u/private_static_int Apr 11 '21
In what universe would this be faster than "return x%2==0"?
26
5
Apr 11 '21
Personally, I don't like relying on the optimizer for simple stuff so I still type (n&1)==0
1
9
21
u/meneo Apr 11 '21
Jokes on him, he should have used the is even api
8
Apr 11 '21
Exactly.
For real though, it would be faster. First of all, the overhead of compilation in itself is too big. Second, if using Windows, it would be slow since it would create a new thread, which is generally slow on windows. Third, on a mechanical drive, reading and writing speed is much slower than sending a request.
0
u/DZekor Apr 11 '21 edited Apr 12 '21
Wait is that compiling C at python runtime?
Compile it to ram for more speed ez. What could go wrong...
edit: to be clear that was a joke
17
u/PsCustomObject Apr 11 '21
Maybe I have a fetichism for comments but ‘ok now run it’ is what really got me :-)
17
22
u/LVNmaster Apr 11 '21
How do these people have a job? Like, every single idea here is so atrocious it hurts.
22
5
Apr 11 '21
[removed] — view removed comment
9
u/CheezyArmpit Apr 11 '21
Writing long inefficient isEven programs is a meme, so many people wooshed here.
4
Apr 11 '21
They must have never heard of the Python c api
Or maybe they thought that's how a JIT works
7
u/sneakykenny Apr 11 '21
+ you can actually give the code as stdin to gcc and give it a flag to set the output file path, making this even worse than it already is 🤔
11
u/taptrappapalapa Apr 11 '21
Hopefully this wasnt in any sort of production code. I can just imagine the security issues that this would cause.
21
9
u/hoss_pumpkin Apr 11 '21
anyone know the theme?
13
u/TheEnderChipmunk Apr 11 '21
I think it's solarized light? Nor sure
5
0
u/hoss_pumpkin Apr 11 '21
i was thinking that’s what it was but for some reason remembered solarized light being a bit brighter
1
7
u/MyChaOS87 Apr 11 '21
What's so hard with (x & 1)
9
u/ShatterdPrism Apr 11 '21
Remind me if I am wrong, but this is a bit wise Operator and it checks in this case if the last bit of x is a 1 which would mean the number isnt even, right?
6
3
u/CodeLobe Apr 11 '21
!(x&1) // true if even (not odd), false otherwise.In low level langs the
! /* not operator */can be optimized away by using jump if zero JZ instead of JNZ, jump if not zero, or vise versa.1
u/aeropl3b Apr 11 '21
Even in Python this should be faster than trying to compile a C kernel on the fly and then calling it via subprocess... subprocess alone is probably more expensive than most trivial operations...
1
u/ShatterdPrism Apr 12 '21
I find those kind of thought processes, optimization and low level abstraction very interesting to read.
But tbh I don't think it's something I could focus on for longer xD
10
Apr 11 '21
Hey, ignoring the rest of the clusterfuck, it's pretty cool you can compile and run shell commands from python
Edit: shell
13
u/Shakespeare-Bot Apr 11 '21
Ho, ignoring the rest of the clusterfuck, t's quaint merit thee can compile and runneth the lady'll hests from python
I am a bot and I swapp'd some of thy words with Shakespeare words.
Commands:
!ShakespeareInsult,!fordo,!optout5
u/recycle4science Apr 11 '21
This is a terrible bot, it's
'tisandthou. And it has the clbuttic error, which is extra rich on a programming sub.
6
8
5
Apr 11 '21
This is what I call A Fast JIT Engine. Blows LLVM out of the water.
I also love how they didn't even bother to replace the number directly in the Python string, and they opted for command line arguments.
6
u/TheBrainStone Apr 11 '21
Using it as a command line parameter is actually faster as it only compiles once.
So I agree very disappointing.0
1
u/joske79 Apr 11 '21
You should add caching. Once we looked up a value we can remember whether it's even, odd or something else. Maybe remember the last 10,000 values that were queried?
0
4
3
3
u/RecDep Apr 11 '21 edited Apr 11 '21
Why not send the C code into the compiler through stdin?? Why not use -o to write the output directly to the desired file path?? Why not compile in advance and use Python’s builtin support for C?????
I hate everything about this post, it just gets worse the more you read into it.
5
2
2
1
u/acid_minnelli Apr 11 '21
Ok but this isnt the w o r s t idea.
I would move the c code in to its own file, it doesn't need to be hard coded in to this file.
Also the code to check whether compilation has happened before needs work on.
But the idea of running C code from Python is a great idea (its how the scientific packages work) & who ever wrote this is clearly thinking in the right direction.
1
u/ikoniq93 Apr 13 '21
Just to make sure I'm reading this clusterfuck right...
We're using JIT compilation of C code to find if a number is even or odd, then handing that off to the rest of the script as a boolean to continue to run?
Wouldn't it be easier to just do the number mod 2 and then use an if statement to return false if the result isn't zero?
tbf i'm not super familiar with python but that just seems to be the logical answer to me.
1
1
1
1
1
Apr 11 '21
O M G! There is a fine line between clever and stupid, but this code never gets close to that line.
0
0
0
0
0
0
0
0
0
492
u/pandolf86 Apr 11 '21
Wait is that compiling C at python runtime?