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

POC: to batch plugin responses this has reduced webworker scripting by 2 seconds #37762

Draft
wants to merge 2 commits into
base: release
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions app/client/src/sagas/ActionExecution/PluginActionSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import {
all,
call,
delay,
fork,
put,
race,
select,
take,
takeEvery,
Expand Down Expand Up @@ -1677,7 +1679,7 @@ function* softRefreshActionsSaga() {
function* handleUpdateActionData(
action: ReduxAction<updateActionDataPayloadType>,
) {
const { actionDataPayload, parentSpan } = action.payload;
const { actionDataPayload, parentSpan } = action;

yield call(
evalWorker.request,
Expand All @@ -1690,6 +1692,45 @@ function* handleUpdateActionData(
}
}

// Use a channel to queue all actions

function* captureActionsWithinPeriod() {
while (true) {
const buffer = []; // Initialize a new buffer for each batch
const endTime = Date.now() + 10000;
let parentSpan;
// eslint-disable-next-line prefer-const

while (Date.now() < endTime) {
try {
// Use a non-blocking `take` to capture actions within the period

const { action } = yield race({
action: take(ReduxActionTypes.UPDATE_ACTION_DATA),
del: delay(1000),
});

if (!action) continue;

const { actionDataPayload } = action.payload;

parentSpan = action.payload.parentSpan;
buffer.push(...actionDataPayload);
} catch (e) {
// Handle errors if needed
}
}

// After the time period, dispatch the collected actions
if (buffer.length > 0) {
yield fork(handleUpdateActionData, {
parentSpan,
actionDataPayload: buffer,
});
}
}
}

Comment on lines +1725 to +1763
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Refactor looping mechanism to prevent high CPU usage.

The inner loop while (Date.now() < endTime) can cause high CPU utilization due to continuous execution. Consider using a more efficient approach that waits for actions or a timeout without busy-waiting.

Apply this diff to refactor the function:

 function* captureActionsWithinPeriod() {
   while (true) {
+    const buffer = []; // Initialize a new buffer for each batch
+    let parentSpan;
+    const startTime = Date.now();
+    const endTime = startTime + 10000;
+
+    while (Date.now() < endTime) {
       try {
-        const { action } = yield race({
-          action: take(ReduxActionTypes.UPDATE_ACTION_DATA),
-          del: delay(1000),
-        });
+        const { action, timeout } = yield race({
+          action: take(ReduxActionTypes.UPDATE_ACTION_DATA),
+          timeout: delay(endTime - Date.now()),
+        });

-        if (!action) continue;
+        if (timeout) {
+          break;
+        }

         const { actionDataPayload } = action.payload;
         parentSpan = action.payload.parentSpan;
         buffer.push(...actionDataPayload);
       } catch (e) {
         // Handle errors if needed
       }
     }

     // After the time period, dispatch the collected actions
     if (buffer.length > 0) {
       yield fork(handleUpdateActionData, {
         parentSpan,
         actionDataPayload: buffer,
       });
     }
   }
 }

Committable suggestion skipped: line range outside the PR's diff.

export function* watchPluginActionExecutionSagas() {
yield all([
takeLatest(ReduxActionTypes.RUN_ACTION_REQUEST, runActionSaga),
Expand All @@ -1703,6 +1744,6 @@ export function* watchPluginActionExecutionSagas() {
),
takeLatest(ReduxActionTypes.PLUGIN_SOFT_REFRESH, softRefreshActionsSaga),
takeEvery(ReduxActionTypes.EXECUTE_JS_UPDATES, makeUpdateJSCollection),
takeEvery(ReduxActionTypes.UPDATE_ACTION_DATA, handleUpdateActionData),
takeEvery(ReduxActionTypes.START_EVALUATION, captureActionsWithinPeriod),
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Prevent multiple instances of captureActionsWithinPeriod.

Using takeEvery with captureActionsWithinPeriod may start multiple saga instances if START_EVALUATION is dispatched repeatedly. To ensure only a single instance runs, consider using takeLeading or starting the saga once.

Apply this diff to adjust the saga watcher:

 export function* watchPluginActionExecutionSagas() {
   yield all([
     takeLatest(ReduxActionTypes.RUN_ACTION_REQUEST, runActionSaga),
     takeLatest(
       ReduxActionTypes.RUN_ACTION_SHORTCUT_REQUEST,
       runActionShortcutSaga,
     ),
     takeLatest(
       ReduxActionTypes.EXECUTE_PAGE_LOAD_ACTIONS,
       executePageLoadActionsSaga,
     ),
     takeLatest(ReduxActionTypes.PLUGIN_SOFT_REFRESH, softRefreshActionsSaga),
     takeEvery(ReduxActionTypes.EXECUTE_JS_UPDATES, makeUpdateJSCollection),
-    takeEvery(ReduxActionTypes.START_EVALUATION, captureActionsWithinPeriod),
+    takeLeading(ReduxActionTypes.START_EVALUATION, captureActionsWithinPeriod),
   ]);
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
takeEvery(ReduxActionTypes.START_EVALUATION, captureActionsWithinPeriod),
export function* watchPluginActionExecutionSagas() {
yield all([
takeLatest(ReduxActionTypes.RUN_ACTION_REQUEST, runActionSaga),
takeLatest(
ReduxActionTypes.RUN_ACTION_SHORTCUT_REQUEST,
runActionShortcutSaga,
),
takeLatest(
ReduxActionTypes.EXECUTE_PAGE_LOAD_ACTIONS,
executePageLoadActionsSaga,
),
takeLatest(ReduxActionTypes.PLUGIN_SOFT_REFRESH, softRefreshActionsSaga),
takeEvery(ReduxActionTypes.EXECUTE_JS_UPDATES, makeUpdateJSCollection),
takeLeading(ReduxActionTypes.START_EVALUATION, captureActionsWithinPeriod),
]);
}

]);
}
Loading