Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inital Blocking "RPC" Proof of Concept #87

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

alvinosh
Copy link
Collaborator

No description provided.

Copy link

Benchmark for ac44f9c

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 248.2±2.68µs 249.3±0.81µs +0.44%
blake3_resource_id_creation/compute_from_bytes:medium 15.6±0.07µs 15.7±0.24µs +0.64%
blake3_resource_id_creation/compute_from_bytes:small 1375.6±17.15ns 1387.7±2.51ns +0.88%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 197.0±0.54µs 197.4±0.64µs +0.20%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1720.4±4.18µs 1726.7±13.95µs +0.37%
crc32_resource_id_creation/compute_from_bytes:large 86.6±0.60µs 86.5±0.27µs -0.12%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.01µs 5.4±0.03µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.9±0.30ns 93.1±0.67ns +0.22%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 64.7±0.36µs 64.3±0.30µs -0.62%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 979.2±2.34µs 973.6±9.53µs -0.57%
resource_index/index_build//tmp/ark-fs-index-benchmarksHyRbKS 110.2±1.69ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarkstKdt3L 108.3±4.04ms N/A N/A
resource_index/index_get_resource_by_id 97.0±0.41ns 97.2±0.43ns +0.21%
resource_index/index_get_resource_by_path 56.2±0.40ns 53.2±0.29ns -5.34%
resource_index/index_update_all 1100.2±39.36ms 1137.1±42.03ms +3.35%

Copy link

github-actions bot commented Sep 2, 2024

Benchmark for 47b2a50

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 247.8±0.48µs 249.6±1.35µs +0.73%
blake3_resource_id_creation/compute_from_bytes:medium 15.7±0.04µs 15.6±0.04µs -0.64%
blake3_resource_id_creation/compute_from_bytes:small 1389.7±7.85ns 1382.8±9.46ns -0.50%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 197.0±0.39µs 197.7±0.88µs +0.36%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1745.4±11.61µs 1764.7±8.52µs +1.11%
crc32_resource_id_creation/compute_from_bytes:large 86.7±0.52µs 86.7±0.23µs 0.00%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.03µs 5.4±0.03µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 93.0±0.47ns 93.1±0.75ns +0.11%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 65.8±1.34µs 65.3±0.33µs -0.76%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 998.7±10.16µs 989.7±6.67µs -0.90%
resource_index/index_build//tmp/ark-fs-index-benchmarks3Y9tLv 114.6±6.06ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarkscqvV6R 114.1±5.14ms N/A N/A
resource_index/index_get_resource_by_id 97.7±1.21ns 97.5±0.60ns -0.20%
resource_index/index_get_resource_by_path 53.0±1.13ns 55.5±1.51ns +4.72%
resource_index/index_update_all 1161.4±44.70ms 1185.0±45.48ms +2.03%

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Rust crate should be located directly in ark-rust/nonblock-rpc not in /rust

Copy link
Member

@kirillt kirillt Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, we might need split the crate in 2 parts: one in this repo, one in ark-android repo.

On other hand, we have some bindings in this repo.

@tareknaser what should be final structure of the crate?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the crate name, maybe ark-rpc or just rpc?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what should be final structure of the crate?

I think we should keep the bindings and the code that generates them in the same repo, since they depend on each other. For example, the command to build the bindings is:

cargo run --bin uniffi-bingen generate --library ./target/debug/rpc_core.dll --language kotlin --out-dir ../android/app/src/main/java/ark/rpc_core

This relies on the Rust code being built in the current folder, so any changes to the code would affect the bindings. That's why they should stay in the same Git repo.

I see two options:

  • Keep all the Rust code in ark-core/nonblock-rpc and the bindings in ark-core/nonblock-rpc/android.
  • Move the crate and bindings to a new repo. The crate can still depend on other ark-core crates as external dependencies.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the crate name, maybe ark-rpc or just rpc?

I think rpc, block-rpc, or non-block-rpc would work well.
All the crates in this repository are related to ARK, it doesn't need ark in the name.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think RPC makes more sense since in the future we want this one crate to support both blocking and non-blocking requests

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a different name like dispatcher or function registry will be more appropriate since this a single process method dispatch. The same name can be used for both sync and async.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also include it in the ark-rust Cargo.toml file so it's part of the cargo workspace.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ark-rust is the older name of this repo, new one is ark-core. Just for clarity, technically we can split the repos by languages again. I don't remember why we decided to keep java folder here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember why we decided to keep java folder here.

Because they are bindings to the Rust code in this repository. For example, here, we are using the compiled Rust code to build the bindings:


    // Set the JVM argument for the java.library.path (To import rust compiled library)
    val rustLibPath = projectDir.resolve("../../target/release").absolutePath

Keeping the bindings in the same crate allows us to treat everything as one bundle of code. For example, we want the CI to run Java bindings checks for each PR in this repo, and that wouldn’t be possible if the bindings were in a different repository (as far as I know).

Another approach would be to move the code that generates the bindings in Rust to a separate repository that depends on ark-core. For instance, files in the fs-storage/src/jni folder could be moved to their own repository, with an external dependency on ark-core.
The benefit of this is better organization, as we would divide the repositories by language. However, the downside is that we would have to maintain them separately. Any changes to the API in ark-core would need to be manually propagated in the other repository.

Comment on lines 1 to 6
[workspace]

members = [
"core",
"uniffi-bindgen",
]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This setup creates nested Rust workspaces, which is generally discouraged.

Do you think there's a way to refactor this? Perhaps uniffi-bindgen could be included as a binary within the same crate.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the build guide! Could we also add some usage instructions and perhaps an example to show the crate in action?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for including the full integration test with Kotlin. Having a couple of pure Rust tests that use the Router will also be very helpful. They will also be useful in quickly testing out changes. One other thing to consider is to include tests that call HandlerFunctions with different number of arguments like 3-4.

### Build the dylib

```sh
cargo build
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we consider adding the --release flag as well?

Suggested change
cargo build
cargo build --release

Some(handler) => {
handler.call(data)
},
None => "Unknown function".to_string(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're currently suppressing errors here. This method should return Result<String>, where the error type is a uniffi error type instead.

Comment on lines 29 to 34
#[derive(Serialize)]
pub struct Response<T> {
pub result: Option<T>,
pub error: Option<String>,
pub is_success: bool,
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fields of Response<T> shouldn't be public.

Comment on lines 21 to 26
pub fn call(&self, name: &str, args: Vec<String>) -> String {
match self.routes.get(name) {
Some(handler) => handler.call(args),
None => "Unknown function".to_string(),
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unknown function should be wrapped in an error type. The call() method should return Result<String> instead.

pub is_success: bool,
}

const CATASTROPHIC_ERROR: &str = "{\"result\": null, \"error\": \"CATASTROPHIC_ERROR: Failed to serialize response\", \"is_success\": false}";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

‘CATASTROPHIC_ERROR’ sounds pretty intense 😃
Is there a specific reason for using such a strong term?

By the way, I think this might be something that could be serialized into JSON.

use crate::ROUTER;

#[uniffi::export]
pub fn call(path: String, data: Vec<String>) -> String {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think the term 'path' might be a bit confusing here. Perhaps 'route' would be a better fit.

Copy link

Benchmark for f9a3451

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 250.7±1.03µs 247.8±0.51µs -1.16%
blake3_resource_id_creation/compute_from_bytes:medium 15.6±0.04µs 15.5±0.07µs -0.64%
blake3_resource_id_creation/compute_from_bytes:small 1360.2±1.86ns 1360.3±11.99ns +0.01%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 197.8±3.22µs 197.8±2.02µs 0.00%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1770.0±20.33µs 1771.1±22.31µs +0.06%
crc32_resource_id_creation/compute_from_bytes:large 86.7±0.17µs 87.0±1.12µs +0.35%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.21µs 5.4±0.01µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.6±1.84ns 93.0±3.13ns +0.43%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 64.6±0.68µs 64.7±1.05µs +0.15%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 950.3±4.58µs 958.2±9.24µs +0.83%
resource_index/index_build//tmp/ark-fs-index-benchmarks1m3iyz 112.3±0.67ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarksD2wolS 110.3±1.47ms N/A N/A
resource_index/index_get_resource_by_id 98.0±0.66ns 97.1±0.50ns -0.92%
resource_index/index_get_resource_by_path 52.8±0.43ns 53.1±0.71ns +0.57%
resource_index/index_update_all 1106.9±33.00ms 1133.6±34.25ms +2.41%
resource_index/index_update_one 662.9±18.69ms 663.0±17.99ms +0.02%

Copy link

Benchmark for a2e89ec

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 252.0±1.28µs 251.6±2.36µs -0.16%
blake3_resource_id_creation/compute_from_bytes:medium 15.6±0.03µs 15.6±0.04µs 0.00%
blake3_resource_id_creation/compute_from_bytes:small 1367.0±2.14ns 1361.9±10.50ns -0.37%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 200.7±0.39µs 196.8±3.14µs -1.94%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1721.2±6.37µs 1708.8±15.11µs -0.72%
crc32_resource_id_creation/compute_from_bytes:large 86.7±0.41µs 86.7±0.69µs 0.00%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.05µs 5.4±0.03µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.3±0.20ns 92.4±0.70ns +0.11%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 64.3±0.41µs 64.6±2.34µs +0.47%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 948.0±5.95µs 938.0±6.41µs -1.05%
resource_index/index_build//tmp/ark-fs-index-benchmarksb5uJjN 112.9±1.23ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarksmeviTU 110.0±2.23ms N/A N/A
resource_index/index_get_resource_by_id 191.2±2.98ns 98.7±0.48ns -48.38%
resource_index/index_get_resource_by_path 143.8±0.54ns 54.0±0.75ns -62.45%
resource_index/index_update_all 1155.6±45.15ms 1159.3±54.97ms +0.32%
resource_index/index_update_one 687.2±34.93ms 689.8±27.28ms +0.38%

Copy link

Benchmark for f5947fa

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 249.6±0.56µs 249.2±0.94µs -0.16%
blake3_resource_id_creation/compute_from_bytes:medium 15.7±0.07µs 15.6±0.05µs -0.64%
blake3_resource_id_creation/compute_from_bytes:small 1360.0±9.01ns 1366.7±1.99ns +0.49%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 197.8±0.38µs 197.2±4.13µs -0.30%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1716.2±25.81µs 1707.2±14.87µs -0.52%
crc32_resource_id_creation/compute_from_bytes:large 86.8±1.04µs 86.7±0.31µs -0.12%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.02µs 5.4±0.02µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.3±0.20ns 92.3±0.23ns 0.00%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 64.1±0.21µs 64.4±0.47µs +0.47%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 940.8±4.76µs 938.0±6.16µs -0.30%
resource_index/index_build//tmp/ark-fs-index-benchmarksCJGHl1 109.1±1.15ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarksjftE4T 110.5±1.12ms N/A N/A
resource_index/index_get_resource_by_id 97.6±0.27ns 97.9±0.64ns +0.31%
resource_index/index_get_resource_by_path 53.9±2.59ns 53.8±0.32ns -0.19%
resource_index/index_update_all 1109.8±28.70ms 1130.7±50.95ms +1.88%
resource_index/index_update_one 681.4±20.57ms 679.4±19.54ms -0.29%

Copy link

Benchmark for 61336a4

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 249.9±0.86µs 250.5±0.47µs +0.24%
blake3_resource_id_creation/compute_from_bytes:medium 15.7±0.06µs 15.6±0.04µs -0.64%
blake3_resource_id_creation/compute_from_bytes:small 1366.6±4.08ns 1370.2±17.33ns +0.26%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 196.8±0.62µs 196.6±0.50µs -0.10%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1690.3±13.21µs 1696.6±18.69µs +0.37%
crc32_resource_id_creation/compute_from_bytes:large 87.0±0.49µs 86.8±0.22µs -0.23%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.01µs 5.4±0.03µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.7±2.53ns 92.5±0.80ns -0.22%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 64.5±0.29µs 64.5±0.66µs 0.00%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 936.8±7.66µs 937.2±4.23µs +0.04%
resource_index/index_build//tmp/ark-fs-index-benchmarksrhcwNi 114.0±1.06ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarksvcDbk7 110.9±2.23ms N/A N/A
resource_index/index_get_resource_by_id 104.3±0.90ns 98.0±0.32ns -6.04%
resource_index/index_get_resource_by_path 80.1±0.93ns 54.7±1.71ns -31.71%
resource_index/index_update_all 1118.7±35.33ms 1131.4±40.64ms +1.14%
resource_index/index_update_one 674.9±19.77ms 688.0±20.10ms +1.94%

Copy link

Benchmark for 56bcdbb

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 251.4±0.81µs 254.4±1.52µs +1.19%
blake3_resource_id_creation/compute_from_bytes:medium 15.7±0.04µs 15.7±0.07µs 0.00%
blake3_resource_id_creation/compute_from_bytes:small 1358.7±7.48ns 1358.5±7.00ns -0.01%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 195.3±1.12µs 195.5±1.27µs +0.10%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1689.5±5.95µs 1698.9±19.55µs +0.56%
crc32_resource_id_creation/compute_from_bytes:large 87.4±0.34µs 87.5±0.21µs +0.11%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.02µs 5.4±0.03µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.9±0.85ns 92.9±0.47ns 0.00%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 62.9±0.31µs 63.1±1.08µs +0.32%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 958.8±5.37µs 950.8±4.55µs -0.83%
resource_index/index_build//tmp/ark-fs-index-benchmarks2KcKWW 98.6±1.72ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarksKkMdEn 99.6±1.48ms N/A N/A
resource_index/index_get_resource_by_id 98.6±0.21ns 98.5±0.26ns -0.10%
resource_index/index_get_resource_by_path 54.1±0.63ns 54.2±0.77ns +0.18%
resource_index/index_update_all 1077.6±46.86ms 1087.1±61.58ms +0.88%
resource_index/index_update_one 659.6±30.28ms 655.2±30.40ms -0.67%

Copy link

Benchmark for 6f83905

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 251.5±0.81µs 253.6±1.44µs +0.83%
blake3_resource_id_creation/compute_from_bytes:medium 15.8±0.05µs 15.8±0.18µs 0.00%
blake3_resource_id_creation/compute_from_bytes:small 1366.4±6.04ns 1359.7±9.29ns -0.49%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 196.3±0.27µs 196.7±1.25µs +0.20%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1689.4±2.44µs 1692.8±17.04µs +0.20%
crc32_resource_id_creation/compute_from_bytes:large 86.7±0.54µs 86.6±0.36µs -0.12%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.02µs 5.4±0.04µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.4±0.95ns 92.3±0.46ns -0.11%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 64.2±0.18µs 64.1±0.38µs -0.16%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 934.6±4.22µs 935.7±9.98µs +0.12%
resource_index/index_build//tmp/ark-fs-index-benchmarkseMs7gg 106.2±1.51ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarksjX9MVq 105.0±1.59ms N/A N/A
resource_index/index_get_resource_by_id 98.0±0.41ns 98.5±1.24ns +0.51%
resource_index/index_get_resource_by_path 54.4±3.36ns 53.7±0.46ns -1.29%
resource_index/index_update_all 1090.5±32.40ms 1125.2±38.13ms +3.18%
resource_index/index_update_one 677.5±21.56ms 666.7±19.62ms -1.59%

Copy link

Benchmark for 24c5c06

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 250.2±0.90µs 251.3±0.64µs +0.44%
blake3_resource_id_creation/compute_from_bytes:medium 15.6±0.08µs 15.6±0.06µs 0.00%
blake3_resource_id_creation/compute_from_bytes:small 1368.1±7.49ns 1366.2±2.18ns -0.14%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 196.6±0.53µs 197.3±0.69µs +0.36%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1693.8±11.70µs 1693.8±17.11µs 0.00%
crc32_resource_id_creation/compute_from_bytes:large 86.9±0.32µs 86.9±0.34µs 0.00%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.02µs 5.4±0.02µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 103.0±0.93ns 92.4±0.50ns -10.29%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 64.4±0.20µs 64.4±0.57µs 0.00%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 957.1±3.73µs 943.9±12.60µs -1.38%
resource_index/index_build//tmp/ark-fs-index-benchmarksKc64OU 112.8±1.01ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarkspvV9YW 114.6±1.13ms N/A N/A
resource_index/index_get_resource_by_id 98.2±0.54ns 98.3±0.40ns +0.10%
resource_index/index_get_resource_by_path 53.7±0.51ns 54.2±0.83ns +0.93%
resource_index/index_update_all 1157.4±42.89ms 1169.4±50.53ms +1.04%
resource_index/index_update_one 686.8±32.91ms 700.1±28.86ms +1.94%

@twitu
Copy link
Collaborator

twitu commented Sep 18, 2024

@kirillt suggested I take a look at this for a review. I do not have full context on it's implementation, so please help me in my understanding. Having implemented something similar previously, it seems to be a "client-server" like architecture where kotlin code is the client and can send requests to the rust server which can send back responses. The benefit of this approach is that it greatly simplifies dealing with generics because request/responses are strings or vec of strings.

I have some points to offer to enhance the understand-ability and maintainability of this module.

  • Perhaps renaming it to function registry or method dispatcher will be a better fit. Because it is NOT RPC. RPC implies that the procedures are being called in a different address space i.e. a different process. Here the client and server exist in the same process, so calling it RPC can create confusion later on.
  • It would be more appropriate to rename HandlerFunction to Callback because that is what a callback is. A pointer to a function stored on the heap. This will also help differentiate it from FunctionHandler struct 😄
  • There are two traits Handler and HandlerFunction which have the same methods but different type constraints. I'm not sure what the difference is between the two. Making it clear with some documentation comments will help.
  • The struct that actually stores the callback in this case the FunctionHandler would be more appropriately named as RequestHandler, since it is a handler that is triggered by a request from the client and internally calls a callback.

These terms have standard meanings which will make it much easier to understand and follow the logic. Of course some documentation will also help.

I found the impl_handler_function very useful and it should be documented well because it will come in handy if the callbacks need to be extended for more arguments.

@kirillt
Copy link
Member

kirillt commented Sep 19, 2024

@twitu good writeup, thanks. Yes, your understanding about client-server architecture is correct, and indeed we aim at applying it to a probably simpler task, where both sides are located not only on same device but also in same process.

I like your idea for the naming, "dispatch" sounds like a better word. Regarding "callback" term: technically, you are correct but it also might be an oversimplification. I like the terminology taken from Web, with "requests" and "responses". Especially, because we're aiming on async handling as well. Although when we implement subscription pattern, such a terminology could stand in our way.

Could you please split this list into smaller comments and leave them on related lines, so it would be easier to discuss and address?

Copy link
Collaborator

@twitu twitu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've left a few comments on naming and design. If this works well then it's possible that we don't need JNI bindings for individual storages. The storages can be registered to this dispatcher and the application and can send "requests" to the dispatcher and get back "responses". This will also eliminate the problem of exposing generic bindings.

Is the plan to replace JNI bindings with this style?

}
}
}
pub trait HandlerFunction<Marker>: Send + Sync + 'static {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming this to Callback will be more appropriate because it represents a function that will be boxed and put on the heap and called with some value.

Moreover, having a different name will reduce confusion between it and FunctionHandler.

impl_handler_function!(T0, T1, T2, T3);
impl_handler_function!(T0, T1, T2, T3, T4);

struct FunctionHandler<F, Marker> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renaming this to RequestHandler will be more appropriate since it will accept a request and pass it to a "callback". It will also help differentiate it from HandlerFunction which can renamed as Callback.

}
}

pub trait Handler {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this is doing and how it is different from HandlerFunction.

impl Router {
pub fn new() -> Self {
Router {
routes: HashMap::new(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pub-sub pattern can be supported by adding a routing table here which is a HashMap that stores the topic and a Vec of callbacks - HashMap<String, Vec<HandlerFunction>>. And exposing publish and subscribe, unsubscribe that allow publishing data and modifying subscriptions.

use serde::{Deserialize, Serialize};

pub struct Router {
pub routes: HashMap<String, Box<dyn Handler + 'static + Send + Sync>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider running the Router in a separate thread and using a single-thread per-core runtime like tokio-uring. This will make the code and types significantly less complex - you will be able to remove 'static, Send and Sync while still using an async runtime.

https://emschwartz.me/async-rust-can-be-a-pleasure-to-work-with-without-send-sync-static/

}
}

macro_rules! impl_handler_function {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some documentation for this will be very helpful.

Copy link

Benchmark for aeeb442

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 250.1±0.57µs 250.7±1.02µs +0.24%
blake3_resource_id_creation/compute_from_bytes:medium 15.7±0.20µs 15.7±0.06µs 0.00%
blake3_resource_id_creation/compute_from_bytes:small 1370.1±7.27ns 1373.5±1.84ns +0.25%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 195.0±0.52µs 194.9±0.78µs -0.05%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1685.8±7.70µs 1692.6±19.41µs +0.40%
crc32_resource_id_creation/compute_from_bytes:large 87.2±0.56µs 87.3±0.97µs +0.11%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.05µs 5.4±0.04µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.8±0.43ns 92.9±0.50ns +0.11%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 62.4±0.35µs 62.5±0.26µs +0.16%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 934.4±10.22µs 937.1±6.42µs +0.29%
resource_index/index_build//tmp/ark-fs-index-benchmarksS0HmIL 104.1±1.17ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarkspCffli 102.6±1.09ms N/A N/A
resource_index/index_get_resource_by_id 98.0±0.25ns 99.5±0.64ns +1.53%
resource_index/index_get_resource_by_path 54.1±0.62ns 54.2±0.83ns +0.18%
resource_index/index_update_all 1048.9±37.91ms 1074.3±48.10ms +2.42%
resource_index/index_update_one 637.7±21.50ms 640.7±24.10ms +0.47%

Copy link

Benchmark for 1967138

Click to view benchmark
Test Base PR %
blake3_resource_id_creation/compute_from_bytes:large 249.9±0.64µs 252.6±0.48µs +1.08%
blake3_resource_id_creation/compute_from_bytes:medium 15.7±0.06µs 15.7±0.08µs 0.00%
blake3_resource_id_creation/compute_from_bytes:small 1374.1±5.17ns 1371.2±14.01ns -0.21%
blake3_resource_id_creation/compute_from_path:../test-assets/lena.jpg 195.3±4.49µs 195.0±0.45µs -0.15%
blake3_resource_id_creation/compute_from_path:../test-assets/test.pdf 1683.6±5.82µs 1687.5±14.31µs +0.23%
crc32_resource_id_creation/compute_from_bytes:large 87.3±0.18µs 87.4±0.58µs +0.11%
crc32_resource_id_creation/compute_from_bytes:medium 5.4±0.02µs 5.4±0.02µs 0.00%
crc32_resource_id_creation/compute_from_bytes:small 92.8±0.44ns 92.9±0.61ns +0.11%
crc32_resource_id_creation/compute_from_path:../test-assets/lena.jpg 62.3±0.27µs 62.4±0.52µs +0.16%
crc32_resource_id_creation/compute_from_path:../test-assets/test.pdf 936.9±10.83µs 934.7±7.68µs -0.23%
resource_index/index_build//tmp/ark-fs-index-benchmarks1EnAGU 104.8±0.93ms N/A N/A
resource_index/index_build//tmp/ark-fs-index-benchmarksDCQ4b3 96.1±2.35ms N/A N/A
resource_index/index_get_resource_by_id 98.4±0.28ns 98.9±0.47ns +0.51%
resource_index/index_get_resource_by_path 54.6±0.27ns 54.8±1.22ns +0.37%
resource_index/index_update_all 1054.5±34.45ms 1070.9±51.29ms +1.56%
resource_index/index_update_one 632.9±26.50ms 639.8±22.03ms +1.09%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants