It pays to have the source!

Armitage

Banned
Feb 23, 2001
8,086
0
0
Or at least CONFIRM that libraries you use are thread safe if you're doing threaded development!

I was recently looking through the source of a 3rd party library I use to see how they did a certain calculation. Hmmm - what are these static variables here for???

Turns out they stored the results in static variables so that they could be returned immediately if the function was called again with the same inputs! It's a fairly expensive set of calculations, and it's pretty likely that it could be called multiple times with the same input, so maybe not a bad idea if you're single threaded.

But it would have really bit me in the ass in my threaded program and been nearly impossible to track down. It would have been one of those bugs that claimed what's left of my hair :p

Having the source also let me modify the library to take long doubles for a parameter that previously only took doubles and was leading to some unacceptable precision errors.
 

jman19

Lifer
Nov 3, 2000
11,225
664
126
Originally posted by: Armitage
Or at least CONFIRM that libraries you use are thread safe if you're doing threaded development!

I was recently looking through the source of a 3rd party library I use to see how they did a certain calculation. Hmmm - what are these static variables here for???

Turns out they stored the results in static variables so that they could be returned immediately if the function was called again with the same inputs! It's a fairly expensive set of calculations, and it's pretty likely that it could be called multiple times with the same input, so maybe not a bad idea if you're single threaded.

But it would have really bit me in the ass in my threaded program and been nearly impossible to track down. It would have been one of those bugs that claimed what's left of my hair :p

Having the source also let me modify the library to take long doubles for a parameter that previously only took doubles and was leading to some unacceptable precision errors.

I had a really similar problem happen to me as well... was using some classes that were NOT thread safe and I had a hell of a time until I went into the source code, added some debug info, recompiled... eventually realized that there were a whole bunch of static variables defined that was killing my code :p
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
I was working for a company this summer that had a webframework done by contractors specifically for them years ago (back in the day before there were nice frameworks for java). Even though they paid for one-off, custom development and had the rights to it, they never managed to obtain the source code (pretty stupid).

So I was messing around with downloads of generated reports or something like that which was pretty busted throughout the entire site and it came down to something that the contractors hadn't anticipated. I knew where (and probably what) the problem was but I didn't have the source. Being java, however, I was able to simply decompile and read the class files which was great. Of course you lose the comments but they did shite for comments anyways. At least seeing what was being done, I was able to hack around it and if I'd been really determined I could have modified the decompiled source and recompiled it. Yay for java!
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
At work we use C++, Win32, MFC, and several components: lib, DLL, OCX, and some with c++ source.

It can be really annoying trying to debug problems in the lib and OCX since they don't come with source. Not being able to set a breakpoint or watch variables is bad, and not being able to fix bugs or add a hack to work around them is worse.

Unfortunately writing equivalent code to the components from scratch would take person-years, so we just have to live with it.

The C++ components are great: I've added custom cells to the grid and metafile scaling to the image library. Having source is nifty-keen!
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Markbnj
They should have just stuck them in thread local storage.

I suppose you could have - I've never worked with that myself. I think the better way would be to simply provide an intermediate function that provides the bits that are expensive to calculate, and let the user cache them for succesive calls as needed.

I've found that most scientists and engineers don't have a strong CompSci background (non compsci of course). So stuff like this isn't even on the radar for them - they don't know anything about threads/thread safety etc. I did email the maintainer and explain the problem and gave them my work-around and they're going to look into it for the next revision :thumbsup:
 

oboeguy

Diamond Member
Dec 7, 1999
3,907
0
76
The other nice thing about using open-source libraries and the like is that if they are actively developed, you can join the mailing list for help, to request features or even suggest improvements. Oh wait, that's what Armitage did, well done! :D
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I think the better way would be to simply provide an intermediate function that provides the bits that are expensive to calculate, and let the user cache them for succesive calls as needed.

Makes sense. More of a pipeline approach. Design-wise, nearly all blemishes of this sort can be dealt with by properly factoring the object model.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: oboeguy
The other nice thing about using open-source libraries and the like is that if they are actively developed, you can join the mailing list for help, to request features or even suggest improvements. Oh wait, that's what Armitage did, well done! :D

Yep - it's not quite open source. Actually I'm not sure what the license is exactly - it may just be public domain. That's something else I have to ask It comes out of one of the govenment labs. But yea, same idea - much more accesible then many commercial libraries. Of course, a much smaller userbase as well, so you're not going to get swamped if you put your personal email address out there as a contact.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0
Originally posted by: Markbnj
I think the better way would be to simply provide an intermediate function that provides the bits that are expensive to calculate, and let the user cache them for succesive calls as needed.

Makes sense. More of a pipeline approach. Design-wise, nearly all blemishes of this sort can be dealt with by properly factoring the object model.

Well, that's the thing - these guys all think in FORTRAN. Most of this code is pretty much a straight translation from fortran.