Commit 2985a247 authored by manuk's avatar manuk Committed by Commit Bot

[about-omnibox] Performance refactoring of omnibox.js: split refresh method,...

[about-omnibox] Performance refactoring of omnibox.js: split refresh method, extract inlined dom elements, and improved html output clearing.

1. Split `refresh` into `refreshAllResults` and `refreshNewResult` for performance reasons as explained in the comments (tldr, refreshing entire output is unnecessary when we only need to add to the output).
2. Extract variables for inlined expressions to retrieve document elements.
3. Use `Element.removeChild` instead of `Element.innerHTML = ` to clear output

Change-Id: Ia7085d97537c5f445c950ecb50f35e5880b6124a
Reviewed-on: https://chromium-review.googlesource.com/1246295
Commit-Queue: manuk hovanesian <manukh@chromium.org>
Reviewed-by: default avatarTommy Li <tommycli@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594820}
parent 441b0678
...@@ -18,19 +18,11 @@ ...@@ -18,19 +18,11 @@
*/ */
(function () { (function () {
/** // Variables to DOM elements to avoid multiple document.getElement calls.
* Register our event handlers. let omniboxDebugText;
*/ let showIncompleteResults;
function initialize() { let showDetails;
$('input-text').addEventListener('input', startOmniboxQuery, false); let showAllProviders;
$('prevent-inline-autocomplete')
.addEventListener('change', startOmniboxQuery);
$('prefer-keyword').addEventListener('change', startOmniboxQuery);
$('page-classification').addEventListener('change', startOmniboxQuery);
$('show-details').addEventListener('change', refresh);
$('show-incomplete-results').addEventListener('change', refresh);
$('show-all-providers').addEventListener('change', refresh);
}
/** /**
* @type {!Array<mojom.OmniboxResult>} an array of all autocomplete results we've seen * @type {!Array<mojom.OmniboxResult>} an array of all autocomplete results we've seen
...@@ -47,6 +39,25 @@ ...@@ -47,6 +39,25 @@
*/ */
let cursorPositionUsed = -1; let cursorPositionUsed = -1;
/**
* Register our event handlers.
*/
function initialize() {
omniboxDebugText = $('omnibox-debug-text');
showIncompleteResults = $('show-incomplete-results');
showDetails = $('show-details');
showAllProviders = $('show-all-providers');
$('input-text').addEventListener('input', startOmniboxQuery);
$('prevent-inline-autocomplete')
.addEventListener('change', startOmniboxQuery);
$('prefer-keyword').addEventListener('change', startOmniboxQuery);
$('page-classification').addEventListener('change', startOmniboxQuery);
showIncompleteResults.addEventListener('change', refreshAllResults);
showDetails.addEventListener('change', refreshAllResults);
showAllProviders.addEventListener('change', refreshAllResults);
}
/** /**
* Extracts the input text from the text field and sends it to the * Extracts the input text from the text field and sends it to the
* C++ portion of chrome to handle. The C++ code will iteratively * C++ portion of chrome to handle. The C++ code will iteratively
...@@ -54,6 +65,7 @@ ...@@ -54,6 +65,7 @@
*/ */
function startOmniboxQuery(event) { function startOmniboxQuery(event) {
// First, clear the results of past calls (if any). // First, clear the results of past calls (if any).
clearOutput();
progressiveAutocompleteResults = []; progressiveAutocompleteResults = [];
// Then, call chrome with a five-element list: // Then, call chrome with a five-element list:
// - first element: the value in the text box // - first element: the value in the text box
...@@ -61,14 +73,12 @@ ...@@ -61,14 +73,12 @@
// - third element: the value of prevent-inline-autocomplete // - third element: the value of prevent-inline-autocomplete
// - forth element: the value of prefer-keyword // - forth element: the value of prefer-keyword
// - fifth element: the value of page-classification // - fifth element: the value of page-classification
cursorPositionUsed = $('input-text').selectionEnd; let inputTextElement = $('input-text');
cursorPositionUsed = inputTextElement.selectionEnd;
browserProxy.startOmniboxQuery( browserProxy.startOmniboxQuery(
$('input-text').value, cursorPositionUsed, inputTextElement.value, cursorPositionUsed,
$('prevent-inline-autocomplete').checked, $('prefer-keyword').checked, $('prevent-inline-autocomplete').checked, $('prefer-keyword').checked,
parseInt($('page-classification').value, 10)); parseInt($('page-classification').value, 10));
// Cancel the submit action. i.e., don't submit the form. (We handle
// display the results solely with Javascript.)
event.preventDefault();
} }
/** /**
...@@ -272,21 +282,16 @@ ...@@ -272,21 +282,16 @@
* for each autocomplete match. The input parameter is an OmniboxResultMojo. * for each autocomplete match. The input parameter is an OmniboxResultMojo.
*/ */
function addResultToOutput(result) { function addResultToOutput(result) {
let output = $('omnibox-debug-text');
let inDetailedMode = $('show-details').checked;
let showIncompleteResults = $('show-incomplete-results').checked;
let showPerProviderResults = $('show-all-providers').checked;
// Output the result-level features in detailed mode and in // Output the result-level features in detailed mode and in
// show incomplete results mode. We do the latter because without // show incomplete results mode. We do the latter because without
// these result-level features, one can't make sense of each // these result-level features, one can't make sense of each
// batch of results. // batch of results.
if (inDetailedMode || showIncompleteResults) { if (showDetails.checked || showIncompleteResults.checked) {
addParagraph(output, `cursor position = ${cursorPositionUsed}`); addParagraph(omniboxDebugText, `cursor position = ${cursorPositionUsed}`);
addParagraph(output, `inferred input type = ${result.type}`); addParagraph(omniboxDebugText, `inferred input type = ${result.type}`);
addParagraph( addParagraph(
output, `elapsed time = ${result.timeSinceOmniboxStartedMs}ms`); omniboxDebugText, `elapsed time = ${result.timeSinceOmniboxStartedMs}ms`);
addParagraph(output, `all providers done = ${result.done}`); addParagraph(omniboxDebugText, `all providers done = ${result.done}`);
let p = document.createElement('p'); let p = document.createElement('p');
p.textContent = `host = ${result.host}`; p.textContent = `host = ${result.host}`;
// The field isn't actually optional in the mojo object; instead it assumes // The field isn't actually optional in the mojo object; instead it assumes
...@@ -298,22 +303,22 @@ ...@@ -298,22 +303,22 @@
p.textContent = p.textContent =
p.textContent + ` has isTypedHost = ${result.isTypedHost}`; p.textContent + ` has isTypedHost = ${result.isTypedHost}`;
} }
output.appendChild(p); omniboxDebugText.appendChild(p);
} }
// Combined results go after the lines below. // Combined results go after the lines below.
let group = document.createElement('a'); let group = document.createElement('a');
group.className = 'group-separator'; group.className = 'group-separator';
group.textContent = 'Combined results.'; group.textContent = 'Combined results.';
output.appendChild(group); omniboxDebugText.appendChild(group);
// Add combined/merged result table. // Add combined/merged result table.
let p = document.createElement('p'); let p = document.createElement('p');
p.appendChild(addResultTableToOutput(result.combinedResults)); p.appendChild(addResultTableToOutput(result.combinedResults));
output.appendChild(p); omniboxDebugText.appendChild(p);
// Move forward only if you want to display per provider results. // Move forward only if you want to display per provider results.
if (!showPerProviderResults) { if (!showAllProviders.checked) {
return; return;
} }
...@@ -321,7 +326,7 @@ ...@@ -321,7 +326,7 @@
group = document.createElement('a'); group = document.createElement('a');
group.className = 'group-separator'; group.className = 'group-separator';
group.textContent = 'Results for individual providers.'; group.textContent = 'Results for individual providers.';
output.appendChild(group); omniboxDebugText.appendChild(group);
// Add the per-provider result tables with labels. We do not append the // Add the per-provider result tables with labels. We do not append the
// combined/merged result table since we already have the per provider // combined/merged result table since we already have the per provider
...@@ -332,7 +337,7 @@ ...@@ -332,7 +337,7 @@
return; return;
let p = document.createElement('p'); let p = document.createElement('p');
p.appendChild(addResultTableToOutput(providerResults.results)); p.appendChild(addResultTableToOutput(providerResults.results));
output.appendChild(p); omniboxDebugText.appendChild(p);
}); });
} }
...@@ -392,20 +397,34 @@ ...@@ -392,20 +397,34 @@
* entry unless we're asked to display incomplete results. For an * entry unless we're asked to display incomplete results. For an
* example of the output, play with chrome://omnibox/ * example of the output, play with chrome://omnibox/
*/ */
function refresh() { function refreshAllResults() {
// Erase whatever is currently being displayed. clearOutput();
let output = $('omnibox-debug-text'); if (showIncompleteResults.checked)
output.innerHTML = ''; progressiveAutocompleteResults.forEach(addResultToOutput);
else if (progressiveAutocompleteResults.length)
if (progressiveAutocompleteResults.length > 0) { // if we have results addResultToOutput(progressiveAutocompleteResults[progressiveAutocompleteResults.length - 1]);
// Display the results. }
let showIncompleteResults = $('show-incomplete-results').checked;
let startIndex = /*
showIncompleteResults ? 0 : progressiveAutocompleteResults.length - 1; * Adds the last result, based on the contents of the array
for (let i = startIndex; i < progressiveAutocompleteResults.length; i++) { * progressiveAutocompleteResults, to the page. If we're not displaying
addResultToOutput(progressiveAutocompleteResults[i]); * incomplete results, we clear the page and add the last result, similar to
} * refreshAllResults. If we are displaying incomplete results, then this is
} * more efficient than refreshAllResults, as there's no need to clear and
* re-add previous results.
*/
function refreshNewResult() {
if (!showIncompleteResults.checked)
clearOutput();
addResultToOutput(progressiveAutocompleteResults[progressiveAutocompleteResults.length - 1]);
}
/*
Removes all results from the page.
*/
function clearOutput() {
while (omniboxDebugText.firstChild)
omniboxDebugText.removeChild(omniboxDebugText.firstChild);
} }
// NOTE: Need to keep a global reference to the |pageImpl| such that it is not // NOTE: Need to keep a global reference to the |pageImpl| such that it is not
...@@ -426,7 +445,7 @@ ...@@ -426,7 +445,7 @@
handleNewAutocompleteResult(result) { handleNewAutocompleteResult(result) {
progressiveAutocompleteResults.push(result); progressiveAutocompleteResults.push(result);
refresh(); refreshNewResult();
} }
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment