Java Object Serialization Performance tips

后端 未结 9 1211
别那么骄傲
别那么骄傲 2021-02-01 11:15

I must serialize a huge tree of objects (7,000) into disk. Originally we kept this tree in a database with Kodo, but it would make thousands upon thousands of Queries to load t

9条回答
  •  说谎
    说谎 (楼主)
    2021-02-01 11:40

    This is how I would do it, form the top of my head

    Serialization

    1. Serialize each object individually
    2. Assign each object a unique key
    3. When an object holds a reference to another object, put the unique key for that object in the objects place in the serialization. (I would use an UUID converted to binary)
    4. Save each object into a file/database/storage using the unique key

    Unserialization

    1. Start form an arbitrary object (usually the root i suspect) unserialize it and put it in a map with it's unique key as index and return it
    2. When you step on an object key in the serialization stream, first check if it's already unserialized by looking up it's unique key in the map and if it is just grab it from there, if not put a lazy loading proxy (which repeats these two steps for that object) instead of the real object which has hooks to load the right object when you need it.

    Edit, you might need to use two-pass serialization and unserialization if you have circular references in there, it complicates things a bit - but not that much.

提交回复
热议问题