Addiction to soft coding...

Started by
14 comments, last by coder84 17 years ago
Quote:Lua would be just as slow, or even slower, its not a problem with C# specifically.

The question really is : would it be too slow ?

"Every time an object appears" doesn't sound too bad. (unless you have hundreds of thousands of objects appearing every frame)

Oh, and when i said Areas i meant parts of the code. Even an MMO have some areas of the code that will run thousands of times each second (or maybe even millions of times / second), those are the areas where you want to avoid reflection.

On an entity (Monster, NPC, etc) that is updated a few times / second or less it is hardly a big issue.

For an MMO your main concern will be bandwidth, not CPU time. (You might want to avoid heavy reflection use on the client though as you want the game to run smoothly on as old hardware as possible)


Well, thats a relief. Talking of hundreds of thousands will only appear while sending and receiving messages from and to the server (where no reflection at all is used). Remember that the creation of objects only happens in the server-side, so joining everyone they may make some thousands... It may need a bit more of processing power, but it worths it.
Advertisement
Quote:Original post by coder84Well, thats a relief. Talking of hundreds of thousands will only appear while sending and receiving messages from and to the server (where no reflection at all is used). Remember that the creation of objects only happens in the server-side, so joining everyone they may make some thousands... It may need a bit more of processing power, but it worths it.


Think one step further.

How often do values change? Do you, in an MMO, update every single creature at every step of game loop?

Most time will be spent serializing the data. This is simply due to exponential increase in number of messages that need to be sent.

In addition, even in active areas, do objects really change frequently?

So even if you use reflection, you can always cache (as blobs) serialized form of an object. And if that object changes, just discard the blob. If you only use this blob 3 times, you've probably saved enough CPU to offset benefits of introspection.

What about world updates?

Which attributes are very likely to change every loop? Location, orientation, health and energy. That's it. Those should be hard-coded. Everything else can be fully introspectable.

You level up once every few hours. You examine 1 object every 5 seconds. Gain an ability every few hours. Acquire a badge every few hours.

Inventory and other containers are handled differently anyway - container is once again fixed, but its contents are dynamic.

The general rule is that IO is the bottleneck. In 3D graphics, in networking, in disk access, even RAM (L1,L2 cache). IO will kill you, never processing power.

So the greatest drawback of using introspection would be losing locality of your data, hence taking cache miss performance decrease. But then again, in an MMO with hundreds of thousands of objects dynamically instantiated and destroyed, that locality will be small to begin with.

And once you consider all the different interactions you need to provide in such game, hardcoding anything is essentially a death sentence.

Just developing a better range search will help you increase the performance by a large factor.
So... Is it OK to use reflection or is it totally bad? ( I didn't understand =/ )

If its bad, what do you suggest without sacrificing versatility and flexibility?
Quote:Original post by coder84
So... Is it OK to use reflection or is it totally bad? ( I didn't understand =/ )

If its bad, what do you suggest without sacrificing versatility and flexibility?


It is not bad, my original reply was to kazes assumption that it would perform almost as well as statically linked classes. (it doesn't unless you only use the basic parts (then it can get quite close but isn't really that flexible))

Versatility and flexibility comes at a price, Reflection is one of the cheapest ways of getting it(it is cheaper than interpreted scripts) but it is still far from free.

My advice is to make things as flexible as you want it first, then profile it to see if it is too expensive and reduce flexibility in the areas where the cost is too great compared to the benefit. (In a large project the benefits are huge)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
One thing that I don't really get, Coder84, is how much of the AI Behavior are you actually scripting. You should use stuff like variables to control the behavior:

Your example was a wolf that, instead of attacking the player, would just stand there. Then why not add an "agressiveness" variable to your class so you can control whether it does attack or not? You can follow with this line of thought: add a variable to control almost every behavior of your entity, so you script only what is really necessary.

Then, if that is not enough use scripting to "override" the default behavior of your entity. I think the rule of thumb here should be, if you are going to have a certain class/behavior instantied too many times in your game, then have it hardcoded. Specializations or specific stuff that happens only every once in a while can be put aside and cached during the load or even on the fly.

I believe that even goes to textures and models. You can speedup a bit of loading time by hardcoding a model that is used a 1000 times on your application. The tradeoff is that it'll take longer to open your application (since it's executable is fully loaded into memory before execution), and you also lose memory and control over where will your data-structure be (parts of executables can be paged and stored on the pagefile, I think).

And another sugestion: you can use precompiled assemblies and use then as "plugins", the same way that dlls goes on C++. So, that's the same as having scripts in C#, but the difference is that you compile everything before actual run, which speeds loading time and execution performance. Link with then through reflection during run-time so you still gain flexibility.

If you follow my last suggestion, I suggest you think about a 4 layered application (or think that your application 2nd layer have two sub-layers):

1. Engine Core
2. Customization Layer
2.1 Hardcoded Customization (base class to use inside your MMO)
2.2 External Customization (derived classes declared inside assemblies)
3. Scripts

This way you can balance performance and flexibility. If you think a certain NPC or monster have to act in a very specific way, just add a script to it. If that monsters is going to be instantiated 1000 times each 10 minutes, consider moving the script to an assembly. If the monster will be used in future games or can be used as samples, you can move it up to the Hardcoded Customization layer.

Becareful not to overcrowd layer 2.1 or you might endup inflating your executable. Load 2.2 only during runtime.
-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-Senior Programmer - iMAX Games
Great ideas! Now I understand a bit more how this works.

You are right about when to use scripting and when to hard code. If I want to make an object to act in a very specific way I'll override that hard code with a script. If I'll use that script a lot of times, I should move it to an assembly.

Quote:And another sugestion: you can use precompiled assemblies and use then as "plugins", the same way that dlls goes on C++. So, that's the same as having scripts in C#, but the difference is that you compile everything before actual run, which speeds loading time and execution performance. Link with then through reflection during run-time so you still gain flexibility.


When you talk about precompiled assemblies you mean .cs files? It would be a good idea. I load them before the actual game starts and I have both, performance and versatility improved. (Sorry if I misunderstood).

This topic is closed to new replies.

Advertisement