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

Overview

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
   
     {
        Ok(TestOut {
            a : "test".into(),
        })
    }
  

   
  1. build again, and try it out in Kotin
{ 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") } } ); ">
RustCore.navHelper.test(
            Proto.TestIn.newBuilder().setA("aa").setB("bb").build(),
            object : RustCore.Callback
   
     {
                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

You might also like...
Glommio Messaging Framework (GMF) is a high-performance RPC system designed to work with the Glommio framework.

Glommio Messaging Framework (GMF) The GMF library is a powerful and innovative framework developed for facilitating Remote Procedure Calls (RPCs) in R

A CLI application which allows you to archive Urbit channels and all linked content in them.
A CLI application which allows you to archive Urbit channels and all linked content in them.

The Urbit Content Archiver is a small CLI application that exports channels from your Urbit ship and auto-downloads any directly linked content locall

Taking the best of Substrate Recipes and applying them to a new framework for structuring a collection of how-to guides.

Attention: This repository has been archived and is no longer being maintained. It has been replaced by the Substrate How-to Guides. Please use the Su

All the data an IC app needs to make seamless experiences, accessible directly on the IC. DAB is an open internet service for NFT, Token, Canister, and Dapp registries.
All the data an IC app needs to make seamless experiences, accessible directly on the IC. DAB is an open internet service for NFT, Token, Canister, and Dapp registries.

DAB 👀 Overview An Internet Computer open internet service for data. All the data an IC app needs to make a seamless experience, accessible directly o

Cloup is a template manager that provides the files you desire when you need them, written in Rust.
Cloup is a template manager that provides the files you desire when you need them, written in Rust.

cloup cloup is a template manager that delivers the files you desire when you need them, written in rust you no longer need to locate your previous pr

Teach best practices by violating them.

Substrate Node Template A fresh FRAME-based Substrate node, ready for hacking 🚀 Getting Started Follow the steps below to get started with the Node T

Manage lockfiles in PKGBUILDs for upstreams that don't ship them, `updpkgsums` for dependency trees (Arch Linux tooling)

updlockfiles Manage lockfiles for packages that don't ship any upstream. Like updpkgsums but for 3rd party dependency trees. If you're not actively ma

flow-rust-sdk utilizes the Flow gRPC AccessAPI to make requests on the flow blockchain.

Welcome to the Flow-Rust-SDK! We're glad to have you here. There are a few important items that we should cover real quick before you dive in. Version

Initial project for using Rust on the Nintendo 64.

N64 Project Template for Rust This repo is intended to be a starting point for developing software for the Nintendo 64 console using Rust. Only the ba

Owner
woo
woo
Prototype risk modeling simulation for Portfolio using Arbiter.

proto-sim Prototype simulation using Arbiter as the simulation & agent engine. Build & Run build.sh cargo run Arbiter config The arbiter.toml config

Primitive 13 Aug 14, 2023
Narrowlink securely connects devices and services together, even when both nodes are behind separate NAT

Narrowlink Narrowlink is a self-hosted platform that allows you to establish secure remote connections between devices within a network that may be hi

Narrowlink 355 Aug 17, 2023
Prototype: ORAM and related for Intel SGX enclaves

mc-oblivious Traits and implementations for Oblivious RAM inside of Intel SGX enclaves. The scope of this repository is: Traits for fast constant-time

MobileCoin 37 Nov 23, 2022
A prototype implementation of the Host Identity Protocol v2 for bare-metal systems, written in pure-rust.

Host Identity Protocol for bare-metal systems, using Rust I've been evaluating TLS replacements in constrained environments for a while now. Embedded

null 31 Dec 12, 2022
A fast tool to scan prototype pollution vulnerability written in Rust. 🦀

ppfuzz Prototype Pollution Fuzzer A fast tool to scan prototype pollution vulnerability written in Rust. ?? Installation Binary Source Dependencies Us

Dwi Siswanto 410 Dec 27, 2022
Implementation of Sunny's Mesh Security talk (Hackathon / Prototype status)

mesh-security (Hackathon / Prototype status) An implementation of Sunny's Mesh Security talk from Cosmoverse 2022. This should run on any CosmWasm ena

CosmWasm 83 Apr 17, 2023
NIP-41 "HD" Key Invalidation Prototype

NIP-41 "HD" Key Invalidation Prototype NIP-41 is a proposal for a scheme whereby a Nostr identity key can be invalidated to a new one safely. nostr-pr

optout 3 Apr 19, 2023
Exploratory work on abigen in rust for Starknet 🦀

Starknet abigen for rust bindings This exploratory work aims at generating rust bindings from a contract ABI. Before the first release, we are termina

null 9 Sep 11, 2023
a handy utility to work with encrypted DMGs

edmgutil edmgutil is a simple wrapper utility to hdiutil to help you work with disposable, encrypted DMGs. It can decompress an encrypted ZIP into a n

Sentry 9 Nov 29, 2022
legitima is a work in progress LDAP provider for ORY Hydra.

legitima is a work in progress LDAP provider for ORY Hydra. Together with it, it can be used as an OpenID Connect (OIDC) provider to authenticate to any OIDC capable apps.

leona 3 Aug 1, 2022