Fast (de)serialization on iPhone
I'm developing a game/engine for iPhone OS. It's the first time I'm using Objective-C.
I made my own binary format for geometry data and for textures I'm focusing on PVRTC. That should be the optimal approach as far as speed and space are concerned. I really want to keep loading time to a minimum and - if possible - be able to save very fast as well.
So now I'm trying to make my "Entity" stuff persistent without sacrificing performance.
First I wanted to use NSKeyedArchiver. From what I've heard, it's not very fast. Also, what I want to serialize is mostly structs made of floats with some ints and strings, so there isn't really a need for all that "object graph" overhead.
NSArchiver would have been more appropriate, but they kicked that off the iphone for some reason.
So now I'm thinking about making my own serialization scheme again. Am I wrong in thinking that NSKeyedArchiver is slow (I only read that, haven'开发者_高级运维t tested it myself)? If so, what's the best way to encode/decode structs (with no pointers, mostly floats) without sacrificing speed?
Don't bother optimizing something that you haven't tested... it might be "good enough". One hint; the binary format is about 2x as fast and nearly 2x as memory efficient as the XML format.
Beyond that... well... you get into all kinds of case specific issues. Do you need all the data at once? Need it to be streamed? Arbitrary subsets? How connected is it? Will it be reconstituted into a single or many ObjC objects? Is the data pre-canned? etc.etc.etc...
I've had good results using yajl-objc for both serialization and communicating with REST-style web services; it is efficient enough for my needs and simple to use.
That being said, unless you have a ton of data NSKeyedArchiver
will likely do just fine.
精彩评论