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

[browser][non-icu] HybridGlobalization support interning #84695

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/design/features/hybrid-globalization.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Hybrid mode does not use ICU data for some functions connected with globalizatio

### WASM

For WebAssembly in Browser we are using Web API instead of some ICU data.
For WebAssembly in Browser we are using Web API instead of some ICU data. Ideally, we would use `System.Runtime.InteropServices.JavaScript` to call JS code from inside of C# but we cannot reference any assemblies from inside of `System.Private.CoreLib`. That is why we are using iCalls for that.

**Case change**

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ internal static partial class Interop
internal static unsafe partial class JsGlobalization
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern unsafe int CompareString(out string exceptionMessage, in string culture, char* str1, int str1Len, char* str2, int str2Len, global::System.Globalization.CompareOptions options);
internal static extern unsafe int CompareString(out string exceptionMessage, in string culture, in string str1, in string str2, global::System.Globalization.CompareOptions options);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace System.Globalization
{
public partial class CompareInfo
{
private unsafe int JsCompareString(ReadOnlySpan<char> string1, ReadOnlySpan<char> string2, CompareOptions options)
private unsafe int JsCompareString(ReadOnlySpan<char> span1, ReadOnlySpan<char> span2, CompareOptions options)
{
Debug.Assert(!GlobalizationMode.Invariant);
Debug.Assert(!GlobalizationMode.UseNls);
Expand All @@ -25,12 +25,7 @@ private unsafe int JsCompareString(ReadOnlySpan<char> string1, ReadOnlySpan<char
throw new PlatformNotSupportedException(GetPNSEForCulture(options, cultureName));

string exceptionMessage;
int cmpResult;
fixed (char* pString1 = &MemoryMarshal.GetReference(string1))
fixed (char* pString2 = &MemoryMarshal.GetReference(string2))
{
cmpResult = Interop.JsGlobalization.CompareString(out exceptionMessage, cultureName, pString1, string1.Length, pString2, string2.Length, options);
}
int cmpResult = Interop.JsGlobalization.CompareString(out exceptionMessage, cultureName, span1.ToString(), span2.ToString(), options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this going to allocate temporary C# strings in order to pass them to JS? This will break interning too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how to do it differently, if we want to operate on strings we need to at least allocate a local string in the function. We cannot change the fact that arguments enter in a form of ReadOnlySpan<char> and it looks like span, passing the C#/JS border is treated the same way as char*.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we have speed vs (icu data) size tradeoff here. If they choose hybrid, they would get size, but not speed.
Seems OK to me if the perf is around %200.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the past mesurements on normalization function it was more than that, around 500%. It is a good idea to start measuring how HG does in comparison to ICU4C.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure how to do it differently, if we want to operate on strings we need to at least allocate a local string in the function. We cannot change the fact that arguments enter in a form of ReadOnlySpan<char> and it looks like span, passing the C#/JS border is treated the same way as char*.

In that case for this the way you did it previously (passing pointers) is the only reasonable way, making a temp C#/MonoString is not going to help.


if (!string.IsNullOrEmpty(exceptionMessage))
throw new Exception(exceptionMessage);
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/runtime/corebindings.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern void* mono_wasm_invoke_js_blazor (MonoString **exceptionMessage, void *ca
// HybridGlobalization
extern void mono_wasm_change_case_invariant(MonoString **exceptionMessage, const uint16_t* src, int32_t srcLength, uint16_t* dst, int32_t dstLength, mono_bool bToUpper);
extern void mono_wasm_change_case(MonoString **exceptionMessage, MonoString **culture, const uint16_t* src, int32_t srcLength, uint16_t* dst, int32_t dstLength, mono_bool bToUpper);
extern int mono_wasm_compare_string(MonoString **exceptionMessage, MonoString **culture, const uint16_t* str1, int32_t str1Length, const uint16_t* str2, int32_t str2Length, int32_t options);
extern int mono_wasm_compare_string(MonoString **exceptionMessage, MonoString **culture, MonoString **str1, MonoString **str2, int32_t options);

void bindings_initialize_internals (void)
{
Expand Down
14 changes: 10 additions & 4 deletions src/mono/wasm/runtime/net6-legacy/hybrid-globalization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,18 @@ export function mono_wasm_change_case(exceptionMessage: Int32Ptr, culture: MonoS
}
}

export function mono_wasm_compare_string(exceptionMessage: Int32Ptr, culture: MonoStringRef, str1: number, str1Length: number, str2: number, str2Length: number, options: number) : number{
export function mono_wasm_compare_string(exceptionMessage: Int32Ptr, culture: MonoStringRef, str1: MonoStringRef, str2: MonoStringRef, options: number) : number{
const cultureRoot = mono_wasm_new_external_root<MonoString>(culture);
const str1Root = mono_wasm_new_external_root<MonoString>(str1);
const str2Root = mono_wasm_new_external_root<MonoString>(str2);
try{
const string1 = conv_string_root(str1Root);
const string2 = conv_string_root(str2Root);
if (string1 === null || string2 === null)
throw new Error("$String conversion failed.");
const cultureName = conv_string_root(cultureRoot);
const string1 = get_utf16_string(str1, str1Length);
const string2 = get_utf16_string(str2, str2Length);
const casePicker = (options & 0x1f);
const locale = cultureName ? cultureName : undefined;
const casePicker = (options & 0x1f);
const result = compare_strings(string1, string2, locale, casePicker);
if (result == -2)
throw new Error("$Invalid comparison option.");
Expand All @@ -66,6 +70,8 @@ export function mono_wasm_compare_string(exceptionMessage: Int32Ptr, culture: Mo
}
finally {
cultureRoot.release();
str1Root.release();
str2Root.release();
}
}

Expand Down