MMO DEMO optimizing itemid

edited February 2011 in Photon Server
While looking through the MMO Demo code I noticed that ItemIds are generated client Side via GUIDs. This is done in the Game constructor (line 126):
this.avatar = new MyItem(Guid.NewGuid().ToString(), (byte)ItemType.Avatar, this, avatarName);

It's not even the 16 byte GUID its the full 36 byte version since its a string. That item ID is passed back in forth in a great deal of the messages, especially MOVE (Operations.cs line 266). And every MoveEvent (the event sent to every subscribing client as result of the move op) also sends it back.

One of my first changes to this code will be to change this. I really am looking at two options:

The first is the easy option. Simply change the above to Guid.NewGuid().ToByteArray(), this spits out 16 bytes as opposed to 36. I simply need to change all the method signatures of all operations to "byte[] itemid" as opposed to string itemid. I also need to reconstruct the GUID for any itemid lookups (ie game.TryGetItem(itemType, itemId, out item)) but that is easy too. It's a lot of files to hit on both the client and server but the concept is pretty straight forward and there shouldn't be any tricky logic gotchas.

The second option is harder but gives me more byte savings and implements a new op that I have wanted any way. I would need to implement a new operation for an globally unique ID counter. Any clients would have to request a new id before they generated any new item. This can be tricky because we now have to generate new items asynchronously the up side is that if you were planning to have the server be authoritative on these operations anyway (server needs to approve) than its not that big a deal. The server would just send you back the id when it approved your new item request.

This second method allows the server to maintain a thread safe counter for IDs which can be smaller than 16 bytes. I was thinking a int32 should be plenty big enough so thats another 12 bytes saved on operations and events that are performed all the time.

I would be curious what others thought and if anyone had already done this.



  • you could also have spawn item and enter world operation return an item id (response parameter instead of the request parameter). Using the Interlocked.Increment method it should be easy to generate short and unique ids.
  • Great find!

    Thanks for sharing!