Skip to content

Commit b7856f8

Browse files
paulovapdbaileychess
andauthoredMay 26, 2023
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>
1 parent 0100f6a commit b7856f8

File tree

43 files changed

+4599
-361
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+4599
-361
lines changed
 

‎.github/labeler.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ java:
4848
kotlin:
4949
- '**/*.kt'
5050
- src/idl_gen_kotlin.cpp
51+
- src/idl_gen_kotlin_kmp.cpp
5152

5253
lua:
5354
- '**/*.lua'

‎.github/workflows/build.yml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,14 @@ jobs:
422422
with:
423423
distribution: 'temurin'
424424
java-version: '11'
425+
- name: Build flatc
426+
run: |
427+
cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF .
428+
make -j
429+
echo "${PWD}" >> $GITHUB_PATH
425430
- name: Build
426431
working-directory: kotlin
427-
run: ./gradlew clean iosX64Test macosX64Test
432+
run: ./gradlew clean iosSimulatorArm64Test macosX64Test macosArm64Test
428433

429434
build-kotlin-linux:
430435
name: Build Kotlin Linux
@@ -437,6 +442,11 @@ jobs:
437442
distribution: 'temurin'
438443
java-version: '11'
439444
- uses: gradle/wrapper-validation-action@v1.0.5
445+
- name: Build flatc
446+
run: |
447+
cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF .
448+
make -j
449+
echo "${PWD}" >> $GITHUB_PATH
440450
- name: Build
441451
working-directory: kotlin
442452
# we are using docker's version of gradle

0 commit comments

Comments
 (0)