Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Suspicious Dish posted:

I wonder if Intel publishes "here's nops for all byte sizes that matter" which their processors will try to handle faster than other nop instructions.

IA-32 Intel Architecture Optimization Reference Manual, page 2-81, from 2004 posted:

NOPs

Code generators generate a no-operation (NOP) to align instructions. The NOPs are recommended for the following operations:
  • 1-byte: xchg EAX, EAX
  • 2-byte: mov reg, reg
  • 3-byte: lea reg, 0 (reg) (8-bit displacement)
  • 6-byte: lea reg, 0 (reg) (32-bit displacement)
These are all true NOPs, having no effect on the state of the machine except to advance the EIP. Because NOPs require hardware resources to decode and execute, use the least number of NOPs to achieve the desired padding.

The one byte NOP, xchg EAX,EAX, has special hardware support. Although it still consumes a µop and its accompanying resources, the dependence upon the old value of EAX is removed. Therefore, this µop can executed at the earliest possible opportunity, reducing the number of outstanding instructions. This is the lowest cost NOP possible.

They probably have more recent recommendations.

Adbot
ADBOT LOVES YOU

Zhentar
Sep 28, 2003

Brilliant Master Genius

rjmccall posted:

As I read the language spec, Java is actually required to constant-fold concatenations that involve only constant strings and primitives, so "abc" + 4 * 5 + "def" is required to be treated exactly like "abc20def".

This seems likely; I know that C# has the same requirement.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
One other thing is that 0x90 is also a true nop on amd64. Xchg ebx ebx has the net effect of clearing the upper bits of rbx, while 0x90 doesn't affect rax at all.

trex eaterofcadrs
Jun 17, 2005
My lack of understanding is only exceeded by my lack of concern.

rjmccall posted:

They probably have more recent recommendations.

....And that answers my question.

quote:

Because NOPs require hardware resources to decode and execute, use the least number of NOPs to achieve the desired padding.

Goat Bastard
Oct 20, 2004

rjmccall posted:

As I read the language spec, Java is actually required to constant-fold concatenations that involve only constant strings and primitives, so "abc" + 4 * 5 + "def" is required to be treated exactly like "abc20def".

If string concatenation in Java ever used String.concat, it was a very long time ago; Java was using StringBuffer for it at least in 1.1. It started using StringBuilder in 1.5 (when targeting 1.5 or higher).

Yea I could have been wrong about that. Maybe it made a new StringBuffer for every + operation? I just remember that people used to freak out about seeing string concatenation with a + because of all the unnecessary objects it would create.

rjmccall
Sep 7, 2007

no worries friend
Fun Shoe

Goat Bastard posted:

Yea I could have been wrong about that. Maybe it made a new StringBuffer for every + operation? I just remember that people used to freak out about seeing string concatenation with a + because of all the unnecessary objects it would create.

Well, the compiler definitely doesn't make much effort to be clever about avoiding new StringBuffers. For example, the bytecode generated for this is awful:
Java code:
    static int main(String[] argv) {
        String s = "<";
        for (String arg : argv) s += " " + arg; // s = new StringBuilder().append(s).append(" ").append(arg).toString()
        s += ">"; // s = new StringBuilder().append(s).append(">").toString()
        System.out.println("args: " + s);  // new StringBuilder().append("args: ").append(s).toString()
        return 0;
    }
That is, the Java static compiler does not make any effort to translate this into the optimal form:
Java code:
    static int main(String[] argv) {
        StringBuilder s = new StringBuilder().append("args: <");
        for (String arg : argv) s.append(' ').append(arg);
        s.append('>');
        System.out.println(s.toString());
        return 0;
    }
I wouldn't be surprised if the JIT did this transformation, though.

Bonfire Lit
Jul 9, 2008

If you're one of the sinners who caused this please unfriend me now.

rjmccall posted:

They probably have more recent recommendations.
The 2011 one has these:

Intel® 64 and IA-32 Architectures Optimization Reference Manual, June 2011 posted:

1-byte: XCHG EAX, EAX
2-byte: 66 NOP
3-byte: LEA REG, 0 (REG) (8-bit displacement)
4-byte: NOP DWORD PTR [EAX + 0] (8-bit displacement)
5-byte: NOP DWORD PTR [EAX + EAX*1 + 0] (8-bit displacement)
6-byte: LEA REG, 0 (REG) (32-bit displacement)
7-byte: NOP DWORD PTR [EAX + 0] (32-bit displacement)
8-byte: NOP DWORD PTR [EAX + EAX*1 + 0] (32-bit displacement)
9-byte: NOP WORD PTR [EAX + EAX*1 + 0] (32-bit displacement)

Kilson
Jan 16, 2003

I EAT LITTLE CHILDREN FOR BREAKFAST !!11!!1!!!!111!

rjmccall posted:

Well, the compiler definitely doesn't make much effort to be clever about avoiding new StringBuffers. For example, the bytecode generated for this is awful:
Java code:
    static int main(String[] argv) {
        String s = "<";
        for (String arg : argv) s += " " + arg; // s = new StringBuilder().append(s).append(" ").append(arg).toString()
        s += ">"; // s = new StringBuilder().append(s).append(">").toString()
        System.out.println("args: " + s);  // new StringBuilder().append("args: ").append(s).toString()
        return 0;
    }

Yeah, using + on strings isn't really all that bad, because the compiler can mostly take care of it ... except when done in a loop.

raminasi
Jan 25, 2005

a last drink with no ice
Just found this:
C# code:
Func<bool> isOutdoors = () => OtherSideCondition == OtherSideConditionType.Outdoors;
return isOutdoors() ? SurfaceType.Roof : SurfaceType.Ceiling;
The worst part? ...It's...it's mine. What was I thinking? The evil is inside of me :gonk:

Shugojin
Sep 6, 2007

THE TAIL THAT BURNS TWICE AS BRIGHT...


Gonna whine about IDL some more as well as somebody else's code.

Evidently IDL just inherently does poo poo as pointers so when I was trying code a little script that was just a FOR loop and pass the value of the FOR counter variable into the other person's code, I was passing a pointer to the address and the thing somebody else wrote that I was calling a lot.

That person's code does things to the value of the input snapshot number (I don't know why exactly, but it ends up being on the order of a few thousand by the end).

Then their code would finish, the counter variable would be on the order of a few thousand and it would exit after processing the first snapshot leaving the remaining ~50-200 untouched. This was a simple fix by just doing

code:
pro (name)(number)
N = number
instead of

code:
pro (name)
or

code:
IDL's asstarded FOR structure
batchnum = i
(other IDL script's name), batchnum
instead of

code:
IDL's asstarded FOR structure
(other IDL script's name),i
I hate IDL :saddowns:

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...
The coding horrors I come across every day are the coding question implementations that my phone screen candidates give me.

Is it really so hard to do LinkedList implementations? :negative:

Janitor Prime
Jan 22, 2004

PC LOAD LETTER

What da fuck does that mean

Fun Shoe

Volmarias posted:

The coding horrors I come across every day are the coding question implementations that my phone screen candidates give me.

Is it really so hard to do LinkedList implementations? :negative:

In what language?

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip
I think I found it. The worst C++ program ever written.


I walked over into manufacturing for a moment today to help one of my coworkers figure out a problem with a calibration station he's making for the product we're both working on. While I was doing this, one of the production support guys came over and asked whether either of us knew C++. I foolishly answered yes.

The system he showed me, written in 2008 (so there's no excuse for it), had one class with about 450 public member variables. The whole thing "worked" by having one method that is a giant state machine that implements all the functionality - manipulating I/O lines, performing GUI functions, etc. The rest of the code works by poking values into member variables, then manipulating the state variable.

Did I mention that they author felt the need to implement all the GUI widgets he used himself?

Do I need to mention that he did so very badly?



The story I eventually teased out of another coworker familiar with the situation is that the program was written by a MechE who had gone to a VB course and written production support software in VB for a few years, who then bought one of those "Learn C++ in a weekend" books or something like that. *shudders*

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

Shugojin posted:

I hate IDL :saddowns:

I loving despise IDL, and i hate the fact that I need to use it for my work. Usually, I can get around it by using Python, but I often have to use some of my team's programs, and astronomers LOVE IDL. You know how much physicists love loving FORTRAN? Yeah, astronomers love IDL like that. A part of it is that the astrolib library has been ingrained very deeply in the community. So, understandably, you are sometimes just stuck dealing with IDL. There's also just the fact that we all tend to like to code in the language we know, and scientists tend to be very much of the thought "eh, why bother" when it comes to learning new programming languages not named FORTRAN or IDL... and in some circumstances, Perl.

I'm just so loving happy that Python has been (quickly) taking over the Universe (literally :colbert: ). Many, many astronomy libraries are being written and released for Python. The young astronomer community is embracing it, and hopefully, by the time I'm a crusty old scientist with my own horde of grad students and post docs, IDL will be a distant memory.

This is all without bringing up how loving outrageously expensive the IDL licenses are. Only loving MATLAB surpasses it in terms of "hey bend over", and at least MATLAB has enough of an audience to justify their insane license costs.

Doctor w-rw-rw-
Jun 24, 2008
Hate to break it to you, but a lot of those Python libs probably have Fortran powering some part of them: http://www.scipy.org/Installing_SciPy/BuildingGeneral

Fortran is also faster than C++ and possibly C for a lot of math operations. Not going away anytime soon - it's still got that niche locked down.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

Hard NOP Life posted:

In what language?

Any language the candidate wants; I tell them to use whatever they're most comfortable with. Most choose Java, though I've had a couple do C#.

At this point, I'm pleasantly surprised if the method implementation they write would actually compile.

Volmarias fucked around with this message at 20:15 on Apr 11, 2013

Zhentar
Sep 28, 2003

Brilliant Master Genius
Throw in a question with string manipulation, and you'll find that most of them are actually choosing a hypothetical Java variant with a mutable String class (which they call "Java" for short).

raminasi
Jan 25, 2005

a last drink with no ice

Doctor w-rw-rw- posted:

Hate to break it to you, but a lot of those Python libs probably have Fortran powering some part of them: http://www.scipy.org/Installing_SciPy/BuildingGeneral

Fortran is also faster than C++ and possibly C for a lot of math operations. Not going away anytime soon - it's still got that niche locked down.

I don't think anybody is complaining about battle-tested, bulletproof numeric Fortran libraries.

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

GrumpyDoctor posted:

I don't think anybody is complaining about battle-tested, bulletproof numeric Fortran libraries.
Especially when it's hidden away from you with a nice API.

There's a pretty big difference between working directly with lovely Fortran code and happening to depend on some very high quality Fortran code that you never need to know exists.

hobbesmaster
Jan 28, 2008

Doctor w-rw-rw- posted:

Fortran is also faster than C++ and possibly C for a lot of math operations. Not going away anytime soon - it's still got that niche locked down.

And how exactly is it faster? Don't point to libraries with bindings for all languages involved.

Internet Janitor
May 17, 2008

"That isn't the appropriate trash receptacle."
Otto Skorzeny: you may have discovered a compelling counterpoint to the argument that introductory programming courses should start with gate-logic and work their way up the stack. Any program running on a real computer clearly can be viewed as a finite state machine, it just isn't a sane level of abstraction for most tasks.

edit: hobbesmaster: One thing I am aware of is that Fortran makes it easier to analyze loop kernels and prove that references do not alias against one another, which can pave the way for optimizations. In classical Fortran the only means of data indirection is array lookups, and data structures tend to be simply flat arrays.

Internet Janitor fucked around with this message at 21:54 on Apr 11, 2013

Blotto Skorzany
Nov 7, 2008

He's a PSoC, loose and runnin'
came the whisper from each lip
And he's here to do some business with
the bad ADC on his chip
bad ADC on his chiiiiip

hobbesmaster posted:

And how exactly is it faster? Don't point to libraries with bindings for all languages involved.


Mostly because it is easier for a compiler to prove that Fortran code operating on a matrix doesn't violate strict aliasing, so it is free to perform a couple of optimizations that would render code that violated strict aliasing incorrect. C99 code written to use the restrict qualifier on pointers judiciously can often overcome this hurdle. (You can think of the Fortran approach of EQUIVALENCE vs the C99 approach of restrict as a whitelist for aliasing and a blacklist, respectively).

Besides real reasons for Fortran to be faster than C, however, there are also stupid reasons, eg. writing a matrix operation in Fortran and C and traversing in column-major order in both and wondering why the C is so slow.

Blotto Skorzany fucked around with this message at 21:55 on Apr 11, 2013

Opinion Haver
Apr 9, 2007

Why wouldn't that be slow in Fortran as well; surely you'd run into the same cache issues? (I know nothing about Fortran).

weird
Jun 4, 2012

by zen death robot

yaoi prophet posted:

Why wouldn't that be slow in Fortran as well; surely you'd run into the same cache issues? (I know nothing about Fortran).

Fortran stores matrices in column major order.

JetsGuy
Sep 17, 2003

science + hockey
=
LASER SKATES

Doctor w-rw-rw- posted:

Hate to break it to you, but a lot of those Python libs probably have Fortran powering some part of them: http://www.scipy.org/Installing_SciPy/BuildingGeneral

Fortran is also faster than C++ and possibly C for a lot of math operations. Not going away anytime soon - it's still got that niche locked down.

At what point did I deride Fortran?

Opinion Haver
Apr 9, 2007

VanillaKid posted:

Fortran stores matrices in column major order.

Oh, well that makes sense.

Do non-programming but still software-related horrors count? Because this website is absolutely atrocious.

Volmarias
Dec 31, 2002

EMAIL... THE INTERNET... SEARCH ENGINES...

yaoi prophet posted:

Oh, well that makes sense.

Do non-programming but still software-related horrors count? Because this website is absolutely atrocious.

I love the guy with the bananas.

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

yaoi prophet posted:

Oh, well that makes sense.

Do non-programming but still software-related horrors count? Because this website is absolutely atrocious.

Haha, no video with supported mime type found. You lose HTML5 Homies!

ephphatha
Dec 18, 2009




More adventures in old and dodgy perl code. Found a script that takes user input, builds a query (by string concatenation) and executes it directly. But it's ok, because they escape quotes (in only one of the possible execution paths). This script runs as a soap service, but despite this someone copy pasted the code directly into another script running under apache on the same server. This copy doesn't sanitise user input at all.

The scripts serve to look up staff contact details based on a few parameters, one of which is location. The location isn't stored in the database so instead they try infer the location by the phone number (if it's been saved).

The client side code reads (embedded in the perl source, naturally):
code:
<html>
...
<form>
  <select name="location">
  <option value="N">New South Wales</option>
  <option value="NT">Northern Territory</option>
  <option value="T">Tasmania</option>
  ...
  </select>
</form>
...
</html>
And on the server (comments added):
code:
%locationMask = (
N => '02________', #Note, 02 includes NSW and ACT phone numbers
NT => '03________', #03 is actually for Tasmania
T => '08________', #08 is actually for Northern Territory
...
)
$sth = $dbh->prepare("select * from person_details where phone like '$locationMask{$cgiparam{'location'}}' and somecolumn like '$userinput'");
$sth->execute();
So when people tried to use this feature there was no guarantee that they would get back anything near what they requested.

QuarkJets
Sep 8, 2008

JetsGuy posted:

I loving despise IDL, and i hate the fact that I need to use it for my work. Usually, I can get around it by using Python, but I often have to use some of my team's programs, and astronomers LOVE IDL. You know how much physicists love loving FORTRAN? Yeah, astronomers love IDL like that. A part of it is that the astrolib library has been ingrained very deeply in the community. So, understandably, you are sometimes just stuck dealing with IDL. There's also just the fact that we all tend to like to code in the language we know, and scientists tend to be very much of the thought "eh, why bother" when it comes to learning new programming languages not named FORTRAN or IDL... and in some circumstances, Perl.

I'm just so loving happy that Python has been (quickly) taking over the Universe (literally :colbert: ). Many, many astronomy libraries are being written and released for Python. The young astronomer community is embracing it, and hopefully, by the time I'm a crusty old scientist with my own horde of grad students and post docs, IDL will be a distant memory.

This is all without bringing up how loving outrageously expensive the IDL licenses are. Only loving MATLAB surpasses it in terms of "hey bend over", and at least MATLAB has enough of an audience to justify their insane license costs.

FORTRAN and IDL are loved by dinosaurs; modern physicists have mostly made the switch to C++ or Python. There's a bunch of legacy poo poo written in FORTRAN, and there will always be a FORTRAN niche, but the language is quickly disappearing from actual scientific usage

(as an example, the everything at CERN is almost exclusively C++ and Python with a few things that are based in FORTRAN; for computational purposes there will be some FORTRAN libraries hanging around for a very long time, I think we all agree)

It's exactly for the reasons that you say; the older crowd wants to keep using FORTRAN and IDL because that's what they've always used, whereas the younger crowd wants to use Python and C++ because it's what they learned in school (and a lot of other reasons)

VikingofRock
Aug 24, 2008




QuarkJets posted:

FORTRAN and IDL are loved by dinosaurs; modern physicists have mostly made the switch to C++ or Python. There's a bunch of legacy poo poo written in FORTRAN, and there will always be a FORTRAN niche, but the language is quickly disappearing from actual scientific usage

(as an example, the everything at CERN is almost exclusively C++ and Python with a few things that are based in FORTRAN; for computational purposes there will be some FORTRAN libraries hanging around for a very long time, I think we all agree)

It's exactly for the reasons that you say; the older crowd wants to keep using FORTRAN and IDL because that's what they've always used, whereas the younger crowd wants to use Python and C++ because it's what they learned in school (and a lot of other reasons)

This has been my experience as well, and thank god. Although ROOT is pretty annoying sometimes, and in my experience it is used a ton by the particle physics community.

Progressive JPEG
Feb 19, 2003

The worst code I've yet encountered was produced by Astro researchers. The best theory I can come up with is that perhaps the industry is accustomed to one-off code that only needs to function until a paper gets published. This may also be why they're fine with using something like IDL in the first place.

raminasi
Jan 25, 2005

a last drink with no ice

Progressive JPEG posted:

The worst code I've yet encountered was produced by Astro researchers. The best theory I can come up with is that perhaps the industry is accustomed to one-off code that only needs to function until a paper gets published. This may also be why they're fine with using something like IDL in the first place.

Most code written by scientists is terrible for exactly this reason.

evensevenone
May 12, 2001
Glass is a solid.
Yeah but you should see the quality of astrophysics done by computer scientists.

QuarkJets
Sep 8, 2008

VikingofRock posted:

This has been my experience as well, and thank god. Although ROOT is pretty annoying sometimes, and in my experience it is used a ton by the particle physics community.

Having used both ROOT and MATLAB for many years now, you should count your lucky stars that you get to use ROOT. It has a whole bunch of horrible problems and bizarre ways of doing things done, but MATLAB is one hundred times worse and lacks a lot of the graphical power that ROOT has

(although for writing little one-off projects that just produce results, MATLAB is better; what I'm saying is that ROOT is far better for producing plots and other pretty things, whereas MATLAB is more of a result workhorse with the presentation of data thrown in as an afterthought)

Also, many of the problems with ROOT disappear if you start using PyROOT. Give that a shot

dis astranagant
Dec 14, 2006

A longstanding issue with godawful physicist code is that it's often so terrible that no one can hope to reproduce the results because of how loving fragile it is.

QuarkJets
Sep 8, 2008

Progressive JPEG posted:

The worst code I've yet encountered was produced by Astro researchers. The best theory I can come up with is that perhaps the industry is accustomed to one-off code that only needs to function until a paper gets published. This may also be why they're fine with using something like IDL in the first place.

You are exactly right. Even today most graduate physics/astronomy students have maybe one computational physics course before entering grad school, and most grad programs may only offer one additional computational course. There's not even an introduction to programming in these programs, you're told about these tools that are necessary for solving certain problems but nobody explains how to actually produce good code or anything like that. My graduate level computational class was basically just a class on Mathematica and was completely useless

All that we have is our own experience and the experience of our peers, which often isn't much to go on. Legacy code becomes untouchable because it produces the results that we expect.

CERN specifically has an advantage in that there are actual computer scientists working there alongside the physicists, and there are sometimes workshops to help people learn better coding skills. Many fields don't get that; you're in a basement lab with a bunch of other grad students who are just as clueless as you are.

QuarkJets fucked around with this message at 08:44 on Apr 13, 2013

Murodese
Mar 6, 2007

Think you've got what it takes?
We're looking for fine Men & Women to help Protect the Australian Way of Life.

Become part of the Legend. Defence Jobs.

Progressive JPEG posted:

The worst code I've yet encountered was produced by Astro researchers. The best theory I can come up with is that perhaps the industry is accustomed to one-off code that only needs to function until a paper gets published.

I do computer science research and my proof of concept code literally makes my skin crawl but I don't loving care because of the reason you just listed.


MORE PAPERS

Shugojin
Sep 6, 2007

THE TAIL THAT BURNS TWICE AS BRIGHT...


QuarkJets posted:

You are exactly right. Even today most graduate physics/astronomy students have maybe one computational physics course before entering grad school, and most grad programs may only offer one additional computational course. There's not even an introduction to programming in these programs, you're told about these tools that are necessary for solving certain problems but nobody explains how to actually produce good code or anything like that. My graduate level computational class was basically just a class on Mathematica and was completely useless

All that we have is our own experience and the experience of our peers, which often isn't much to go on. Legacy code becomes untouchable because it produces the results that we expect.

CERN specifically has an advantage in that there are actual computer scientists working there alongside the physicists, and there are sometimes workshops to help people learn better coding skills. Many fields don't get that; you're in a basement lab with a bunch of other grad students who are just as clueless as you are.

I'm in the undergrad computational (astro)physics at my university at the moment, it's what I was bitching about. It's basically 3 hours a week of being told about algorithms and getting then getting thrown headfirst into actually coding an implementation. It's pretty rough.

I've uh, implemented some code the professor has written and it's kinda godawful in a lot of ways. What I produced for this class is far from the best I've ever written and as far as I can tell I'm one of the better people who aren't the compsci guy. I don't envy the TA who grades our poo poo.

Also she loves numerical recipes which is just terrible, she has the code sometimes in slides like it's insightful and doesn't hide half the calculations in proprietary libraries.

Adbot
ADBOT LOVES YOU

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

dis astranagant posted:

A longstanding issue with godawful physicist code is that it's often so terrible that no one can hope to reproduce the results because of how loving fragile it is.

Yep. This is an issue across a lot of fields in science and is part of the reason there's a crisis of sorts with regards to reproducibility. I'm in particular thinking about the more smooshy medical/health/social sciences.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply