Skip to content

[HOLD for payment 2024-07-17] LOW: [HOLD] Create secondary maps for connection keys in Onyx #35809

Closed

Description

@mountiny

Coming from here

Problem

On the connect from Onyx, we store in a map (callbackToStateMapping) a unique connectionID with a mapping of the connected component (which has all the information about the component and configs), in that mapping there is a key which is the used in the withOnyx or the raw Onyx.connext to listen for value changes.

When a value for a key changes, the function keyChanged or keysChanged (if it is a collection) is being called, passing the key and some data. What that function does is, get all the connection IDs from the callbackToStateMapping, and goes through all the items checking if that connection has the key that changed. This could be a really low process, because it look into all the keys connected in the app, and as we can see in the screenshot, a really small account with a few chats and messages can create hundred of connections, and on top of that, the keyChanged function can be called multiple times.

Solution

Create a secondary map, which is the Key that is being listen (like session) and an array of connection IDs that uses that key. This will improve the look up and sending the changes because it look for the connections that are being listening to that Key.

// on connect function
+    if (!onyxKeyToConnectionIDs.has(mapping.key)) {
+        onyxKeyToConnectionIDs.set(mapping.key, []);
+    }
+    onyxKeyToConnectionIDs.get(mapping.key).push(connectionID);

// on keyChanged/keysChanged function
-    const stateMappingKeys = _.keys(callbackToStateMapping);
+    const stateMappingKeys = onyxKeyToConnectionIDs.get(key);

// on disconnect function
+    if (onyxKeyToConnectionIDs.has(keyToDelete)) {
+        onyxKeyToConnectionIDs.set(keyToDelete, _.without(onyxKeyToConnectionIDs.get(keyToDelete), connectionID));
+    }

Running a performance test on the keyChanged function we can notice a big improvement: onyx notify connections [function]: 13.7 ms → 1.5 ms (-12.2 ms, -88.9%) 🟢🟢 | 1 → 1
(this test created around 10k of keys and connections)
We did some tests on Android Production build and we got some results:
Report Load:
Hot load:
without patch -> 3677 ms
with patch -> 3380 ms
Improvement -> 300ms
Cold load:
without patch -> 34936 ms
with patch -> 4756 ms
Improvement -> 30180 ms ‼️

Issue OwnerCurrent Issue Owner: @johncschuster / @anmurali
Issue OwnerCurrent Issue Owner: @johncschuster / @anmurali

Activity

  1. self-assigned this
    on Feb 5, 2024
  2. melvin-bot commented on Feb 5, 2024

    @melvin-bot
  3. gedu commented on Feb 5, 2024

    @gedu
    Contributor

    I am from Callstack - expert contributor I would like to work on this

  4. gedu commented on Feb 7, 2024

    @gedu
    Contributor

    @mountiny I created a PR, which is a the app with an Onyx patch with my proposed changes, so they can be tested using the app. if all goes good, I can implement those changes into Onyx and make a new PR

  5. 36 remaining items

  6. melvin-bot commented on Jul 10, 2024

    @melvin-bot

    BugZero Checklist: The PR adding this new feature has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

    • [@gedu] Please propose regression test steps to ensure the new feature will work correctly on production in further releases.
    • [@johncschuster / @anmurali] Link the GH issue for creating/updating the regression test once above steps have been agreed upon.
  7. changed the title [-][HOLD for payment 2024-07-17] LOW: [HOLD] Create secondary maps for connection keys in Onyx[/-] [+][HOLD for payment 2024-07-22] [HOLD for payment 2024-07-17] LOW: [HOLD] Create secondary maps for connection keys in Onyx[/+] on Jul 15, 2024
  8. melvin-bot commented on Jul 15, 2024

    @melvin-bot

    The solution for this issue has been 🚀 deployed to production 🚀 in version 9.0.6-8 and is now subject to a 7-day regression period 📆. Here is the list of pull requests that resolve this issue:

    If no regressions arise, payment will be issued on 2024-07-22. 🎊

    For reference, here are some details about the assignees on this issue:

    • @gedu does not require payment (Contractor)
  9. melvin-bot commented on Jul 15, 2024

    @melvin-bot

    BugZero Checklist: The PR adding this new feature has been merged! The following checklist (instructions) will need to be completed before the issue can be closed:

    • [@gedu] Please propose regression test steps to ensure the new feature will work correctly on production in further releases.
    • [@johncschuster / @anmurali] Link the GH issue for creating/updating the regression test once above steps have been agreed upon.
  10. changed the title [-][HOLD for payment 2024-07-22] [HOLD for payment 2024-07-17] LOW: [HOLD] Create secondary maps for connection keys in Onyx[/-] [+][HOLD for payment 2024-07-17] LOW: [HOLD] Create secondary maps for connection keys in Onyx[/+] on Jul 16, 2024
  11. johncschuster commented on Jul 16, 2024

    @johncschuster
    Contributor

    @gedu, do we need a regression test here? If so, can you propose the steps?

  12. johncschuster commented on Jul 17, 2024

    @johncschuster
    Contributor

    No payment required here. We're just waiting on the regression test steps.

  13. melvin-bot commented on Jul 17, 2024

    @melvin-bot

    Skipping the payment summary for this issue since all the assignees are employees or vendors. If this is incorrect, please manually add the payment summary SO.

  14. johncschuster commented on Jul 18, 2024

    @johncschuster
    Contributor

    Bumped @gedu in Slack about the regression test suite

  15. gedu commented on Jul 18, 2024

    @gedu
    Contributor

    I don't think a regression test is needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionDailyKSv2NewFeatureSomething to build that is a new item.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions