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

Use hashed string values for type creator lookup #199

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Use hashed string values for type creator lookup",
"packageName": "react-native-xaml",
"email": "[email protected]",
"dependentChangeType": "patch"
}
59 changes: 30 additions & 29 deletions package/Codegen/TypeCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,39 @@ public virtual string TransformText()

}

this.Write(@"**************************************************************/

winrt::Windows::Foundation::IInspectable XamlMetadata::Create(const std::string_view& typeName) const {
wchar_t buf[128]{};
for (size_t i = 0; i < typeName.size() && i < ARRAYSIZE(buf) - 1; i++) {
buf[i] = static_cast<wchar_t>(typeName[i]);
}

HSTRING clsid = nullptr;
if (SUCCEEDED(WindowsCreateString(buf, static_cast<UINT32>(wcslen(buf)), &clsid))) {
winrt::com_ptr<::IInspectable> insp{ nullptr };
if (SUCCEEDED(RoActivateInstance(clsid, insp.put()))) {
winrt::IUnknown unk{ nullptr };
winrt::copy_from_abi(unk, insp.get());
WindowsDeleteString(clsid);
return unk.as<winrt::IInspectable>();
} else {
// type probably has a custom activation factory, use C++/WinRT to create it
WindowsDeleteString(clsid);
clsid = nullptr;
const auto key = COMPILE_TIME_CRC32_STR(typeName.data());
switch (key) {
");
foreach (var t in Types.Where(t => Util.GetComposableFactoryType(t) != null)) {
this.Write(" case COMPILE_TIME_CRC32_STR(\"");
this.Write("**************************************************************/\r\n\r\ntemplate<typen" +
"ame T, typename K, size_t N>\r\nconst T* binary_search_map(const std::pair<K, T> (" +
"& map)[N], size_t low, size_t high, const K& key) {\r\n while (low != high) {\r\n" +
" size_t midpoint = (low + high) / 2;\r\n if (map[midpoint].first == " +
"key) return &(map[midpoint].second);\r\n else if (((high - low) % 2 == 1) &" +
"& map[midpoint + 1].first == key) return &(map[midpoint + 1].second);\r\n e" +
"lse if (key < map[midpoint].first) high = midpoint;\r\n else /*if (key > ma" +
"p[midpoint].first)*/ low = midpoint + 1;\r\n }\r\n if (map[low].first == key) " +
"return &(map[low].second);\r\n return nullptr;\r\n}\r\n\r\nwinrt::Windows::Foundation" +
"::IInspectable XamlMetadata::Create(const std::string_view& typeName) const {\r\n " +
" wchar_t buf[128]{};\r\n for (size_t i = 0; i < typeName.size() && i < ARRAYSIZE(" +
"buf) - 1; i++) {\r\n buf[i] = static_cast<wchar_t>(typeName[i]);\r\n }\r\n\r\n HSTR" +
"ING clsid = nullptr;\r\n if (SUCCEEDED(WindowsCreateString(buf, static_cast<UINT3" +
"2>(wcslen(buf)), &clsid))) {\r\n winrt::com_ptr<::IInspectable> insp{ nullptr }" +
";\r\n if (SUCCEEDED(RoActivateInstance(clsid, insp.put()))) {\r\n winrt::IUn" +
"known unk{ nullptr };\r\n winrt::copy_from_abi(unk, insp.get());\r\n Windo" +
"wsDeleteString(clsid);\r\n return unk.as<winrt::IInspectable>();\r\n } else " +
"{\r\n // type probably has a custom activation factory, use C++/WinRT to crea" +
"te it\r\n WindowsDeleteString(clsid);\r\n clsid = nullptr;\r\n const au" +
"to key = COMPILE_TIME_CRC32_STR(typeName.data());\r\n static constexpr const " +
"std::pair<uint32_t, winrt::Windows::Foundation::IInspectable(*)()> map[] = {\r\n");
foreach (var t in Types.Where(t => Util.GetComposableFactoryType(t) != null).OrderBy(t => Util.GetCRC32(t.GetFullName()))) {
this.Write(" { COMPILE_TIME_CRC32_STR(\"");
this.Write(this.ToStringHelper.ToStringWithCulture(t.GetFullName()));
this.Write("\"): { return ");
this.Write("\"), []() -> winrt::Windows::Foundation::IInspectable { return ");
this.Write(this.ToStringHelper.ToStringWithCulture(Util.GetCppWinRTType(t)));
this.Write("(); }\r\n");
this.Write("(); } }, // 0x");
this.Write(this.ToStringHelper.ToStringWithCulture(Util.GetCRC32(t.GetFullName()).ToString("X8")));
this.Write("\r\n");
}
this.Write(" }\r\n }\r\n }\r\n assert(false && \"xaml type not found\");\r\n return nullptr;\r\n" +
"}\r\n\r\n\r\n");
this.Write(" };\r\n\r\n if (const auto it = binary_search_map(map, 0, std::size(map), k" +
"ey)) {\r\n return (* it)();\r\n }\r\n }\r\n }\r\n assert(false && \"xaml t" +
"ype not found\");\r\n return nullptr;\r\n}\r\n\r\n\r\n");
return this.GenerationEnvironment.ToString();
}
}
Expand Down
33 changes: 25 additions & 8 deletions package/Codegen/TypeCreator.tt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ foreach (var winmd in WinMDs) {
#>
**************************************************************/

template<typename T, typename K, size_t N>
const T* binary_search_map(const std::pair<K, T> (& map)[N], size_t low, size_t high, const K& key) {
while (low != high) {
size_t midpoint = (low + high) / 2;
if (map[midpoint].first == key) return &(map[midpoint].second);
else if (((high - low) % 2 == 1) && map[midpoint + 1].first == key) return &(map[midpoint + 1].second);
else if (key < map[midpoint].first) high = midpoint;
else /*if (key > map[midpoint].first)*/ low = midpoint + 1;
}
if (map[low].first == key) return &(map[low].second);
return nullptr;
}

winrt::Windows::Foundation::IInspectable XamlMetadata::Create(const std::string_view& typeName) const {
wchar_t buf[128]{};
for (size_t i = 0; i < typeName.size() && i < ARRAYSIZE(buf) - 1; i++) {
Expand All @@ -39,15 +52,19 @@ winrt::Windows::Foundation::IInspectable XamlMetadata::Create(const std::string_
WindowsDeleteString(clsid);
return unk.as<winrt::IInspectable>();
} else {
// type probably has a custom activation factory, use C++/WinRT to create it
WindowsDeleteString(clsid);
clsid = nullptr;
const auto key = COMPILE_TIME_CRC32_STR(typeName.data());
switch (key) {
<# foreach (var t in Types.Where(t => Util.GetComposableFactoryType(t) != null)) { #>
case COMPILE_TIME_CRC32_STR("<#= t.GetFullName() #>"): { return <#= Util.GetCppWinRTType(t) #>(); }
// type probably has a custom activation factory, use C++/WinRT to create it
WindowsDeleteString(clsid);
clsid = nullptr;
const auto key = COMPILE_TIME_CRC32_STR(typeName.data());
static constexpr const std::pair<uint32_t, winrt::Windows::Foundation::IInspectable(*)()> map[] = {
<# foreach (var t in Types.Where(t => Util.GetComposableFactoryType(t) != null).OrderBy(t => Util.GetCRC32(t.GetFullName()))) { #>
{ COMPILE_TIME_CRC32_STR("<#= t.GetFullName() #>"), []() -> winrt::Windows::Foundation::IInspectable { return <#= Util.GetCppWinRTType(t) #>(); } }, // 0x<#= Util.GetCRC32(t.GetFullName()).ToString("X8") #>
<# } #>
}
};

if (const auto it = binary_search_map(map, 0, std::size(map), key)) {
return (* it)();
}
}
}
assert(false && "xaml type not found");
Expand Down
10 changes: 10 additions & 0 deletions package/Codegen/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,16 @@ public static string GetEventArgsMethodArgs(MrType argType, string methodName)
return paramTypes;
}

public static UInt32 GetCRC32(string input)
{
var crc32 = 5381u;
for (int i = input.Length - 1; i >=0; i--)
{
crc32 = 33 * crc32 + input[i];
}
return crc32;
}

public static string GetEventArgsMethodReturnType(MrType argType, string methodName)
{
argType.GetMethodsAndConstructors(out var methods, out var ctors);
Expand Down
Loading