Giter Site home page Giter Site logo

linecode / kotlinrustproto Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gaxxx/kotlinrustproto

0.0 1.0 0.0 149 KB

a prototype project integrating jni rust into Kotlin and using protobuf to make them work together

License: MIT License

Kotlin 38.00% Rust 47.43% Batchfile 0.09% Python 14.37% Shell 0.11%

kotlinrustproto's Introduction

KotlinRustProto

a prototype project integrating jni rust into Kotlin and using protobuf to make them work together

How to start

add a RPC call in DroidBackendService

service DroidBackendService {
    // add a new rpc call here
    rpc Test(TestIn) returns (TestOut);
    
}

message TestIn {
  string A = 1;
  string B = 2;
}

message TestOut {
  string A = 1;
}

Do the small implementation work

  1. try building and wait, or go get a coffee to get the compiling failure
  2. implement the rust code
// import new type
use crate::proto::TestOut;

impl DroidBackendService for Backend {

// add new implementation
fn test(&self, input: TestIn) -> BackendResult<TestOut> {
        Ok(TestOut {
            a : "test".into(),
        })
    }
  
  1. build again, and try it out in Kotin
RustCore.navHelper.test(
            Proto.TestIn.newBuilder().setA("aa").setB("bb").build(),
            object : RustCore.Callback<Proto.TestOut> {
                override fun onSuccess(arg: Proto.TestOut) {
                    // since we've set it as "test" 
                    if (arg.a != "test") {
                        throw RuntimeException("ooops in onSuccess")
                    }
                }

                override fun onErr(code: Int, msg: String) {
                    throw RuntimeException("ooops in onErr")
                }
            }
        );

What's more

My first goal is to embed a high performance kvstore into Kotlin, namely lmdb, sled, etc. Then it goes to what it likes now after some iterations and could do some more. here is how I make it work step by step...

As it works in this demo run each function for 10000 times, and here is the result, in my real phone (Xiaomi 10).

Function time used
empty jni call 3ms
Java Hashmap set 200ms
Java Hashmap get 90ms
Jni cocurrent Hashmap set 200ms
Jni cocurrent Hashmap get 116ms
Jni cocurrent Hashmap with Proto encoding / decoding 200ms
MMKV write 189ms
MMKV read 98ms
Sled write without Proto 200ms
Sled write with Proto 347ms
Sled read with Proto 200ms
Lmdb write with Proto 4300ms
Lmdb read with Proto 192ms
SharedPrefence write 48550ms
SharedPrefence read 81ms
  1. MMKV is the fastest solution, really close to native hashmap.
  2. Protobuf encoding / decoding is a bottleneck. It may takes an extra 100ms. On that note, sled is quite close to the MMKV if we don't count the protobuf thing in. (Sled write without proto)
  3. Sled outperforms SharedPrefence & Lmdb, a lot. Of course, rust is stable enough, but to put it into real productions, there is more work to do, something like
  • add multiprocess support
  • space error handling

As a toy project, it works. Maybe with protobuf, the performance is not good enough. But on the other hand, with protobuf support, we could build some core features in Rust and integrate them with other languages like swift, flutter etc.

happy hacking

kotlinrustproto's People

Contributors

gaxxx avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.