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

Improve rapid generation performance via UI throttling #7084

Merged
merged 2 commits into from Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/flat-suns-cry.md
@@ -0,0 +1,6 @@
---
"@gradio/app": minor
"gradio": minor
---

feat:Improve rapid generation performance via UI throttling
43 changes: 40 additions & 3 deletions js/app/src/Blocks.svelte
Expand Up @@ -305,6 +305,43 @@
});
}

function throttle<T extends (...args: any[]) => any>(
func: T,
limit: number
): (...funcArgs: Parameters<T>) => void {
let lastFunc: ReturnType<typeof setTimeout>;
let lastRan: number;
let lastThis: any;
let lastArgs: IArguments | null;

return function (this: any, ...args: Parameters<T>) {
if (!lastRan) {
func.apply(this, args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastThis = this;
lastArgs = arguments;

lastFunc = setTimeout(
() => {
if (Date.now() - lastRan >= limit) {
if (lastArgs) {
func.apply(lastThis, Array.prototype.slice.call(lastArgs));
}
lastRan = Date.now();
}
},
Math.max(limit - (Date.now() - lastRan), 0)
);
}
};
}

const refresh = throttle(() => {
rootNode = rootNode;
}, 50);

async function handle_update(
data: any,
fn_index: number,
Expand All @@ -317,7 +354,7 @@
output.props.value_is_output = true;
});

rootNode = rootNode;
refresh();
await tick();
data?.forEach((value: any, i: number) => {
const output = instance_map[outputs[i]];
Expand All @@ -340,7 +377,7 @@
output.props.value = value;
}
});
rootNode = rootNode;
refresh();
}

let submit_map: Map<number, ReturnType<typeof app.submit>> = new Map();
Expand All @@ -355,7 +392,7 @@
obj.props = {};
}
obj.props[prop] = val;
rootNode = rootNode;
refresh();
}
let handled_dependencies: number[][] = [];

Expand Down