commit | b7856f8e273bfab36ddd6b26fb7c8b4520e97f40 | [log] [tgz] |
---|---|---|
author | Paulo Pinheiro <paulovictor.pinheiro@gmail.com> | Fri May 26 20:00:33 2023 +0200 |
committer | GitHub <noreply@github.com> | Fri May 26 11:00:33 2023 -0700 |
tree | 51c65e567a2c623083d8da1e86c7564429705ea2 | |
parent | 0100f6a5779831fa7a651e4b67ef389a8752bd9b [diff] |
Add Kotlin multiplatform support (#7969) * [Kotlin] Introduction to Kotlin Multiplaform The first implementation of the Kotlin code generation was made years ago at the time Kotlin Multiplaform was not stable and Kotlin is mostly used on JVM-based targets. For this reason the generated code uses java based runtime. That design decision comes with many drawbacks, leaving the code generated more java-like and making it impossible to use more advanced features of the Kotlin language. In this change we are adding two parts: A pure, multi-plaform, Kotlin runtime and a new code generator to accompany it. * [Kotlin] Remove scalar sign cast from code generation Now that we have a new runtime the accepts unsigned types, we don't need to code generate casting back and from signed scalars. This MR removes this from both code generations and adds the necessary API to the runtime. * [Kotlin] Use offset on public API to represent buffer position Currently, kotlin was following Java's approach of representing objects, vectors, tables as "Int" (the position of it in the buffer). This change replaces naked Int with Offset<T>, offering a type-safe API. So, instead of fun Table.createTable(b: FlatBufferBuilder, subTable: Int) We will have fun Table.createTable(b: FlatBufferBuilder, subTable: Offset<SubTable>) Making impossible to accidentally switch parameters. The performance should be similar to use Int as we are using value class for Offset and ArrayOffset, which most of the time translate to Int in the bytecode. * [Kotlin] Add builder for tables Add builder constructor to make create of table more ergonomic. For example the movie sample for the test set could be written as: Movie.createMovie(fbb, mainCharacterType = Character_.MuLan, mainCharacter = att) { charactersType = charsType this.characters = characters } instead of: Movie.startMovie(fbb) Movie.addMainCharacterType(fbb, Character_.MuLan) Movie.addMainCharacter(fbb, att as Offset<Any>) Movie.addCharactersType(fbb, charsType) Movie.addCharacters(fbb, charsVec) Movie.endMovie(fbb) * [Kotlin] Move enum types to value class Moving to flatbuffer enums to value class adds type safety for parameters with minimum to no performance impact. * [Kotlin] Simplify Union parameters to avoid naked casting Just a small change on the APIs that receive union as parameters, creating a typealias UnionOffset to avoid using Offset<Any>. To "convert" an table offset to an union, one just call Offset.toUnion(). * [Kotlin] Apply clang-format on kotlin code generators * [Kotlin] Update kotlin generator to follow official naming conventions Updating directory, package and enum naming to follow Kotlin official convention. https://kotlinlang.org/docs/coding-conventions.html#naming-rules * [Kotlin] Add fixes to improve performance 1 - Add benchmark comparing serialization between Java & Kotlin 2 - ReadWriteBuffer does not auto-grow (thus avoid check size in every op) 3 - Add specialized add functions on FlatBufferBuilder to avoid boxing offsets. 4 - Remove a few Kotlin syntax sugar that generated performance penalties. * [Kotlin] Remove builder from Kotlin KMP and add some optimizations to avoid boxing of Offset classes --------- Co-authored-by: Derek Bailey <derekbailey@google.com>
FlatBuffers is a cross platform serialization library architected for maximum memory efficiency. It allows you to directly access serialized data without parsing/unpacking it first, while still having great forwards/backwards compatibility.
Build the compiler for flatbuffers (flatc
)
Use cmake
to create the build files for your platform and then perform the compliation (Linux example).
cmake -G "Unix Makefiles" make -j
Define your flatbuffer schema (.fbs
)
Write the schema to define the data you want to serialize. See monster.fbs for an example.
Generate code for your language(s)
Use the flatc
compiler to take your schema and generate language-specific code:
./flatc --cpp --rust monster.fbs
Which generates monster_generated.h
and monster_generated.rs
files.
Serialize data
Use the generated code, as well as the FlatBufferBuilder
to construct your serialized buffer. (C++
example)
Transmit/store/save Buffer
Use your serialized buffer however you want. Send it to someone, save it for later, etc...
Read the data
Use the generated accessors to read the data from the serialized buffer.
It doesn't need to be the same language/schema version, FlatBuffers ensures the data is readable across languages and schema versions. See the Rust
example reading the data written by C++
.
Go to our landing page to browse our documentation.
Code generation and runtime libraries for many popular languages.
FlatBuffers does not follow traditional SemVer versioning (see rationale) but rather uses a format of the date of the release.
flatbuffers
tag for any questions regarding FlatBuffers.To contribute to this project, see CONTRIBUTING.
Please see our Security Policy for reporting vulnerabilities.
Flatbuffers is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.