Skip to content

Commit

Permalink
使用 ulong 代替字符串,节省一点 IPC 传输性能
Browse files Browse the repository at this point in the history
  • Loading branch information
walterlv committed Feb 10, 2023
1 parent 8493392 commit 1ed2ee8
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public MemberDeclarationSourceTextBuilder GenerateProxyMember(SourceTextBuilder
Task {methodContainingTypeName}.{_contractMethod.Name}({parameters})
{{
return CallMethodAsync(""{methodId}"", new Garm<object?>[] {{ {arguments} }}, {namedValues});
return CallMethodAsync({methodId}, new Garm<object?>[] {{ {arguments} }}, {namedValues});
}}
",
Expand All @@ -93,7 +93,7 @@ public MemberDeclarationSourceTextBuilder GenerateProxyMember(SourceTextBuilder
Task<{returnTypeName}> {methodContainingTypeName}.{_contractMethod.Name}({parameters})
{{
return CallMethodAsync<{returnTypeName}>(""{methodId}"", new Garm<object?>[] {{ {arguments} }}, {namedValues});
return CallMethodAsync<{returnTypeName}>({methodId}, new Garm<object?>[] {{ {arguments} }}, {namedValues});
}}
",
Expand All @@ -102,19 +102,19 @@ public MemberDeclarationSourceTextBuilder GenerateProxyMember(SourceTextBuilder
? @$"
void {methodContainingTypeName}.{_contractMethod.Name}({parameters})
{{
CallMethod(""{methodId}"", new Garm<object?>[] {{ {arguments} }}, {namedValues}).Wait();
CallMethod({methodId}, new Garm<object?>[] {{ {arguments} }}, {namedValues}).Wait();
}}"
: @$"
void {methodContainingTypeName}.{_contractMethod.Name}({parameters})
{{
_ = CallMethod(""{methodId}"", new Garm<object?>[] {{ {arguments} }}, {namedValues});
_ = CallMethod({methodId}, new Garm<object?>[] {{ {arguments} }}, {namedValues});
}}",

// 同步 T 方法。
(false, _) => $@"
{returnTypeName} {methodContainingTypeName}.{_contractMethod.Name}({parameters})
{{
return CallMethod<{returnTypeName}>(""{methodId}"", new Garm<object?>[] {{ {arguments} }}, {namedValues}).Result;
return CallMethod<{returnTypeName}>({methodId}, new Garm<object?>[] {{ {arguments} }}, {namedValues}).Result;
}}
",
}
Expand Down Expand Up @@ -150,7 +150,7 @@ public MemberDeclarationSourceTextBuilder GenerateShapeMember(SourceTextBuilder
/// <returns>方法源代码。</returns>
public string GenerateJointMatch(SourceTextBuilder builder, string real)
{
var memberId = MemberIdGenerator.GenerateMethodId(_contractMethod);
var methodId = MemberIdGenerator.GenerateMethodId(_contractMethod);
var containingTypeName = builder.SimplifyNameByAddUsing(_contractMethod.ContainingType);
var parameterTypes = GenerateMethodParameterTypes(builder, _contractMethod.Parameters);
var arguments = GenerateMethodArguments(_contractMethod.Parameters);
Expand All @@ -164,8 +164,8 @@ public string GenerateJointMatch(SourceTextBuilder builder, string real)
// 异步 Task 方法。
var call = $"{real}.{_contractMethod.Name}({arguments})";
var sourceCode = string.IsNullOrWhiteSpace(arguments)
? $"MatchMethod(\"{memberId}\", new Func<Task>(() => {call}));"
: $"MatchMethod(\"{memberId}\", new Func<{parameterTypes}, Task>(({arguments}) => {call}));";
? $"MatchMethod({methodId}, new Func<Task>(() => {call}));"
: $"MatchMethod({methodId}, new Func<{parameterTypes}, Task>(({arguments}) => {call}));";
return sourceCode;
}
else if (isAsync && !returnsVoid)
Expand All @@ -174,17 +174,17 @@ public string GenerateJointMatch(SourceTextBuilder builder, string real)
var @return = $"Task<Garm<{returnTypeName}>>";
var call = GenerateGarmReturn(builder, asyncReturnType!, $"await {real}.{_contractMethod.Name}({arguments}).ConfigureAwait(false)");
var sourceCode = string.IsNullOrWhiteSpace(arguments)
? $"MatchMethod(\"{memberId}\", new Func<{@return}>(async () => {call}));"
: $"MatchMethod(\"{memberId}\", new Func<{parameterTypes}, {@return}>(async ({arguments}) => {call}));";
? $"MatchMethod({methodId}, new Func<{@return}>(async () => {call}));"
: $"MatchMethod({methodId}, new Func<{parameterTypes}, {@return}>(async ({arguments}) => {call}));";
return sourceCode;
}
else if (!isAsync && returnsVoid)
{
// 同步 void 方法。
var call = $"{real}.{_contractMethod.Name}({arguments})";
var sourceCode = string.IsNullOrWhiteSpace(arguments)
? $"MatchMethod(\"{memberId}\", new Action(() => {call}));"
: $"MatchMethod(\"{memberId}\", new Action<{parameterTypes}>(({arguments}) => {call}));";
? $"MatchMethod({methodId}, new Action(() => {call}));"
: $"MatchMethod({methodId}, new Action<{parameterTypes}>(({arguments}) => {call}));";
return sourceCode;
}
else
Expand All @@ -193,8 +193,8 @@ public string GenerateJointMatch(SourceTextBuilder builder, string real)
var @return = $"Garm<{returnTypeName}>";
var call = GenerateGarmReturn(builder, _contractMethod.ReturnType, $"{real}.{_contractMethod.Name}({arguments})");
var sourceCode = string.IsNullOrWhiteSpace(arguments)
? $"MatchMethod(\"{memberId}\", new Func<{@return}>(() => {call}));"
: $"MatchMethod(\"{memberId}\", new Func<{parameterTypes}, {@return}>(({arguments}) => {call}));";
? $"MatchMethod({methodId}, new Func<{@return}>(() => {call}));"
: $"MatchMethod({methodId}, new Func<{parameterTypes}, {@return}>(({arguments}) => {call}));";
return sourceCode;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ public MemberDeclarationSourceTextBuilder GenerateProxyMember(SourceTextBuilder
{propertyTypeName} {containingTypeName}.{_contractProperty.Name}
{{
get => GetValueAsync<{propertyTypeName}>(""{getMemberId}"", {namedValues}).Result;
set => SetValueAsync<{propertyTypeName}>(""{setMemberId}"", {valueArgumentName}, {namedValues}).Wait();
get => GetValueAsync<{propertyTypeName}>({getMemberId}, {namedValues}).Result;
set => SetValueAsync<{propertyTypeName}>({setMemberId}, {valueArgumentName}, {namedValues}).Wait();
}}
",
// get 属性。
(true, false) => $@"
{propertyTypeName} {containingTypeName}.{_contractProperty.Name} => GetValueAsync<{propertyTypeName}>(""{getMemberId}"", {namedValues}).Result;
{propertyTypeName} {containingTypeName}.{_contractProperty.Name} => GetValueAsync<{propertyTypeName}>({getMemberId}, {namedValues}).Result;
",
// 不支持 set 属性。
Expand Down Expand Up @@ -142,12 +142,12 @@ public string GenerateJointMatch(SourceTextBuilder builder, string real)
var (hasGet, hasSet) = (_contractProperty.GetMethod is not null, _contractProperty.SetMethod is not null);
if (hasGet && hasSet)
{
var sourceCode = $"MatchProperty(\"{getMemberId}\", \"{setMemberId}\", new Func<{garmPropertyTypeName}>(() => {garmPropertyArgumentName}), new Action<{propertyTypeName}>(value => {real}.{_contractProperty.Name} = value));";
var sourceCode = $"MatchProperty({getMemberId}, {setMemberId}, new Func<{garmPropertyTypeName}>(() => {garmPropertyArgumentName}), new Action<{propertyTypeName}>(value => {real}.{_contractProperty.Name} = value));";
return sourceCode;
}
else if (hasGet)
{
var sourceCode = $"MatchProperty(\"{getMemberId}\", new Func<{garmPropertyTypeName}>(() => {garmPropertyArgumentName}));";
var sourceCode = $"MatchProperty({getMemberId}, new Func<{garmPropertyTypeName}>(() => {garmPropertyArgumentName}));";
return sourceCode;
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ private static string CalculateHash(string text)
var inputBytes = Encoding.UTF8.GetBytes(text);
var hashBytes = sha256.ComputeHash(inputBytes);

return BitConverter.ToString(hashBytes)
.Replace("-", "")
.Substring(0, 16)
.ToLower(CultureInfo.InvariantCulture);
return $"0x{BitConverter.ToInt64(hashBytes, 0).ToString("X")}";
}
}
Loading

0 comments on commit 1ed2ee8

Please sign in to comment.