react-native-clusterer icon indicating copy to clipboard operation
react-native-clusterer copied to clipboard

Feature: Rewrite regionToBBox and getMarkersRegion in cpp

Open IslamRustamov opened this issue 1 month ago • 0 comments

Re #31

What was done

  • Util functions such as calculateDelta, calculateAverage, regionToBBox and getMarkersRegion were rewritten from JS to C++
  • Created installHelpers to install regionToBBox and getMarkersRegion as globals

Test environment

MacBook Pro M1 iOS Emulator

IMPORTANT

I did some calculations to check a couple of things:

Does C++ functions return the same results as original JS functions

To prove that C++ and JS functions return the same results I run the next computations: https://gist.github.com/IslamRustamov/0c177a062b2af0836104f269f6809ca0 - comparison of regionToBBox https://gist.github.com/IslamRustamov/3081cd26d04fd3de736eded42f37ea1d - comparison of getMarkersRegion

As a result all of these computations - C++ functions were returning the same results as JS functions, which means that C++ rewrite was correct.

Does C++ functions compute results faster than the original JS functions

To evaluate the speed of computation I did the next things:

  1. Generate random arguments of size N;
  2. Run function with specified arguments N times;
  3. On each run calculate the time of execution with performance.now();
  4. Repeat steps 2 and 3 M times;
  5. Calculate the average.

Sample code looked like this for both functions (regionToBBox and getMarkersRegion): https://gist.github.com/IslamRustamov/25514243ae992a6c0e2f3946da5a8d73

The results are, sadly, not in favor of C++: Screenshot 2025-01-04 at 22 02 36

The reason why C++ performs worse than JS is probably because passing big chunks of data between JS and native side is taking a lot of time. While C++ may compute the result faster, passing of data is still going to be the problem. This can be solved by switching to Turbo Module implementation of these functions, what will (with high probability) produce a better result for C++.

Source: https://medium.com/@islamrustamov/obj-c-turbo-module-vs-c-turbo-module-vs-flutter-platform-channel-e610a344ec92

Note: maybe I messed up something and that's why C++ is not performing well, I am not an average C++ enjoyer so please do find problems in my PR if there are any. Otherwise this PR is actually going to make this lib work slower, so it can be closed.

IslamRustamov avatar Jan 04 '25 19:01 IslamRustamov