Blazor Web App SIMD Detect Example
BlazorWebAppSIMDDetectExample
Blazor WebAssembly Standalone SIMD Detect Example (this repo)
BlazorWASMSIMDDetectExample
If you have done a lot of testing with Blazor WASM you may eventually hit some compatibility issues you weren't expecting. This .Net 8 Blazor WASM project demonstrates a way of detecting SIMD support and using it if available.
Single Instruction, Multiple Data support has been added to Blazor WASM and is now enabled by default in .Net 8, and for good reason. Enabling SIMD brings some large speed improvements in many areas of Blazor WASM. There are many articles that praise the benefits of SIMD. While the linked articles below do not specifically mention Blazor, they all talk about the benefits SIMD can bring to C#.
SIMD and C# articles:
- Use SIMD-accelerated numeric types
- .Net 9 AVX10v1 Support
- LINQ on steroids with SIMD
- SIMD, a parallel processing at hardware level in C#
- LINQ Internals: Speed Optimizations
- Optimizing string.Count all the way from LINQ to hardware accelerated vectorized instructions
- Faster Guid comparisons using Vectors (SIMD) in .NET
SIMD WASM support is far from universal and a lack of support kills any Blazor WASM app that requires it before it starts.
A lack of WASM SIMD support can be caused by:
- old browsers. Convincing end users to upgrade for your site is not a great option.
- old hardware. An AMD Phenom 2 X6 has 6 cores and runs at a base speed of 3.2 GHz but no browser running on it can support SIMD because the CPU does not support it. This, and a lot of hardware like it, is still in use and is quite capable.
I also have an updated test phone running Android 9 "Pie" with the latest Firefox 118 and SIMD is not supported. Chrome on the same device supports SIMD. That same version of Firefox on a tablet running Android 11 "Red Velvet Cake" supports SIMD.
So it is obvious that SIMD support is a bit fractured.
Here are 2 options to handle SIMD compatibility issues.
This is the simplest option and only requires adding the flag <WasmEnableSIMD>false</WasmEnableSIMD> to your project's .csproj file inside a <PropertyGroup>. This is the easiest and most compatible way to get around a lack of SIMD support but you lose the ability to take advantage if it is supported. <WasmEnableExceptionHandling>false</WasmEnableExceptionHandling> is also recommended for compatibility builds.
I also recommend disabling BlazorWebAssemblyJiterpreter in your compatibility build. Testing on systems that did not support SIMD with SIMD disabled builds would get an exception MONO_WASM: get_Cache:0 code generation failed: CompileError: at offset 161: bad type U ... and also the message MONO_WASM: Disabling jiterpreter after 2 failures. Setting <BlazorWebAssemblyJiterpreter>false</BlazorWebAssemblyJiterpreter> fixes it.
- Modify your index.html to detect SIMD support and load a compatibility build if needed.
- Create a compatibility build with SIMD and BlazorWebAssemblyJiterpreter disabled.
Modify the Blazor startup in the main html file. Depending on the template you used it could be index.html if a Blazor WebAssembly Standalone app or App.razor in the server project if a Blazor Web App.
The code below will detect SIMD support and use the appropriate build folder.
Modify the html file like below.
<!-- autostart is set to false so we can detect SIMD support and load the appropriate build --><!-- the below script can also be "_framework/blazor.web.js" depending on the hosting model --><scriptsrc="_framework/blazor.webassembly.js" autostart="false"></script><!-- WASM Feature Detect - from GoogleChromeLabs CDN UMD Version: https://unpkg.com/wasm-feature-detect/dist/umd/index.js Repo: https://github.com/GoogleChromeLabs/wasm-feature-detect--><scriptwebworker-enabledsrc="wasm-feature-detect.1.5.1.js"></script><!-- The below script tag is used to detect SIMD support on the running device and load the appropriate build If SIMD is not supported it loads _frameworkCompat/ instead of _framework/ --><scriptwebworker-enabled>// Blazor WASM will fail to load if BigInt64Array or BigUint64Array is not found, but it does not use them on startupif(!globalThis.BigInt64Array)globalThis.BigInt64Array=function(){};if(!globalThis.BigUint64Array)globalThis.BigUint64Array=function(){};(async()=>{varurl=newURL(location.href);letverboseStart=url.searchParams.get('verboseStart')==='1';varforceCompatMode=url.searchParams.get('forceCompatMode')==='1';varsupportsSimd=awaitwasmFeatureDetect.simd();if(verboseStart)console.log('supportsSimd',supportsSimd);// compat mode build could be built without wasm exception support if needed and detected herevarsupportsExceptions=awaitwasmFeatureDetect.exceptions();if(verboseStart)console.log('supportsExceptions',supportsExceptions);varuseCompatMode=!supportsSimd||!supportsExceptions;if(forceCompatMode){if(verboseStart)console.log('forceCompatMode',forceCompatMode);useCompatMode=true;}if(verboseStart)console.log('useCompatMode',useCompatMode);// Blazor United (.Net 8 Blazor Web App) Blazor.start settings are slightly different than Blazor WebAssembly (Blazor WebAssembly Standalone App)vargetRuntimeType=function(){for(varscriptofdocument.scripts){if(script.src.indexOf('_framework/blazor.web.js')!==-1)return'united';if(script.src.indexOf('_framework/blazor.webassembly.js')!==-1)return'wasm';}return'';}varruntimeType=getRuntimeType();// customize the resource loader for the runtime that is loaded// https://learn.microsoft.com/en-us/aspnet/core/blazor/fundamentals/startup?view=aspnetcore-8.0#load-boot-resourcesvarwebAssemblyConfig={loadBootResource: function(type,name,defaultUri,integrity){if(verboseStart)console.log(`Loading: '${type}', '${name}', '${defaultUri}', '${integrity}'`);if(useCompatMode){letnewUrl=defaultUri.replace('_framework/','_frameworkCompat/');returnnewUrl;}},};if(runtimeType==='wasm'){// Blazor WebAssembly Standalone AppBlazor.start(webAssemblyConfig);}elseif(runtimeType==='united'){// Blazor Web App (formally Blazor United)Blazor.start({webAssembly: webAssemblyConfig});}else{// Fallback supports both known Blazor WASM runtimes// Modified loader that will work with both United and WASM runtimes (doesn't require detection)webAssemblyConfig.webAssembly=webAssemblyConfig;Blazor.start(webAssemblyConfig);}})();</script>If using ASP.Net Core hosted Blazor WASM, the server needs to be told to serve the .dat file type or some files will not be served from _frameworkCompat resulting in File not found errors in the browser.
In the server project Program.cs file modify the app.UseStaticFiles call to allow serving .dat files.
// Enable the .dat file extension (required to serve icudt.dat from _frameworkCompat/varprovider=newFileExtensionContentTypeProvider();provider.Mappings[".dat"]="application/octet-stream";app.UseStaticFiles(newStaticFileOptions{ContentTypeProvider=provider});Add ReleaseCompat configuration rule to the Blazor WASM .csproj file (used during publish)
<PropertyGroupCondition=" '$(Configuration)' == 'ReleaseCompat' "> <WasmEnableSIMD>false</WasmEnableSIMD> <BlazorWebAssemblyJiterpreter>false</BlazorWebAssemblyJiterpreter> <WasmEnableExceptionHandling>false</WasmEnableExceptionHandling> </PropertyGroup>Example publish.bat to build first with SIMD support, and then without SIMD support for compatibility. This batch script is located in the project folder. (If using ASP.Net Core hosted Blazor WASM this file would be in the Server's project folder)
REM Normal build with SIMD and BlazorWebAssemblyJiterpreter enabled (.Net 8 RC 2 defaults) dotnet publish --nologo --configuration Release --output "bin\Publish"REM ReleaseCompat build with SIMD and BlazorWebAssemblyJiterpreter disabled dotnet publish --nologo --no-restore --configuration ReleaseCompat --output "bin\PublishCompat"REM Combine buildsREM Copy the 'wwwroot\_framework' folder contents from the 2nd build to 'wwwroot\_frameworkCompat' in the 1st buildxcopy /I /E /Y "bin\PublishCompat\wwwroot\_framework""bin\Publish\wwwroot\_frameworkCompat"Deploy your modified 1st build. Some extra changes, that are not covered here, would be needed for PWAs with service workers and caching. See Progressive Web Apps below for more information about detecting SIMD in PWAs.
Note: The webworker-enabled attribute on the <script> tags enables those scripts in Web Workers when using SpawnDev.BlazorJS.WebWorkers.
Option 2 above does not work completely with PWAs. Some additional changes are needed to support caching in service workers.
Example modified service-worker.published.js
// WASM feature detection requires an async call so the code is wrapped in an async function.(asyncfunction(){// Caution! Be sure you understand the caveats before publishing an application with// offline support. See https://aka.ms/blazor-offline-considerations// Use SIMD support detection to decide which build assets to cache// Below line is disabled in place of SIMD detection which will determine which assets list to load// self.importScripts('./service-worker-assets.js');self.importScripts('./wasm-feature-detect.1.5.1.js');varsupportsSimd=awaitwasmFeatureDetect.simd();varsupportsExceptions=awaitwasmFeatureDetect.exceptions();varuseCompatMode=!supportsSimd||!supportsExceptions;varserviceWorkerAssetsFile=useCompatMode ? './service-worker-assets-compat.js' : './service-worker-assets.js';self.importScripts(serviceWorkerAssetsFile);if(useCompatMode){// update the url of the assets to use _frameworkCompat/ instead of _framework/self.assetsManifest.assets.forEach(function(o){if(o.url.startsWith('_framework/')){o.url=o.url.replace('_framework/','_frameworkCompat/');}});}// The code below is standard unmodified service worker code from the Blazor WASM templateself.addEventListener('install',event=>event.waitUntil(onInstall(event)));self.addEventListener('activate',event=>event.waitUntil(onActivate(event)));self.addEventListener('fetch',event=>event.respondWith(onFetch(event)));constcacheNamePrefix='offline-cache-';constcacheName=`${cacheNamePrefix}${self.assetsManifest.version}`;constofflineAssetsInclude=[/\.dll$/,/\.pdb$/,/\.wasm/,/\.html/,/\.js$/,/\.json$/,/\.css$/,/\.woff$/,/\.png$/,/\.jpe?g$/,/\.gif$/,/\.ico$/,/\.blat$/,/\.dat$/];constofflineAssetsExclude=[/^service-worker\.js$/];// Replace with your base path if you are hosting on a subfolder. Ensure there is a trailing '/'.constbase="/";constbaseUrl=newURL(base,self.origin);constmanifestUrlList=self.assetsManifest.assets.map(asset=>newURL(asset.url,baseUrl).href);asyncfunctiononInstall(event){console.info('Service worker: Install');// Fetch and cache all matching items from the assets manifestconstassetsRequests=self.assetsManifest.assets.filter(asset=>offlineAssetsInclude.some(pattern=>pattern.test(asset.url))).filter(asset=>!offlineAssetsExclude.some(pattern=>pattern.test(asset.url))).map(asset=>newRequest(asset.url,{integrity: asset.hash,cache: 'no-cache'}));awaitcaches.open(cacheName).then(cache=>cache.addAll(assetsRequests));}asyncfunctiononActivate(event){console.info('Service worker: Activate');// Delete unused cachesconstcacheKeys=awaitcaches.keys();awaitPromise.all(cacheKeys.filter(key=>key.startsWith(cacheNamePrefix)&&key!==cacheName).map(key=>caches.delete(key)));}asyncfunctiononFetch(event){letcachedResponse=null;if(event.request.method==='GET'){// For all navigation requests, try to serve index.html from cache,// unless that request is for an offline resource.// If you need some URLs to be server-rendered, edit the following check to exclude those URLsconstshouldServeIndexHtml=event.request.mode==='navigate'&&!manifestUrlList.some(url=>url===event.request.url);constrequest=shouldServeIndexHtml ? 'index.html' : event.request;constcache=awaitcaches.open(cacheName);cachedResponse=awaitcache.match(request);}returncachedResponse||fetch(event.request);}})()Also, one more command is needed in the publish.bat file to copy over the compatibility build's service-worker-assets.js file.
Updated publish.bat
REM Normal build with SIMD and BlazorWebAssemblyJiterpreter enabled (.Net 8 RC 2 defaults) dotnet publish --nologo --configuration Release --output "bin\Publish"REM ReleaseCompat build with SIMD and BlazorWebAssemblyJiterpreter disabled dotnet publish --nologo --no-restore --configuration ReleaseCompat --output "bin\PublishCompat"REM Combine buildsREM Copy the 'wwwroot\_framework' folder contents from the 2nd build to 'wwwroot\_frameworkCompat' in the 1st buildxcopy /I /E /Y "bin\PublishCompat\wwwroot\_framework""bin\Publish\wwwroot\_frameworkCompat"REM If building a PWA app with server-worker-assets.js the service-worker script needs to be modified to also detect SIMD and cache the appropriate buildREM Copy the service-worker-assets.js from the 2nd build to 'service-worker-assets-compat.js' of the 1st buildcopy /Y "bin\PublishCompat\wwwroot\service-worker-assets.js""bin\Publish\wwwroot\service-worker-assets-compat.js"This project is tested with BrowserStack. "BrowserStack.com loves Open Source." It is a great resource for testing web apps and desktop apps on multiple platforms. Most open source projects, even popular ones, have zero or little funding. BrowserStack supports Open Source with free testing for open source projects.
Test results on various platforms will be listed here once they have been tested.