forked from google/flatbuffers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Kotlin multiplatform support (google#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 <[email protected]>
- Loading branch information
1 parent
635843f
commit 1c0ef21
Showing
43 changed files
with
4,601 additions
and
363 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,7 @@ java: | |
kotlin: | ||
- '**/*.kt' | ||
- src/idl_gen_kotlin.cpp | ||
- src/idl_gen_kotlin_kmp.cpp | ||
|
||
lua: | ||
- '**/*.lua' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -422,9 +422,14 @@ jobs: | |
with: | ||
distribution: 'temurin' | ||
java-version: '11' | ||
- name: Build flatc | ||
run: | | ||
cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . | ||
make -j | ||
echo "${PWD}" >> $GITHUB_PATH | ||
- name: Build | ||
working-directory: kotlin | ||
run: ./gradlew clean iosX64Test macosX64Test | ||
run: ./gradlew clean iosSimulatorArm64Test macosX64Test macosArm64Test | ||
|
||
build-kotlin-linux: | ||
name: Build Kotlin Linux | ||
|
@@ -437,6 +442,11 @@ jobs: | |
distribution: 'temurin' | ||
java-version: '11' | ||
- uses: gradle/[email protected] | ||
- name: Build flatc | ||
run: | | ||
cmake -DFLATBUFFERS_BUILD_TESTS=OFF -DFLATBUFFERS_BUILD_FLATLIB=OFF -DFLATBUFFERS_BUILD_FLATHASH=OFF . | ||
make -j | ||
echo "${PWD}" >> $GITHUB_PATH | ||
- name: Build | ||
working-directory: kotlin | ||
# we are using docker's version of gradle | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Example IDL file for our monster's schema. | ||
|
||
namespace jmonster; | ||
|
||
enum JColor:byte { Red = 0, Green, Blue = 2 } | ||
|
||
union JEquipment { JWeapon } // Optionally add more tables. | ||
|
||
struct JVec3 { | ||
x:float; | ||
y:float; | ||
z:float; | ||
} | ||
|
||
table JMonster { | ||
pos:JVec3; | ||
mana:short = 150; | ||
hp:short = 100; | ||
name:string; | ||
friendly:bool = false (deprecated); | ||
inventory:[ubyte]; | ||
color:JColor = Blue; | ||
weapons:[JWeapon]; | ||
equipped:JEquipment; | ||
path:[JVec3]; | ||
} | ||
|
||
table JWeapon { | ||
name:string; | ||
damage:short; | ||
} | ||
|
||
table JAllMonsters { | ||
monsters: [JMonster]; | ||
} | ||
|
||
root_type JAllMonsters; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Example IDL file for our monster's schema. | ||
|
||
namespace monster; | ||
|
||
enum Color:byte { Red = 0, Green, Blue = 2 } | ||
|
||
union Equipment { Weapon } // Optionally add more tables. | ||
|
||
struct Vec3 { | ||
x:float; | ||
y:float; | ||
z:float; | ||
} | ||
|
||
table Monster { | ||
pos:Vec3; | ||
mana:short = 150; | ||
hp:short = 100; | ||
name:string; | ||
friendly:bool = false (deprecated); | ||
inventory:[ubyte]; | ||
color:Color = Blue; | ||
weapons:[Weapon]; | ||
equipped:Equipment; | ||
path:[Vec3]; | ||
} | ||
|
||
table Weapon { | ||
name:string; | ||
damage:short; | ||
} | ||
|
||
table AllMonsters { | ||
monsters: [Monster]; | ||
} | ||
|
||
root_type AllMonsters; |
This file was deleted.
Oops, something went wrong.
68 changes: 68 additions & 0 deletions
68
...nchmark/src/jvmMain/kotlin/com/google/flatbuffers/kotlin/benchmark/FlatbufferBenchmark.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
@file:OptIn(ExperimentalUnsignedTypes::class) | ||
|
||
package com.google.flatbuffers.kotlin.benchmark | ||
|
||
|
||
import com.google.flatbuffers.kotlin.FlatBufferBuilder | ||
import jmonster.JAllMonsters | ||
import jmonster.JMonster | ||
import jmonster.JVec3 | ||
import monster.AllMonsters.Companion.createAllMonsters | ||
import monster.AllMonsters.Companion.createMonstersVector | ||
import monster.Monster | ||
import monster.Monster.Companion.createInventoryVector | ||
import monster.MonsterOffsetArray | ||
import monster.Vec3 | ||
import org.openjdk.jmh.annotations.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
@State(Scope.Benchmark) | ||
@BenchmarkMode(Mode.AverageTime) | ||
@OutputTimeUnit(TimeUnit.NANOSECONDS) | ||
@Measurement(iterations = 20, time = 1, timeUnit = TimeUnit.NANOSECONDS) | ||
open class FlatbufferBenchmark { | ||
|
||
val repetition = 1000000 | ||
val fbKotlin = FlatBufferBuilder(1024 * repetition) | ||
val fbJava = com.google.flatbuffers.FlatBufferBuilder(1024 * repetition) | ||
|
||
@OptIn(ExperimentalUnsignedTypes::class) | ||
@Benchmark | ||
fun monstersKotlin() { | ||
fbKotlin.clear() | ||
val monsterName = fbKotlin.createString("MonsterName"); | ||
val items = ubyteArrayOf(0u, 1u, 2u, 3u, 4u) | ||
val inv = createInventoryVector(fbKotlin, items) | ||
val monsterOffsets: MonsterOffsetArray = MonsterOffsetArray(repetition) { | ||
Monster.startMonster(fbKotlin) | ||
Monster.addName(fbKotlin, monsterName) | ||
Monster.addPos(fbKotlin, Vec3.createVec3(fbKotlin, 1.0f, 2.0f, 3.0f)) | ||
Monster.addHp(fbKotlin, 80) | ||
Monster.addMana(fbKotlin, 150) | ||
Monster.addInventory(fbKotlin, inv) | ||
Monster.endMonster(fbKotlin) | ||
} | ||
val monsters = createMonstersVector(fbKotlin, monsterOffsets) | ||
val allMonsters = createAllMonsters(fbKotlin, monsters) | ||
fbKotlin.finish(allMonsters) | ||
} | ||
|
||
@Benchmark | ||
fun monstersjava() { | ||
fbJava.clear() | ||
val monsterName = fbJava.createString("MonsterName"); | ||
val inv = JMonster.createInventoryVector(fbJava, byteArrayOf(0, 1, 2, 3, 4).asUByteArray()) | ||
val monsters = JAllMonsters.createMonstersVector(fbJava, IntArray(repetition) { | ||
JMonster.startJMonster(fbJava) | ||
JMonster.addName(fbJava, monsterName) | ||
JMonster.addPos(fbJava, JVec3.createJVec3(fbJava, 1.0f, 2.0f, 3.0f)) | ||
JMonster.addHp(fbJava, 80) | ||
JMonster.addMana(fbJava, 150) | ||
JMonster.addInventory(fbJava, inv) | ||
JMonster.endJMonster(fbJava) | ||
}) | ||
val allMonsters = JAllMonsters.createJAllMonsters(fbJava, monsters) | ||
fbJava.finish(allMonsters) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.