-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Tried to delegate to tab the script execution, it was too slow
- Loading branch information
Showing
12 changed files
with
617 additions
and
424 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
function generateRandomString(minLen = 1000, maxLen = 2000) { | ||
const length = Math.floor(Math.random() * (maxLen - minLen + 1)) + minLen; | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
let result = ''; | ||
for (let i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * characters.length)); | ||
} | ||
return result; | ||
} | ||
|
||
function benchmark_fMurmurHash3Hash(numTests = 100) { | ||
let totalTime = 0; | ||
|
||
for (let i = 0; i < numTests; i++) { | ||
// Generate a random string of 1000-2000 characters | ||
const randomString = generateRandomString(); | ||
|
||
// Start the timer | ||
const startTime = performance.now(); | ||
|
||
// Execute the hashing function | ||
const result = fMurmurHash3Hash(randomString); | ||
|
||
// Stop the timer | ||
const endTime = performance.now(); | ||
|
||
// Calculate the time taken for this iteration in milliseconds | ||
const timeTaken = endTime - startTime; | ||
|
||
totalTime += timeTaken; | ||
} | ||
|
||
// Calculate the average time | ||
const avgTime = totalTime / numTests; | ||
|
||
console.log(`Avg time for ${numTests} executions: ${avgTime.toFixed(2)} ms`); | ||
console.log(`Total time for ${numTests} executions: ${totalTime.toFixed(2)} ms`); | ||
} | ||
|
||
// Assuming fMurmurHash3Hash is already defined in your scope, run the benchmark | ||
benchmark_fMurmurHash3Hash(1000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
function generateRandomString(minLen = 5000, maxLen = 1000) { | ||
const length = Math.floor(Math.random() * (maxLen - minLen + 1)) + minLen; | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
let result = ''; | ||
for (let i = 0; i < length; i++) { | ||
result += characters.charAt(Math.floor(Math.random() * characters.length)); | ||
} | ||
return result; | ||
} | ||
|
||
function generateRandomValues(numValues = 100) { | ||
let values = []; | ||
for (let i = 0; i < numValues; i++) { | ||
values.push(Math.random()); // Random float between 0 and 1 | ||
} | ||
return values; | ||
} | ||
|
||
// Existing cache system using fMurmurHash3Hash function | ||
function benchmarkExistingCache(strings, values, numTests = 100) { | ||
let cache = {}; // Simulating the cache as an object | ||
let totalTime = 0; | ||
|
||
for (let i = 0; i < numTests; i++) { | ||
let key = strings[i]; | ||
let value = values[i]; | ||
|
||
// Measure insertion time (hashing + storing) | ||
const startInsert = performance.now(); | ||
const hashKey = fMurmurHash3Hash(key); | ||
cache[hashKey] = value; | ||
const endInsert = performance.now(); | ||
|
||
totalTime += (endInsert - startInsert); | ||
|
||
// Measure retrieval time | ||
const startRetrieve = performance.now(); | ||
const retrievedValue = cache[hashKey]; | ||
const endRetrieve = performance.now(); | ||
|
||
totalTime += (endRetrieve - startRetrieve); | ||
} | ||
|
||
const avgTime = totalTime / numTests; | ||
console.log(`Existing Cache - Avg time for ${numTests} executions: ${avgTime.toFixed(2)} ms`); | ||
console.log(`Existing Cache - Total time for ${numTests} executions: ${totalTime.toFixed(2)} ms`); | ||
} | ||
|
||
// New cache system using Map with no explicit hashing | ||
function benchmarkNewCache(strings, values, numTests = 100) { | ||
let map = new Map(); | ||
let totalTime = 0; | ||
|
||
for (let i = 0; i < numTests; i++) { | ||
let key = strings[i]; | ||
let value = values[i]; | ||
|
||
// Measure insertion time (storing in Map) | ||
const startInsert = performance.now(); | ||
map.set(key, value); | ||
const endInsert = performance.now(); | ||
|
||
totalTime += (endInsert - startInsert); | ||
|
||
// Measure retrieval time | ||
const startRetrieve = performance.now(); | ||
const retrievedValue = map.get(key); | ||
const endRetrieve = performance.now(); | ||
|
||
totalTime += (endRetrieve - startRetrieve); | ||
} | ||
|
||
const avgTime = totalTime / numTests; | ||
console.log(`New Cache (Map) - Avg time for ${numTests} executions: ${avgTime.toFixed(2)} ms`); | ||
console.log(`New Cache (Map) - Total time for ${numTests} executions: ${totalTime.toFixed(2)} ms`); | ||
} | ||
|
||
// Pre-initialize random strings and values | ||
const numTests = 10000; | ||
const randomStrings = Array.from({ length: numTests }, () => generateRandomString()); | ||
const randomValues = generateRandomValues(numTests); | ||
|
||
// Run the benchmarks | ||
console.log("Benchmarking Existing Cache with Hashing Function:"); | ||
benchmarkExistingCache(randomStrings, randomValues, numTests); | ||
|
||
console.log("\nBenchmarking New Cache with Map:"); | ||
benchmarkNewCache(randomStrings, randomValues, numTests); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
|
||
{ // Was too slow and needed overly complex code to write and also to debug. | ||
handleCSSChunk(data, verify, details, filter) { | ||
let str = details.rejectedValues; | ||
if(data){ str += details.decoder.decode(data,{stream:true}); } | ||
|
||
|
||
let options = { chunk:uDark.idk_cache.get(str) }; | ||
|
||
if (options.chunk) { | ||
uDark.idk_cache.delete(str); | ||
uDark.idk_cache.has(details.tabId) && uDark.idk_cache.get(details.tabId).delete(str); | ||
filter.write(details.encoder.encode(options.chunk)); | ||
|
||
details.countDone+=1; | ||
return true; | ||
} | ||
|
||
|
||
let edit_str_promise = globalThis.browser.tabs.executeScript(details.tabId,{code: | ||
` | ||
(function(){ | ||
let details =${JSON.stringify( | ||
{ | ||
url:details.url, | ||
tabId:details.tabId, | ||
frameId:details.frameId, | ||
originUrl:details.originUrl, | ||
documentUrl:details.documentUrl, | ||
requestId:details.requestId, | ||
dataCount:details.dataCount, | ||
transientCache: details.transientCache | ||
|
||
} | ||
)}; | ||
let options=${JSON.stringify(options)}; | ||
options.cant_resolve_vars_yet=true; | ||
let str=uDark.edit_str(${JSON.stringify(str)},false,${JSON.stringify(verify)},details ,false,options); | ||
if(options.unresolvableStylesheet){ | ||
if(options.hasUnresolvedVars_idk_vars){ | ||
console.log("Unresolvable idk vars",options.unresolvableStylesheet.rules); | ||
} | ||
let rulesArray = [...options.unresolvableStylesheet.cssRules].map(r=>({cssText:r.cssText})); | ||
options.unresolvableStylesheet={cssRules:rulesArray}; | ||
} | ||
delete options.cssStyleSheet; | ||
return {str,details ,options}; | ||
})(); | ||
` | ||
,frameId:details.frameId,allFrames:false,matchAboutBlank:true,runAt:"document_start"}) | ||
.then(promise=>promise).then(result=>result[0]); | ||
|
||
|
||
// options.chunk = uDark.edit_str( str, false, verify, details, false, options); | ||
|
||
return edit_str_promise.then(result=>{ | ||
options = result.options; | ||
|
||
details.countDone+=1; | ||
details.unresolvableChunks=details.unresolvableChunks||result.details||false; | ||
options.chunk=result.str; | ||
details.transientCache = result.details.transientCache; | ||
if (options.chunk.message) { | ||
details.rejectedValues = str; // Keep rejected values for later use | ||
return; | ||
} else { | ||
details.rejectedValues = ""; | ||
if (options.chunk.rejected) { | ||
details.rejectedValues = options.chunk.rejected; | ||
options.chunk = options.chunk.str; | ||
} | ||
} | ||
options.str_key_cache = str; | ||
dark_object.misc.chunk_manage_idk_direct(details, options, filter); | ||
filter.write(details.encoder.encode(options.chunk)); | ||
|
||
}) | ||
|
||
}, | ||
editBeforeRequestStyleSheet: function(details) { | ||
let options = {}; | ||
options.isCorsRequest = dark_object.misc.isCorsRequest(details); | ||
|
||
// console.log("Loading CSS", details.url, details.requestId, details.fromCache) | ||
|
||
// Util 2024 jan 02 we were checking details.documentUrl, or details.url to know if a stylesheet was loaded in a excluded page | ||
// Since only CS ports that matches blaclist and whitelist are connected, we can simply check if this resource has a corresponding CS port | ||
if (!uDark.getPort(details)) { | ||
// console.log("CSS", "No port found for", details.url, "loaded by webpage:", details.originUrl, "Assuming it is not an eligible webpage, or even blocked by another extension"); | ||
// console.log("If i'm lacking of knowledge, here is what i know about this request", details.tabId, details.frameId); | ||
return {} | ||
} | ||
|
||
let filter = globalThis.browser.webRequest.filterResponseData(details.requestId); // After this instruction, browser espect us to write data to the filter and close it | ||
|
||
let n = details.responseHeaders.length; | ||
details.headersLow = {} | ||
while (n--) { | ||
details.headersLow[details.responseHeaders[n].name.toLowerCase()] = details.responseHeaders[n].value; | ||
} | ||
console.log("Will darken", details.url, details.requestId, details.fromCache,details) | ||
|
||
details.charset = ((details.headersLow["content-type"] || "").match(/charset=([0-9A-Z-]+)/i) || ["", "utf-8"])[1] | ||
details.decoder = new TextDecoder(details.charset) | ||
details.encoder = new TextEncoder(); | ||
details.dataCount = 0; | ||
details.countDone = 0; | ||
details.rejectedValues = ""; | ||
|
||
|
||
|
||
// // ondata event handler | ||
filter.ondata = async event => { | ||
details.dataCount++; | ||
dark_object.misc.handleCSSChunk(event.data, true, details, filter); | ||
}; | ||
|
||
// onstop event handler | ||
|
||
// onstop event handler | ||
filter.onstop = event => { | ||
if (details.rejectedValues.length > 0) { | ||
let handleResult = dark_object.misc.handleCSSChunk(null, false, details, filter); | ||
// if(handleResult.then){console.log("Closing filter 1"); return handleResult.then(x=>filter.disconnect())} | ||
} | ||
|
||
let intervalClose=setInterval(()=>{ | ||
if(details.countDone>=details.dataCount){ | ||
clearInterval(intervalClose); | ||
filter.disconnect(); // Ensure disconnection after completion of last chunk | ||
} | ||
else{ | ||
console.log("Not closing filter 2",details.countDone,"/",details.dataCount,"for",details.url,details.requestId); | ||
} | ||
},100) | ||
|
||
}; | ||
// return {redirectUrl:details.url}; | ||
// return {responseHeaders:[{name:"Vary",value:"*"},{name:"Location",value:details.url}]}; | ||
// return {}; | ||
// must not return this closes filter// | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Tableau 255x255 avec Dégradé</title> | ||
<style> | ||
table { | ||
border-collapse: collapse; | ||
border: 1px solid black; | ||
} | ||
|
||
td { | ||
border: 1px solid black; | ||
width: 3px; | ||
height: 3px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<script> | ||
document.addEventListener('DOMContentLoaded', function () { | ||
// Créer un élément de tableau | ||
var table = document.createElement('table'); | ||
|
||
// Boucle pour créer les lignes | ||
for (var i = 0; i < 255; i++) { | ||
var row = document.createElement('tr'); | ||
|
||
// Boucle pour créer les cellules dans chaque ligne | ||
for (var j = 0; j < 255; j++) { | ||
var cell = document.createElement('td'); | ||
|
||
// Calculer une couleur du spectre complet en fonction de la position horizontale | ||
var hue = (i + j) % 360; | ||
cell.style.backgroundColor = 'hsl(' + hue + ', 100%, 50%)'; | ||
|
||
row.appendChild(cell); | ||
} | ||
|
||
// Ajouter la ligne au tableau | ||
table.appendChild(row); | ||
} | ||
|
||
// Ajouter le tableau à la page | ||
document.body.appendChild(table); | ||
}); | ||
</script> | ||
|
||
</body> | ||
</html> |
Oops, something went wrong.