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

Reduce allocations for generated constructors #1075

Merged
merged 8 commits into from
Jan 19, 2022
Merged
Changes from 3 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
148 changes: 90 additions & 58 deletions src/cswinrt/code_writers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1227,6 +1227,14 @@ namespace cswinrt
}
}

void write_lazy_interface_type_name(writer& w, type_semantics const& ifaceTypeSemantics)
{
auto interfaceTypeCode = w.write_temp("%", bind<write_type_name>(ifaceTypeSemantics, typedef_name_type::Projected, true));
std::string interfaceTypeName = "_lazy_" + interfaceTypeCode;
std::regex re(R"-((\ |:|<|>|,|\.))-");
w.write("%", std::regex_replace(interfaceTypeName, re, "_"));
}

void write_lazy_interface_initialization(writer& w, TypeDef const& type)
{
int numLazyInterfaces = 0;
Expand All @@ -1247,37 +1255,39 @@ namespace cswinrt
}

numLazyInterfaces++;
auto lazy_interface_name = w.write_temp("%", bind<write_lazy_interface_type_name>(interface_type));
auto interface_name = write_type_name_temp(w, interface_type);
auto interface_abi_name = write_type_name_temp(w, interface_type, "%", typedef_name_type::ABI);

if (settings.netstandard_compat)
{
w.write(R"(
{typeof(%), new Lazy<%>(() => new %(GetReferenceForQI()))},)",
interface_name,
interface_abi_name,
interface_abi_name);
}
else
{
w.write(R"(
{typeof(%), new Lazy<%>(() => (%)(object)new SingleInterfaceOptimizedObject(typeof(%), _inner ?? ((IWinRTObject)this).NativeObject))},)",
interface_name,
interface_name,
auto interface_init_code = settings.netstandard_compat
? w.write_temp(R"(new %(GetReferenceForQI()))",
interface_abi_name)
: w.write_temp(R"((%)(object)new SingleInterfaceOptimizedObject(typeof(%), _inner ?? ((IWinRTObject)this).NativeObject))",
interface_name,
interface_name);
}

w.write(R"(
private volatile % %;
private % Make_%()
{
global::System.Threading.Interlocked.CompareExchange(ref %, %, null);
return %;
}
)",
interface_name,
lazy_interface_name,
interface_name,
lazy_interface_name,
lazy_interface_name,
interface_init_code,
lazy_interface_name);
});
}
});

if (numLazyInterfaces != 0)
{
w.write(R"(
_lazyInterfaces = new Dictionary<Type, object>(%)
{%
};
)", numLazyInterfaces, lazyInterfaces);
w.write(R"(%)", lazyInterfaces);
}
}

Expand Down Expand Up @@ -1876,7 +1886,9 @@ global::System.Collections.Concurrent.ConcurrentDictionary<RuntimeTypeHandle, ob
for (auto&& method : factory_type.MethodList())
{
method_signature signature{ method };
w.write(R"(
if (settings.netstandard_compat)
{
w.write(R"(
%public %(%) : this(((Func<%>)(() => {
IntPtr ptr = (%.%(%));
try
Expand All @@ -1889,18 +1901,49 @@ MarshalInspectable<object>.DisposeAbi(ptr);
}
}))())
{
)",
platform_attribute,
class_type.TypeName(),
bind_list<write_projection_parameter>(", ", signature.params()),
default_interface_name,
cache_object,
method.Name(),
bind_list<write_parameter_name_with_modifier>(", ", signature.params()),
"new " + default_interface_name
);
}
else
{

bool has_base_type = !std::holds_alternative<object_type>(get_type_semantics(class_type.Extends()));

w.write(R"(
%public %(%) %
{
IntPtr ptr = (%.%(%));
try
{
_inner = ComWrappersSupport.GetObjectReferenceForInterface(ptr);
}
finally
{
MarshalInspectable<object>.DisposeAbi(ptr);
}
)",
platform_attribute,
class_type.TypeName(),
bind_list<write_projection_parameter>(", ", signature.params()),
has_base_type ? ":base(global::WinRT.DerivedComposed.Instance)" : "",
cache_object,
method.Name(),
bind_list<write_parameter_name_with_modifier>(", ", signature.params())
);
}
w.write(R"(
ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr);
%
}
)",
platform_attribute,
class_type.TypeName(),
bind_list<write_projection_parameter>(", ", signature.params()),
settings.netstandard_compat ? default_interface_name : "IObjectReference",
cache_object,
method.Name(),
bind_list<write_parameter_name_with_modifier>(", ", signature.params()),
settings.netstandard_compat ? "new " + default_interface_name : "",
settings.netstandard_compat ? "" : "ComWrappersHelper.Init(_inner, false);");
}
}
Expand Down Expand Up @@ -2051,8 +2094,6 @@ try
_inner = ComWrappersSupport.GetObjectReferenceForInterface(ptr);
var defaultInterface = new %(_inner);
_defaultLazy = new Lazy<%>(() => defaultInterface);
%

ComWrappersSupport.RegisterObjectForInterface(this, ThisPtr);
}
finally
Expand All @@ -2071,8 +2112,7 @@ MarshalInspectable<object>.DisposeAbi(ptr);
bind_list<write_parameter_name_with_modifier>(", ", params_without_objects),
[&](writer& w) {w.write("%", params_without_objects.empty() ? " " : ", "); },
default_interface_abi_name,
default_interface_abi_name,
bind<write_lazy_interface_initialization>(class_type));
default_interface_abi_name);
}
else
{
Expand All @@ -2088,7 +2128,6 @@ try
{
ComWrappersHelper.Init(isAggregation, this, composed, inner, out _inner);
%
%
}
finally
{
Expand All @@ -2112,8 +2151,7 @@ Marshal.Release(inner);
{
w.write("_defaultLazy = new Lazy<%>(() => (%)new SingleInterfaceOptimizedObject(typeof(%), _inner));", default_interface_name, default_interface_name, default_interface_name);
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
}
}),
bind<write_lazy_interface_initialization>(class_type));
}));
}
}
}
Expand Down Expand Up @@ -2650,22 +2688,20 @@ remove => %.ErrorsChanged -= value;
if (settings.netstandard_compat)
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
{
w.write(R"(
private % AsInternal(InterfaceTag<%> _) => ((Lazy<%>)_lazyInterfaces[typeof(%)]).Value;
private % AsInternal(InterfaceTag<%> _) => Make_%();
)",
interface_name,
interface_name,
interface_abi_name,
interface_name);
bind<write_lazy_interface_type_name>(interface_type));
}
else if (is_manually_generated_iface(interface_type))
{
w.write(R"(
private % AsInternal(InterfaceTag<%> _) => ((Lazy<%>)_lazyInterfaces[typeof(%)]).Value;
private % AsInternal(InterfaceTag<%> _) => Make_%();
j0shuams marked this conversation as resolved.
Show resolved Hide resolved
)",
interface_name,
interface_name,
interface_name,
interface_name);
bind<write_lazy_interface_type_name>(interface_type));
}
}

Expand Down Expand Up @@ -5897,7 +5933,7 @@ public %IntPtr ThisPtr => _default.ThisPtr;

private IObjectReference _inner = null;
private readonly Lazy<%> _defaultLazy;
private readonly Dictionary<Type, object> _lazyInterfaces;
%

private % _default => _defaultLazy.Value;
%
Expand All @@ -5910,7 +5946,6 @@ return MarshalInspectable<%>.FromAbi(thisPtr);
% %(% ifc)%
{
_defaultLazy = new Lazy<%>(() => ifc);
%
%}
%

Expand All @@ -5936,6 +5971,7 @@ private % AsInternal(InterfaceTag<%> _) => _default;
type_name,
derived_new,
default_interface_abi_name,
bind<write_lazy_interface_initialization>(type),
default_interface_abi_name,
bind<write_attributed_types>(type),
derived_new,
Expand All @@ -5946,7 +5982,7 @@ private % AsInternal(InterfaceTag<%> _) => _default;
default_interface_abi_name,
bind<write_base_constructor_dispatch_netstandard>(base_semantics),
default_interface_abi_name,
bind<write_lazy_interface_initialization>(type),

[&](writer& w)
{
if (!gc_pressure_amount) return;
Expand Down Expand Up @@ -5979,13 +6015,11 @@ GC.RemoveMemoryPressure(%);
protected %(global::WinRT.DerivedComposed _)%
{
_defaultLazy = new Lazy<%>(() => GetDefaultReference<%.Vftbl>());
%
})",
type.TypeName(),
has_base_type ? ":base(_)" : "",
default_interface_abi_name,
default_interface_abi_name,
bind<write_lazy_interface_initialization>(type));
default_interface_abi_name);
}

std::string_view access_spec = "protected ";
Expand Down Expand Up @@ -6047,7 +6081,7 @@ private IntPtr ThisPtr => _inner == null ? (((IWinRTObject)this).NativeObject).T

private IObjectReference _inner = null;
%
private readonly Dictionary<Type, object> _lazyInterfaces;
%

%
%
Expand All @@ -6062,7 +6096,6 @@ return MarshalInspectable<%>.FromAbi(thisPtr);
{
_inner = objRef.As(GuidGenerator.GetIID(typeof(%).GetHelperType()));
%
%
}

public static bool operator ==(% x, % y) => (x?.ThisPtr ?? IntPtr.Zero) == (y?.ThisPtr ?? IntPtr.Zero);
Expand All @@ -6084,6 +6117,7 @@ private struct InterfaceTag<I>{};
type_name,
bind<write_type_inheritance>(type, base_semantics, true, false),
type_name,
bind<write_lazy_interface_initialization>(type),
bind([&](writer& w)
{
if (is_manually_gen_default_interface)
Expand Down Expand Up @@ -6113,7 +6147,7 @@ private struct InterfaceTag<I>{};
w.write("_defaultLazy = new Lazy<%>(() => (%)new SingleInterfaceOptimizedObject(typeof(%), _inner));", default_interface_name, default_interface_name, default_interface_name);
}
}),
bind<write_lazy_interface_initialization>(type),

// Equality operators
type_name,
type_name,
Expand All @@ -6130,18 +6164,16 @@ private struct InterfaceTag<I>{};
protected %(global::WinRT.DerivedComposed _)%
{
%
%
})",
type.TypeName(),
has_base_type ? ":base(_)" : "",
bind([&](writer& w)
{
if (is_manually_gen_default_interface)
{
if (is_manually_gen_default_interface)
{
w.write("_defaultLazy = new Lazy<%>(() => (%)new IInspectable(((IWinRTObject)this).NativeObject));", default_interface_name, default_interface_name);
}
}),
bind<write_lazy_interface_initialization>(type));
w.write("_defaultLazy = new Lazy<%>(() => (%)new IInspectable(((IWinRTObject)this).NativeObject));", default_interface_name, default_interface_name);
}
}));
w.write(R"(
bool IWinRTObject.HasUnwrappableNativeObject => this.GetType() == typeof(%);)",
type.TypeName());
Expand Down