From 9347ad43f17cd05edd16c114b53d1531f99c6fe0 Mon Sep 17 00:00:00 2001 From: Ilona Tomkowicz Date: Thu, 1 Jun 2023 15:35:38 +0200 Subject: [PATCH 1/4] revert --- .../Interop/Browser/Interop.Normalization.cs | 17 ---- ...zation.Extensions.Hybrid.WASM.Tests.csproj | 20 ----- .../System.Private.CoreLib.Shared.projitems | 6 +- .../Normalization.WebAssembly.cs | 88 ------------------- .../src/System/Globalization/Normalization.cs | 8 -- src/mono/sample/wasm/browser-bench/String.cs | 21 ----- src/mono/wasm/runtime/corebindings.c | 4 - src/mono/wasm/runtime/es6/dotnet.es6.lib.js | 4 - src/mono/wasm/runtime/exports-linker.ts | 3 - .../hybrid-globalization/normalization.ts | 61 ------------- 10 files changed, 1 insertion(+), 231 deletions(-) delete mode 100644 src/libraries/Common/src/Interop/Browser/Interop.Normalization.cs delete mode 100644 src/libraries/System.Globalization.Extensions/tests/Hybrid/System.Globalization.Extensions.Hybrid.WASM.Tests.csproj delete mode 100644 src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.WebAssembly.cs delete mode 100644 src/mono/wasm/runtime/hybrid-globalization/normalization.ts diff --git a/src/libraries/Common/src/Interop/Browser/Interop.Normalization.cs b/src/libraries/Common/src/Interop/Browser/Interop.Normalization.cs deleted file mode 100644 index b55cf5307d704f..00000000000000 --- a/src/libraries/Common/src/Interop/Browser/Interop.Normalization.cs +++ /dev/null @@ -1,17 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Runtime.CompilerServices; -using System.Text; - -internal static partial class Interop -{ - internal static unsafe partial class JsGlobalization - { - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern unsafe int IsNormalized(NormalizationForm normalizationForm, in string source, out int exceptionalResult, out object result); - - [MethodImplAttribute(MethodImplOptions.InternalCall)] - internal static extern unsafe int NormalizeString(NormalizationForm normalizationForm, in string source, char* dstBuffer, int dstBufferCapacity, out int exceptionalResult, out object result); - } -} diff --git a/src/libraries/System.Globalization.Extensions/tests/Hybrid/System.Globalization.Extensions.Hybrid.WASM.Tests.csproj b/src/libraries/System.Globalization.Extensions/tests/Hybrid/System.Globalization.Extensions.Hybrid.WASM.Tests.csproj deleted file mode 100644 index 0e7c98c19436f5..00000000000000 --- a/src/libraries/System.Globalization.Extensions/tests/Hybrid/System.Globalization.Extensions.Hybrid.WASM.Tests.csproj +++ /dev/null @@ -1,20 +0,0 @@ - - - $(NetCoreAppCurrent)-browser - true - true - - - - - - - - - NormalizationDataWin8 - - - NormalizationDataWin7 - - - diff --git a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems index 2bd91205377528..e5ef0a06cf42b6 100644 --- a/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems +++ b/src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems @@ -371,7 +371,6 @@ - @@ -1256,9 +1255,6 @@ - - Common\Interop\Interop.Normalization.cs - Common\Interop\Interop.CompareInfo.cs @@ -2617,4 +2613,4 @@ - \ No newline at end of file + diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.WebAssembly.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.WebAssembly.cs deleted file mode 100644 index b930f6812904af..00000000000000 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.WebAssembly.cs +++ /dev/null @@ -1,88 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.Buffers; -using System.Diagnostics; -using System.Runtime.InteropServices; -using System.Text; - -namespace System.Globalization -{ - internal static partial class Normalization - { - private static unsafe bool JsIsNormalized(string strInput, NormalizationForm normalizationForm) - { - Debug.Assert(!GlobalizationMode.Invariant); - Debug.Assert(!GlobalizationMode.UseNls); - - ValidateArguments(strInput, normalizationForm); - - int ret = Interop.JsGlobalization.IsNormalized(normalizationForm, strInput, out int exception, out object ex_result); - if (exception != 0) - throw new Exception((string)ex_result); - - return ret == 1; - } - - private static unsafe string JsNormalize(string strInput, NormalizationForm normalizationForm) - { - Debug.Assert(!GlobalizationMode.Invariant); - Debug.Assert(!GlobalizationMode.UseNls); - - ValidateArguments(strInput, normalizationForm); - - char[]? toReturn = null; - try - { - const int StackallocThreshold = 512; - - Span buffer = strInput.Length <= StackallocThreshold - ? stackalloc char[StackallocThreshold] - : (toReturn = ArrayPool.Shared.Rent(strInput.Length)); - - for (int attempt = 0; attempt < 2; attempt++) - { - int realLen; - fixed (char* pDest = &MemoryMarshal.GetReference(buffer)) - { - realLen = Interop.JsGlobalization.NormalizeString(normalizationForm, strInput, pDest, buffer.Length, out int exception, out object ex_result); - if (exception != 0) - throw new Exception((string)ex_result); - } - - if (realLen <= buffer.Length) - { - ReadOnlySpan result = buffer.Slice(0, realLen); - return result.SequenceEqual(strInput) - ? strInput - : new string(result); - } - - Debug.Assert(realLen > StackallocThreshold); - - if (attempt == 0) - { - if (toReturn != null) - { - // Clear toReturn first to ensure we don't return the same buffer twice - char[] temp = toReturn; - toReturn = null; - ArrayPool.Shared.Return(temp); - } - - buffer = toReturn = ArrayPool.Shared.Rent(realLen); - } - } - - throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, nameof(strInput)); - } - finally - { - if (toReturn != null) - { - ArrayPool.Shared.Return(toReturn); - } - } - } - } -} diff --git a/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.cs b/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.cs index 2bc6107b5dad6c..d120302a8aa8e7 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Globalization/Normalization.cs @@ -19,10 +19,6 @@ internal static bool IsNormalized(string strInput, NormalizationForm normalizati return GlobalizationMode.UseNls ? NlsIsNormalized(strInput, normalizationForm) : -#if TARGET_BROWSER - GlobalizationMode.Hybrid ? - JsIsNormalized(strInput, normalizationForm) : -#endif IcuIsNormalized(strInput, normalizationForm); } @@ -37,10 +33,6 @@ internal static string Normalize(string strInput, NormalizationForm normalizatio return GlobalizationMode.UseNls ? NlsNormalize(strInput, normalizationForm) : -#if TARGET_BROWSER - GlobalizationMode.Hybrid ? - JsNormalize(strInput, normalizationForm) : -#endif IcuNormalize(strInput, normalizationForm); } } diff --git a/src/mono/sample/wasm/browser-bench/String.cs b/src/mono/sample/wasm/browser-bench/String.cs index 224604fd692a78..7ce8900b136858 100644 --- a/src/mono/sample/wasm/browser-bench/String.cs +++ b/src/mono/sample/wasm/browser-bench/String.cs @@ -16,9 +16,6 @@ class StringTask : BenchTask public StringTask() { measurements = new Measurement[] { - new NormalizeMeasurement(), - new IsNormalizedMeasurement(), - new NormalizeMeasurementASCII(), new TextInfoToLower(), new TextInfoToUpper(), new TextInfoToTitleCase(), @@ -74,18 +71,6 @@ public override Task AfterBatch() } } - public class NormalizeMeasurement : StringMeasurement - { - public override string Name => "Normalize"; - public override void RunStep() => str.Normalize(); - } - - public class IsNormalizedMeasurement : StringMeasurement - { - public override string Name => "IsNormalized"; - public override void RunStep() => str.IsNormalized(); - } - public abstract class ASCIIStringMeasurement : StringMeasurement { public override Task BeforeBatch() @@ -100,12 +85,6 @@ public override Task BeforeBatch() } } - public class NormalizeMeasurementASCII : ASCIIStringMeasurement - { - public override string Name => "Normalize ASCII"; - public override void RunStep() => str.Normalize(); - } - public class TextInfoMeasurement : StringMeasurement { protected TextInfo textInfo; diff --git a/src/mono/wasm/runtime/corebindings.c b/src/mono/wasm/runtime/corebindings.c index 1a6494a101cf31..a6d0f39deb39d6 100644 --- a/src/mono/wasm/runtime/corebindings.c +++ b/src/mono/wasm/runtime/corebindings.c @@ -50,8 +50,6 @@ extern int mono_wasm_compare_string(MonoString **culture, const uint16_t* str1, extern mono_bool mono_wasm_starts_with(MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, int *is_exception, MonoObject** ex_result); extern mono_bool mono_wasm_ends_with(MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, int *is_exception, MonoObject** ex_result); extern int mono_wasm_index_of(MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options, mono_bool fromBeginning, int *is_exception, MonoObject** ex_result); -extern mono_bool mono_wasm_is_normalized(int32_t normalizationForm, MonoString **src, int *is_exception, MonoObject** ex_result); -extern int mono_wasm_normalize_string(int32_t normalizationForm, MonoString **src, uint16_t* dst, int32_t dstLength, int *is_exception, MonoObject** ex_result); void bindings_initialize_internals (void) { @@ -84,6 +82,4 @@ void bindings_initialize_internals (void) mono_add_internal_call ("Interop/JsGlobalization::StartsWith", mono_wasm_starts_with); mono_add_internal_call ("Interop/JsGlobalization::EndsWith", mono_wasm_ends_with); mono_add_internal_call ("Interop/JsGlobalization::IndexOf", mono_wasm_index_of); - mono_add_internal_call ("Interop/JsGlobalization::IsNormalized", mono_wasm_is_normalized); - mono_add_internal_call ("Interop/JsGlobalization::NormalizeString", mono_wasm_normalize_string); } diff --git a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js index 9a525a57e24507..3f5fcabf626565 100644 --- a/src/mono/wasm/runtime/es6/dotnet.es6.lib.js +++ b/src/mono/wasm/runtime/es6/dotnet.es6.lib.js @@ -112,10 +112,6 @@ let linked_functions = [ "mono_wasm_starts_with", "mono_wasm_ends_with", "mono_wasm_index_of", - "mono_wasm_is_normalized", - "mono_wasm_normalize_string", - "mono_wasm_to_Unicode", - "mono_wasm_to_ASCII", "icudt68_dat", ]; diff --git a/src/mono/wasm/runtime/exports-linker.ts b/src/mono/wasm/runtime/exports-linker.ts index 63d1e98a8d359a..b56fe3684fcc06 100644 --- a/src/mono/wasm/runtime/exports-linker.ts +++ b/src/mono/wasm/runtime/exports-linker.ts @@ -29,7 +29,6 @@ import { } from "./net6-legacy/method-calls"; import { mono_wasm_change_case, mono_wasm_change_case_invariant } from "./hybrid-globalization/change-case"; import { mono_wasm_compare_string, mono_wasm_ends_with, mono_wasm_starts_with, mono_wasm_index_of } from "./hybrid-globalization/collations"; -import { mono_wasm_is_normalized, mono_wasm_normalize_string } from "./hybrid-globalization/normalization"; // the methods would be visible to EMCC linker // --- keep in sync with dotnet.cjs.lib.js --- @@ -105,8 +104,6 @@ export function export_linker(): any { mono_wasm_starts_with, mono_wasm_ends_with, mono_wasm_index_of, - mono_wasm_is_normalized, - mono_wasm_normalize_string, // threading exports, if threading is enabled ...mono_wasm_threads_exports, diff --git a/src/mono/wasm/runtime/hybrid-globalization/normalization.ts b/src/mono/wasm/runtime/hybrid-globalization/normalization.ts deleted file mode 100644 index 2faf9e0b66a992..00000000000000 --- a/src/mono/wasm/runtime/hybrid-globalization/normalization.ts +++ /dev/null @@ -1,61 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -import { mono_wasm_new_external_root } from "../roots"; -import { monoStringToString, stringToUTF16 } from "../strings"; -import { MonoObject, MonoObjectRef, MonoString, MonoStringRef } from "../types/internal"; -import { Int32Ptr } from "../types/emscripten"; -import { wrap_error_root, wrap_no_error_root } from "../invoke-js"; - -const NORMALIZATION_FORM_MAP = [undefined, "NFC", "NFD", undefined, undefined, "NFKC", "NFKD"]; -const ERROR = -1; - -export function mono_wasm_is_normalized(normalizationForm: number, inputStr: MonoStringRef, is_exception: Int32Ptr, ex_address: MonoObjectRef): number { - const inputRoot = mono_wasm_new_external_root(inputStr), - exceptionRoot = mono_wasm_new_external_root(ex_address); - try { - const jsString = monoStringToString(inputRoot); - if (!jsString) - throw new Error("Invalid string was received."); - - const normalization = normalization_to_string(normalizationForm); - const result = jsString.normalize(normalization); - wrap_no_error_root(is_exception, exceptionRoot); - return result === jsString ? 1 : 0; - } - catch (ex) { - wrap_error_root(is_exception, ex, exceptionRoot); - return ERROR; - } finally { - inputRoot.release(); - exceptionRoot.release(); - } -} - -export function mono_wasm_normalize_string(normalizationForm: number, inputStr: MonoStringRef, dstPtr: number, dstLength: number, is_exception: Int32Ptr, ex_address: MonoObjectRef): number { - const inputRoot = mono_wasm_new_external_root(inputStr), - exceptionRoot = mono_wasm_new_external_root(ex_address); - try { - const jsString = monoStringToString(inputRoot); - if (!jsString) - throw new Error("Invalid string was received."); - - const normalization = normalization_to_string(normalizationForm); - const result = jsString.normalize(normalization); - - // increase the dest buffer - if (result.length > dstLength) - return result.length; - stringToUTF16(dstPtr, dstPtr + 2 * dstLength, result); - return result.length; - } catch (ex) { - wrap_error_root(is_exception, ex, exceptionRoot); - return ERROR; - } finally { - inputRoot.release(); - exceptionRoot.release(); - } -} - -const normalization_to_string = (normalizationForm: number): string => NORMALIZATION_FORM_MAP[normalizationForm] ?? "NFC"; - From 5adc8bb578f20825ec8725154a2608fd6de09ad1 Mon Sep 17 00:00:00 2001 From: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com> Date: Fri, 2 Jun 2023 15:01:53 +0200 Subject: [PATCH 2/4] Keep perf tests but with smaller buffers --- src/mono/sample/wasm/browser-bench/String.cs | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/mono/sample/wasm/browser-bench/String.cs b/src/mono/sample/wasm/browser-bench/String.cs index 7ce8900b136858..0f38b4534cfadd 100644 --- a/src/mono/sample/wasm/browser-bench/String.cs +++ b/src/mono/sample/wasm/browser-bench/String.cs @@ -16,6 +16,9 @@ class StringTask : BenchTask public StringTask() { measurements = new Measurement[] { + new NormalizeMeasurement(), + new IsNormalizedMeasurement(), + new NormalizeMeasurementASCII(), new TextInfoToLower(), new TextInfoToUpper(), new TextInfoToTitleCase(), @@ -70,6 +73,20 @@ public override Task AfterBatch() return Task.CompletedTask; } } + + public class NormalizeMeasurement : StringMeasurement + { + protected int len = 8 * 1024; + public override string Name => "Normalize"; + public override void RunStep() => str.Normalize(); + } + + public class IsNormalizedMeasurement : StringMeasurement + { + protected int len = 8 * 1024; + public override string Name => "IsNormalized"; + public override void RunStep() => str.IsNormalized(); + } public abstract class ASCIIStringMeasurement : StringMeasurement { @@ -84,6 +101,13 @@ public override Task BeforeBatch() return Task.CompletedTask; } } + + public class NormalizeMeasurementASCII : ASCIIStringMeasurement + { + protected int len = 8 * 1024; + public override string Name => "Normalize ASCII"; + public override void RunStep() => str.Normalize(); + } public class TextInfoMeasurement : StringMeasurement { From ed7f5767c05bd74fe14b679457188f7eda95341e Mon Sep 17 00:00:00 2001 From: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com> Date: Mon, 5 Jun 2023 07:35:01 +0000 Subject: [PATCH 3/4] Hiding fix. --- src/mono/sample/wasm/browser-bench/String.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mono/sample/wasm/browser-bench/String.cs b/src/mono/sample/wasm/browser-bench/String.cs index 0f38b4534cfadd..a18b0ce63f5d82 100644 --- a/src/mono/sample/wasm/browser-bench/String.cs +++ b/src/mono/sample/wasm/browser-bench/String.cs @@ -76,14 +76,14 @@ public override Task AfterBatch() public class NormalizeMeasurement : StringMeasurement { - protected int len = 8 * 1024; + protected new int len = 8 * 1024; public override string Name => "Normalize"; public override void RunStep() => str.Normalize(); } public class IsNormalizedMeasurement : StringMeasurement { - protected int len = 8 * 1024; + protected new int len = 8 * 1024; public override string Name => "IsNormalized"; public override void RunStep() => str.IsNormalized(); } @@ -104,7 +104,7 @@ public override Task BeforeBatch() public class NormalizeMeasurementASCII : ASCIIStringMeasurement { - protected int len = 8 * 1024; + protected new int len = 8 * 1024; public override string Name => "Normalize ASCII"; public override void RunStep() => str.Normalize(); } From 5bd2c66ce7081a012cb8b525ff11af46eb4ea278 Mon Sep 17 00:00:00 2001 From: Ilona Tomkowicz <32700855+ilonatommy@users.noreply.github.com> Date: Mon, 5 Jun 2023 07:40:30 +0000 Subject: [PATCH 4/4] Whitespaces. --- src/mono/sample/wasm/browser-bench/String.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mono/sample/wasm/browser-bench/String.cs b/src/mono/sample/wasm/browser-bench/String.cs index a18b0ce63f5d82..c00448632fc16d 100644 --- a/src/mono/sample/wasm/browser-bench/String.cs +++ b/src/mono/sample/wasm/browser-bench/String.cs @@ -73,7 +73,7 @@ public override Task AfterBatch() return Task.CompletedTask; } } - + public class NormalizeMeasurement : StringMeasurement { protected new int len = 8 * 1024; @@ -101,7 +101,7 @@ public override Task BeforeBatch() return Task.CompletedTask; } } - + public class NormalizeMeasurementASCII : ASCIIStringMeasurement { protected new int len = 8 * 1024;