Why is GetLength() used at all?

MOOtantMOOtant Join Date: 2010-06-25 Member: 72158Members
I can only imagine 2 answers:
a) because NS2 devs didn't optimize it
b) because cost of Lua calls/interpreting is so high that it doesn't matter

What I mean is that (a-b).GetLength() < 2 is a lot more expensive than (a-b).GetLengthSquared() < 4.

Also, is Vector class moved to C++ or are parts of it in C++? First half of definitions in Vector.lua is commented out.

It's really easy to write:

IsDistanceLessThan(vec3 x, vec3 y, float dist)
IsDistanceLessThanOrEqual(vec3 x, vec3 y, float dist)

so there 2*2=4 part is hidden and all is even nicely readable.

Comments

  • MCMLXXXIVMCMLXXXIV Join Date: 2010-04-14 Member: 71400Members
    You're right about the performance cost side of this.

    Though in most of the cases I've seen you wouldn't get any value out of changing it to GetLengthSquared, because the calls are made so infrequently that the performance hit is negligible anyway. The maintenance cost of refactoring those kinds of calls and potentially introducing new bugs is probably higher than the performance hit you get from the couple of extra cycles.

    The most important cases are things like distance check loops that get called every tick and hence you're wasting a few cycles each frame, and I don't think I've seen any examples of that in the vanilla NS2 codebase since build 161 or so. If you do find one like this, mention it in the forums and I'm sure they'll plug the gap!
  • YuukiYuuki Join Date: 2010-11-20 Member: 75079Members
    I think they introduced a method GetVelocityLength that probably cache the velocity length. GetLengthSquared is used here and there, but it could maybe be used more systematically.
  • twilitebluetwiliteblue bug stalker Join Date: 2003-02-04 Member: 13116Members, NS2 Playtester, Squad Five Blue
    <!--quoteo(post=1978391:date=Sep 16 2012, 02:00 AM:name=Yuuki)--><div class='quotetop'>QUOTE (Yuuki @ Sep 16 2012, 02:00 AM) <a href="index.php?act=findpost&pid=1978391"><{POST_SNAPBACK}></a></div><div class='quotemain'><!--quotec-->I think they introduced a method GetVelocityLength that probably cache the velocity length. GetLengthSquared is used here and there, but it could maybe be used more systematically.<!--QuoteEnd--></div><!--QuoteEEnd-->
    I didn't know about this method. Thanks for sharing the knowledge!
Sign In or Register to comment.