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

Overview

FlatBuffersSwift

Join the chat at https://gitter.im/mzaks/FlatBuffersSwift Build Status

Motivation

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

One minute introduction

There are three simple steps for you to use FlatBuffersSwift

1. Write a schema file

table List {
  people : [Person];
}

table Person {
  firstName : string;
  lastName : string;
}

root_type List;

2. Generate Swift code

fbsCG console application can be found here: https://github.com/mzaks/FlatBuffersSwiftCodeGen To generate, please execute it as following: fbsCG contacts.fbs contacts.swift

3. Use the generated API

Create objects and encode them

let p1 = Person(firstName: "Maxim", lastName: "Zaks")
let p2 = Person(firstName: "Alex", lastName: "Zaks")
let list = List(people: [p1, p2])
let data = try?list.makeData()

Decode data very efficiently

let newList = List.from(data: data)
let name = newList?.people[0].firstName

Please check out Wiki for more information.

Comments
  • Large number of transient memory allocations during Eager test run

    Large number of transient memory allocations during Eager test run

    Just run an analysis of 1M Eager run to see malloc profile - can see a number of nice multiples of 1M small allocations, adding this issue as a placeholder for digging into if some could be eliminated (even though it is just transient allocations, they seems to be the root cause holding back performance).

    Attaching screenshot from run. malloc

    question 
    opened by hassila 29
  • Swift API fine tuning and initial documentation of reader API

    Swift API fine tuning and initial documentation of reader API

    Some start on polish for: https://github.com/mzaks/FlatBuffersSwift/issues/55 https://github.com/mzaks/FlatBuffersSwift/issues/75

    for your consideration - if you are ok with the direction, I will spend some more time on it.

    opened by hassila 18
  • Review and tweak API using Swift guidelines

    Review and tweak API using Swift guidelines

    Creating this as a reminder:

    It would probably make sense to make a final API cleanup iteration when 'all' functionality is in plac3 considering https://swift.org/documentation/api-design-guidelines/ for even nicer 'swiftyness'.

    opened by hassila 15
  • Migrate to Swift 3

    Migrate to Swift 3

    How would you like to approach this, using a branch, or simply going ahead in master?

    Expecting new shiny tools tomorrow due to WWDC...

    Personally, I wouldn't mind migrating to S3 in master as things are still a bit in flux (and preferably looking at support for the package manager), but doing a swift3 branch is also more than completely fine of course. Thoughts?

    Also related to #55.

    enhancement 
    opened by hassila 14
  • Review and summary of thread safety of pooling

    Review and summary of thread safety of pooling

    Ok, I had a look at the thread safety of the current pooling - and its a mixed bag...

    1. Currently the reader&builder pools are not safe to be used from multiple threads - tried to fix it and added the most lightweight synchronization I could find (objc_sync_enter/exit) and it has a significant impact on the streaming test.

    Pooling is still clearly beneficial, but if adding the sync, it is a definite issue hampering performance.

    1. The general instance pools are safe to be used from one thread per instance-type (that is, if one had two different .fbs they could be used in parallel from two threads, as the caches are separated per type) - but it is in general the same issue as above

    To summarize Enabling the pooling has a significant improvement for single-thread usage Adding naive synchronization kills much, but not all of the advantage of using builder&reader pools Adding it seems to completely kill table instance pool improvements

    Next steps

    1. Can address it either by documenting allowed usage of pooling (with more formal constraints on what is allowable and possibly looking at restructuring/alternate API).
    2. Alternatively, we can add synchronization and always be safe (but significantly slower).
    3. Thirdly, we could have two possible modes so one must explicitly enable multi-thread mode.

    I'll see if I can find any more suitable solutions but would be interesting to hear your thoughts @mzaks.

    I would probably personally lean toward 1 or 3 (or a combination thereof), as pooling is opt-in, one can always choose not to use it - for those applications that only access the data model from the .fbs from one thread, it really does help, but I think it is important that the out of the box behavior is safe but one can opt in.

    opened by hassila 13
  • Get rid of FlatBufferReader and reimplement LazyAcces

    Get rid of FlatBufferReader and reimplement LazyAcces

    #46 shows that by using structs and direct reading from buffer we can achieve fast performance with nice API. Getting rid of FlatBuffersReader should also benefit the eager implementation without the need of instance pooling. The FlatBuffersReader.swift file should only contain the pure functions and no class definition.

    opened by mzaks 12
  • Structs embedded in structs does not work

    Structs embedded in structs does not work

    I had a look at implementing the benchmark used by google for flat buffers using the .fbs from https://github.com/google/flatbuffers/blob/benchmarks/benchmarks/cpp/FB/bench.fbs

    This yielded an empty struct 'Foo' when processed thusly:

    `solid:FlatBuffersPerformanceTestDesktop jocke$ java -jar ../fbsCG.jar -fbs bench.fbs -out bench.swift -lang swift ERROR (org.eclipse.xtext.diagnostics.Diagnostic.Syntax) 'mismatched input 'id' expecting '}'' on Struct maxim.zaks.flatBuffers.impl.StructImpl@3301500b (name: Foo), lineNumber 25, length 2

    'bench.swift' has been generated. `

    Attaching the generated swift file as well. bench.swift.txt

    bug 
    opened by hassila 9
  • Codegen should support structs as root type

    Codegen should support structs as root type

    I believe the scheme must have a root object that is a table?

    It would be nice to optionally generate a struct also for the top level for small payload messages where by value semantics would be desirable. An option for the codegen to generate a struct for the root object would be nice to support this.

    opened by hassila 8
  • Document deserialization modes

    Document deserialization modes

    There are 3 ways to access data from binary:

    • Class.fromByteArray(),
    • Class.LazyAccess()
    • and Class.Fast().

    The wiki explains that the whole object is not deserialized by using LazyAcess. But what about Fast? How does it differ from the others?

    It'd be nice to document somewhere the differences between them.

    Anyway, thanks for your work! 👍

    question 
    opened by tamastimar 7
  • Allow explicit use of builders and access to raw data buffers

    Allow explicit use of builders and access to raw data buffers

    See commit comment https://github.com/hassila/FlatBuffersSwift/commit/017740e97039d19334b2396b8eb464ec5add321a for details, but the TL;DR, "10% encoding performance by optional manual reuse of builders + added raw access to buffer data when needed".

    bench.swift diffs outlines the minor codegen changes needed to support it.

    opened by hassila 7
  • Cast from 'X' to unrelated type 'T' always fails

    Cast from 'X' to unrelated type 'T' always fails

    Hey, at first thank you a lot for providing Flatbuffers support in Swift, I really like the library.

    Currently I have an issue where I am using a Union type but I am not able to decode the Union type correctly.

    Flatbuffers file:

    table A {}
    table B {}
    union Test {
      A,
      B
    }
    
    table Root{
      test:Test;
    }
    
     if let test = root.test {
      switch test {
      case let test as A:
        print("A")
      case let test as B:
        print("B")
      default:
        print("none")
      }
    }
    

    If I try to convert test as A or B I get the following error message: "Cast from 'Test' to unrelated type 'A' always fails" and it indeed fails and it prints "none". Do you have a suggestion why this could be the case?

    I am using Swift v4.2

    Thanks in advance :)

    question 
    opened by alexanderwe 5
  • Which flatc spec is the code generated on?

    Which flatc spec is the code generated on?

    Hello,

    I woiuld liek to understand on which spec of Flatbuffer (flatc) compiler is the current implementation based on? I cannot find any such information or maybe I am missing something. Can someone please give some pointer?

    Basically Current Flatbuffer & flatc gen is 1.11.0 and what is this repo generates the code?

    opened by amitahire 0
  • Support new union syntax

    Support new union syntax

    union Character {
      MuLan: Attacker,  // Can have name be different from type.
      Rapunzel,         // Or just both the same, as before.
      Belle: BookReader,
      BookFan: BookReader,
      Other: string,
      Unused: string
    }
    
    opened by mzaks 0
  • Swift 4 and Codable

    Swift 4 and Codable

    Hi Maxim, I really like the concept of FlatBuffers 😍 With Swift 4 you can create your own Encoder and Decoder to support the new codable feature of Swift 4. This will allow FlatBuffers to be used on a larger scale. Here some resources to help you:

    FlatBuffers should also be available on most environments. For that it would have to be a cross-platform component. 😉 To create a cross-platform component your can follow my tutorial Swift Cross Platform Framework or directly use my tool XcodeTool - Create a new component cross-platform

    opened by TofPlay 1
Owner
Maxim Zaks
Maxim Zaks
Command line tool written in Swift dedicated to perform Mutation Testing of your Swift project

Mutanus Command line tool written in Swift dedicated to perform Mutation Testing of your Swift project. Inspired by Muter Usage mutanus -c <path-to-co

Iurii Sorokin 36 Sep 21, 2022
A prctice project by DevProjects...

NewsApp A prctice project by DevProjects... A mobile app to consume the News API and display a list of news articles. Clicking one of the news article

Ahmed Abaza 3 Jun 8, 2022
A project that uses the Flickr image search API and shows the results in a 3-column scrollable collection view

FlickrImagesDemo FlickrImagesDemo is a project that uses the Flickr image search API and shows the results in a 3-column scrollable collection view. 


प्रणय पवार 0 Dec 9, 2021
iTunesSearch: a screenshot listing project that using iTunes Search API

iTunesSearch iTunesSearch is a screenshot listing project that using iTunes Search API. This project written in Swift with MVVM architecture. Installa

null 0 Dec 14, 2021
[Deprecated] A shiny JSON parsing library in Swift :sparkles: Loved by many from 2015-2021

?? Deprecation Notice ?? Gloss has been deprecated in favor of Swift's Codable framework. The existing Gloss source is not going away, however updates

Harlan Kellaway 1.6k Nov 24, 2022
Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift.

Himotoki Himotoki (紐解き) is a type-safe JSON decoding library written purely in Swift. This library is highly inspired by the popular Swift JSON parsin

IKEDA Sho 799 Dec 6, 2022
Swift library for JSON-RPC

JSONRPC There are already a bunch of packages out there for doing JSON-RPC in Swift. This one is just very simple and makes no assumptions about the t

Chime 16 Dec 30, 2022
A type-safe JSON-RPC 2.0 library purely written in Swift

JSONRPCKit JSONRPCKit is a type-safe JSON-RPC 2.0 library purely written in Swift. // Generating request JSON let batchFactory = BatchFactory(version:

Shinichiro Oba 178 Mar 18, 2022
A JSON deserialization library for Swift

Mapper Mapper is a simple Swift library to convert JSON to strongly typed objects. One advantage to Mapper over some other libraries is you can have i

Lyft 1.2k Dec 29, 2022
A library of Swift extensions to turbocharge your iOS development.

Alexandria A library of Swift extensions to turbocharge your iOS development. Background Here at Oven Bits, we love Swift. We started using it when it

Oven Bits 34 Apr 15, 2022
SwiftIB is a pure Swift implementation of the Interactive Brokers TWS API library on Mac OS X

SwiftIB is a pure Swift implementation of the Interactive Brokers TWS API library on Mac OS X, with Core Foundation of crouse. All the API interfaces are implemented. Request Market Data and Request History Data interface are thoroughly tested.

Harry Li 24 Sep 14, 2022
Developed with use Swift language. As a third party library used SDWebImage. JSON parsing using URLSession with TMDB API. This app provide by the Core Data structure.

Capstone Project ?? About Developed with use Swift language. As a third party library used SDWebImage. JSON parsing using URLSession with TMDB API. Ad

Ensar Batuhan Unverdi 9 Aug 22, 2022
Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend

Argo is a library that lets you extract models from JSON or similar structures in a way that's concise, type-safe, and easy to extend. Using Argo

thoughtbot, inc. 3.5k Dec 20, 2022
Library of Swiftui Views conforming to Codable, meaning we can produce JSON driven UI!

CodableView Library of Swiftui Views conforming to Codable, meaning we can produce JSON driven UI! Adding a CodableView Type Create View that conforms

Daniel Bolella 3 Apr 2, 2022
🧱 A JSON decoding/encoding library that handles optimistically or strictly.

Do you need to handle the root cause of failure in decoding JSON? We often process the value as a default value if it could not be decoded from JSON.

Muukii 252 Oct 28, 2022
A library to turn dictionary into object and vice versa for iOS. Designed for speed!

WAMapping Developed and Maintained by ipodishima Founder & CTO at Wasappli Inc. Sponsored by Wisembly A fast mapper from JSON to NSObject Fast Simple

null 8 Nov 20, 2022
Library Genesis iOS Client

LibraryGenesis This application is showing books from Library Genesis. If you found this project useful leave a star ⭐️ Features Search books with tit

MartinStamenkovski 37 Dec 19, 2022
The only Connect API library you'd ever need

ConnectKit A Swift client for Infinite Flight's Connect APIs, built using the Network framework. Supported APIs IF Session Discovery Connect API V2 Op

Alexander Nikitin 7 Dec 14, 2022
JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift.

JSEN /ˈdʒeɪsən/ JAY-sən JSEN (JSON Swift Enum Notation) is a lightweight enum representation of a JSON, written in Swift. A JSON, as defined in the EC

Roger Oba 8 Nov 22, 2022