mzaks / FlatBuffersSwift

This project brings FlatBuffers (an efficient cross platform serialization library) to Swift.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide possibility to reuse Builder and Reader

mzaks opened this issue · comments

As discussed in #25

I think this is one of the biggest wins for repeated building&reading - e.g. the builder has a bunch of properties that are objects and needs to be set up / allocated regardless of whether cache is in use - e.g.

    public var cache : [ObjectIdentifier : Offset] = [:]
    public var inProgress : Set<ObjectIdentifier> = []
    public var deferedBindings : [(object:Any, cursor:Int)] = []

Would be nice to add something like

    public static func fromReader(r : FlatBufferReader) -> FooBarContainer {
        let reader = r
        let objectOffset = reader.rootObjectOffset
        return create(reader, objectOffset : objectOffset)!
    }

and 

    public func toByteArray (b :FlatBufferBuilder) -> [UInt8] {
        let builder = b
        let offset = addToByteArray(builder)
        performLateBindings(builder)
        return try! builder.finish(offset, fileIdentifier: nil)
    }

and corresponding reader&builder 'reset' methods.

A lazy instance would probably want to keep its reader-per-instance though (can't think of something decent there right now otherwise).

Addressed second half in #40 - I think we can close this one after that one is integrated.