Showing newest posts with label programming. Show older posts
Showing newest posts with label programming. Show older posts

Friday, December 25, 2009

Manifest gotchas

For those of you using Visual Studio 2008, and using it’s C++ TR1 support in SP1, here’s something I wish I hadn’t spent so much time tracking down..

TR1 features only exist in 2008 SP1, but the linker will happily build a manifest to link against the pre-SP1 CRT even if you’re using TR1. Some systems (including your development machines) will have proper side-by-side library redirection from the old CRT to the new one. But machines with the old runtime installed won’t fail to load the DLL, they’ll just mysteriously fail to find symbols in it. For example, one machine failed to find std::tr1::weak_ptr and gave the following error:

The procedure entry point ?_Xweak@tr1@std@@YAXXZ could not be located in the dll MSVCP90.dll

The solution is to force your project’s manifest to specify the lastest CRT version by defining the following macro:

_BIND_TO_CURRENT_CRT_VERSION
Microsoft’s full explanation is here: http://msdn.microsoft.com/en-us/library/cc664727.aspx

Saturday, April 28, 2007

"Scripting Languages" vs "High Level Languages"

Recently, Joe wrote in his blog:
As I see it there are three kinds of languages in this world:
  1. Hard to write, but blazingly fast: C and C++, or even assembly if you’re really hard-core
  2. Easy to write, but so slow that you have to use them sparingly: PHP, Lua, Python, Perl
  3. Flash
Java fits into an awkward niche between 1 and 2. It’s easier to develop in than C++, but not enough to keep up with the scripting languages, and yet it’s far too slow to write the whole game in. Add to that the incredible pain of forcing your users to install the Java VM, and you get a non-starter of a language.
This seems to be the common perception of high level languages lately – languages like Python* are considered “scripting” languages: they’re not compiled like Java or C#, and they have a “simpler” syntax. I’ll use Python for terms of discussion because of my familiarity with it, but I’m sure the same is true for languages like Perl and Ruby.
Implementation
All too often, on the GameDev forums, I read people’s posts claiming that an interpreted language could never be as fast as a language that compiled to a native binary format like C#. So that we’re all on the same page, let’s get this out of the way at the beginning: “managed code”, “language runtime”, and “virtual machine” are all concepts on par with “interpreted language.” Sure, the first three imply a level of “smartness” by the interpreter – compilation of the language into some interpretable bytecode, and perhaps a JIT compiler – but any modern interpreted language should be doing these things. We’ve come a long way from the days of parsing text and executing it.
The fact that Java and C# need a compilation phase is merely an interpretation difference. I like being able to change code on the fly and have the interpreter recompile it into bytecode for me. Other people like having the extra type-safety and syntax checking pass of a compile step. Personally, though, I think these class of bugs get much less frequent with a language like Python.
Runtime Speeds
The Computer Language Shootout does consistently list Java and C# (albeit, on Mono) as faster than Python. In Python’s defense, the benchmarks never have Psyco – the Python JIT compiler – enabled. Running a few of these tests myself using Psyco, I saw a speedup of at least 4x running with the most basic braindead use of Psyco. This still puts Python slightly behind Java and C# for speed, but not by much. Of course, many of these tasks are heavily numeric – something I would hope you’d implement in C and wrap for use in Python if speed were a concern.
Development Speeds
I find myself able to develop applications much faster in Python than in C++, due to the nature of the syntax. (As I over-excitedly told Joe over lunch one day, “orders of magnitude faster!!”) I’ve heard many other developers tell me that it’s impossible to manage anything more than a large project in a high-level language, or that once it comes down to writing production quality code the speed gain is lost.
I’ll step away from the “Python as an example of high-level languages” here for a moment, and talk about Python specifically. A lot of claims against Python’s ability to deal with large projects comes from its dynamic typing. I think a lot of misconceptions come from this – it’s important not to confuse dynamic typing with weak typing. Python is a dynamically- and strongly-typed: references (variables are all references) can point to any type, but data keeps its type and will not be automatically cast to fit the current expression.**
As far as development speeds on production-quality code and suitability for large projects, I’ve found both of these statements to be completely untrue in practice. At a previous job, I worked on large python applications. In several cases, we decided to re-write some of the server-side code in C++ to gain some speed. (Audio processing and mixing on a server processing audio for many clients in any number of audio conferences, for example.) These pieces would take several times longer to write than the original Python modules.
I think a lot of the misconception about production-quality code being slower comes from people who have experiences with Java or C#. In these C-like languages, developers still need to jump through a lot of the syntactic hoops that C++ developers do, but still have the cost of needing a language runtime. The difference from C++ ends up being that you can use a few neat language tricks to hack together code quickly, that’s not “production quality.”
Development Environments
One place Python and other high-level languages fall down compared to Java and C# are in the development tools. Developers who are used to Visual Studio like that you can seamlessly step from C++ to C# code in the debugger. Java users have Eclipse. (Yes, Python users have Eclipse too, through the PyDev plugin, which is quite nice.) There is no well-integrated tool for Python debugging across language boundaries. On Linux, you could set up GDB to recognize Python code, but that’s a huge hassle – and Linux specific.
I don’t think the types of errors you need a strong debugger for come up quite as much in a high-level language. Pointers don’t suddenly go NULL. You can’t mysteriously clobber your vtable. But that’s not to say that a debugger isn’t an invaluable tool. WinPDB is a great tool, but especially in game development, there are going to be times when you need to step into the C++ source for a module you wrote.
Conclusion
As Joe mentions in his article, I think most developers are stuck in their own little bubbles. I certainly have been surprised lately to find out that Java has become much faster and can now be used for non-ugly GUI applications with SWT. Hopefully, I’ve encouraged you to at least look in the direction of high-level languages if you never have. Every tool has its uses, and I think you’ll find that high-level languages are better for a lot more than text processing these days. And C or C++ probably isn’t the best choice for everything that it used to be. But if you’re still writing that text-processing tool in C, you might need to expand your bubble.
* Yes, I’m biased towards Python, I know. I love Python. Whenever anyone in the office mentions anything about languages other than C++, all heads turn to me, expecting my Pytho-vangelism. But there is a reason I love Python; there are several, in fact. I’ll go into them in a later post to keep this footnote short.
** I say ‘usually’ because Python is about as strongly-typed as C when it comes to primitive types. Different numeric formats will usually be automatically casted to the appropriate type for an expression. The expression “x = 5.5 + 10” is valid, “x = 5 + ‘32’” is not.

Tuesday, May 2, 2006

Using Jabber and XMPP in Games

This entry will be slightly technical. I give it a rating of 2 out of 5 monospaced-font stars: **

Introduction

Jabber is an XML-based communication and presence1 protocol originally created to be an open-standards instant messaging network. (Google Talk is a Jabber chat service.) Jabber was later formalized into the XMPP protocol by the IETF.
I think XMPP has a huge number of applications just waiting to be discovered, and could be used for a unified system of data communication, publishing and delivery. I also think many game developers reinvent the wheel for chat systems in online games. I see two huge applications for Jabber in game development: as an in-game chat system and as a high-level inter-server communication channel and administration control interface.

In-Game Chat and Beyond

Imagine that, as a player of an online game, you could chat easily with people not only on your own server cluster but on any server cluster for that game. Imagine that people outside the game could chat with people logged into the game. Imagine that, from your desktop IM client, you could see your in-game contact list, see who was logged into just the chat network, and who was actually logged in from inside the game. Now, imagine being a game developer and having to do very little work to implement that chat system.
Using Jabber as a chat server for an online game, the developer would merely have to use some of the XMPP libraries already available as the back-end for their in-game chat interface. Exposing this client to connections from the outside internet would allow players to log in with standard Jabber clients. Server-to-server restrictions would have to apply, of course, to cut down on chat spam and other undesirable behavior.

Server-to-Server and Administration Channel

In a server cluster, different servers need to discover each other, accept administrative commands, and report their status to each other and to the administrator. All of this is possible with XMPP (as well as the XMPP Service Discovery protocol, described in JEP-0030). Servers could register themselves with a central (internal) Jabber server when they come online, and discover all other existing servers in that cluster (as well as what types of servers they are) using service discovery. Servers would then simply implement message handlers for different types of XMPP messages, depending on what their communication needs are. Administrative commands would be handled in the exact same fashion.
1 Think of presence basically as an enumeration of possible status states for a particular entity. In instant-messaging applications, this is used as a user's status: available, away, idle, offline, etc. It seems simplistic, but the helpful thing about presence in a communication protocol is that it's event-based. If you come online, I get alerted - I don't have to continuously ask if you're around.