Skip to content

Commit

Permalink
delete unused CALLT method token in nef (#1222)
Browse files Browse the repository at this point in the history
* delete unused CALLT method token in nef

* accelerate for zero token
  • Loading branch information
Hecate2 authored Nov 5, 2024
1 parent dc63c47 commit 00b678f
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 4 deletions.
80 changes: 80 additions & 0 deletions src/Neo.Compiler.CSharp/Optimizer/Strategies/Miscellaneous.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using Neo.Json;
using Neo.SmartContract;
using Neo.SmartContract.Manifest;
using Neo.VM;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Neo.Optimizer
{
public static class Miscellaneous
{
/// <summary>
/// If any method token in nef is not utilized by CALLT, remove the method token.
/// </summary>
/// <param name="nef"></param>
/// <param name="manifest"></param>
/// <param name="debugInfo"></param>
/// <returns></returns>
[Strategy(Priority = int.MinValue)]
public static (NefFile, ContractManifest, JObject?) RemoveMethodToken(NefFile nef, ContractManifest manifest, JObject? debugInfo = null)
{
if (nef.Tokens.Length == 0)
return (nef, manifest, debugInfo);
List<bool> oldTokenNeeded = Enumerable.Repeat(false, nef.Tokens.Length).ToList();
Script script = nef.Script;
List<(int a, Instruction i)> oldAddressAndInstructionsList = script.EnumerateInstructions().ToList();
foreach ((_, Instruction i) in oldAddressAndInstructionsList)
if (i.OpCode == OpCode.CALLT)
// Possibly i.TokenU16 >= result.Count
// In this case the CALLT is invalid. We just let it throw exceptions.
oldTokenNeeded[i.TokenU16] = true;
Dictionary<int, int> oldTokenIdToNew = new();
List<MethodToken> newTokens = new();
for (int i = 0; i < oldTokenNeeded.Count; ++i)
{
if (oldTokenNeeded[i])
newTokens.Add(nef.Tokens[i]);
oldTokenIdToNew.Add(i, newTokens.Count - 1);
}
nef.Tokens = newTokens.ToArray();
if (newTokens.Count == 0 || newTokens.Count == oldTokenNeeded.Count)
{// all tokens deleted, or no token deleted
nef.CheckSum = NefFile.ComputeChecksum(nef);
return (nef, manifest, debugInfo);
}
// else: some operand of CALLT should be changed
Dictionary<int, Instruction> oldAddressToInstruction = oldAddressAndInstructionsList.ToDictionary(e => e.a, e => e.i);
(Dictionary<Instruction, Instruction> jumpSourceToTargets,
Dictionary<Instruction, (Instruction, Instruction)> trySourceToTargets,
Dictionary<Instruction, HashSet<Instruction>> jumpTargetToSources) =
JumpTarget.FindAllJumpAndTrySourceToTargets(oldAddressAndInstructionsList);
Dictionary<int, int> oldSequencePointAddressToNew = new();
System.Collections.Specialized.OrderedDictionary simplifiedInstructionsToAddress = new();
int newAddr = 0;
foreach ((int a, Instruction i) in oldAddressAndInstructionsList)
{
if (i.OpCode == OpCode.CALLT && oldTokenIdToNew[i.TokenU16] != i.TokenU16)
{
IEnumerable<byte> newInstructionInBytes = [(byte)OpCode.CALLT];
newInstructionInBytes = newInstructionInBytes.Concat(BitConverter.GetBytes(oldTokenIdToNew[i.TokenU16])[0..2]);
Instruction newInstruction = new Script(newInstructionInBytes.ToArray()).GetInstruction(0);
simplifiedInstructionsToAddress.Add(newInstruction, newAddr);
oldSequencePointAddressToNew.Add(a, newAddr);
newAddr += i.Size;
OptimizedScriptBuilder.RetargetJump(i, newInstruction,
jumpSourceToTargets, trySourceToTargets, jumpTargetToSources);
continue;
}
simplifiedInstructionsToAddress.Add(i, newAddr);
newAddr += i.Size;
}
return AssetBuilder.BuildOptimizedAssets(nef, manifest, debugInfo,
simplifiedInstructionsToAddress,
jumpSourceToTargets, trySourceToTargets,
oldAddressToInstruction,
oldSequencePointAddressToNew: oldSequencePointAddressToNew);
}
}
}
1 change: 1 addition & 0 deletions src/Neo.Compiler.CSharp/Optimizer/Strategies/Optimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public static (NefFile, ContractManifest, JObject?) Optimize(NefFile nef, Contra
(nef, manifest, debugInfo) = JumpCompresser.ReplaceJumpWithRet(nef, manifest, debugInfo);
(nef, manifest, debugInfo) = Reachability.RemoveMultiRet(nef, manifest, debugInfo);
(nef, manifest, debugInfo) = JumpCompresser.RemoveUnnecessaryJumps(nef, manifest, debugInfo);
(nef, manifest, debugInfo) = Miscellaneous.RemoveMethodToken(nef, manifest, debugInfo);
(nef, manifest, debugInfo) = JumpCompresser.CompressJump(nef, manifest, debugInfo);
return (nef, manifest, debugInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public abstract class Contract_ByteString(Neo.SmartContract.Testing.SmartContrac
/// Unsafe method
/// </summary>
/// <remarks>
/// Script: DQABAgMEBQYHCAkKCwwNDg8QERITFBUWFxgZGhscHR4fICEiIyQlJicoKSorLC0uLzAxMjM0NTY3ODk6Ozw9Pj9AQUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVpbXF1eX2BhYmNkZWZnaGlqa2xtbm9wcXJzdHV2d3h5ent8fX5/gIGCg4SFhoeIiYqLjI2Oj5CRkpOUlZaXmJmam5ydnp+goaKjpKWmp6ipqqusra6vsLGys7S1tre4ubq7vL2+v8DBwsPExcbHyMnKy8zNzs/Q0dLT1NXW19jZ2tvc3d7f4OHi4+Tl5ufo6err7O3u7/Dx8vP09fb3+Pn6+/z9/v9A
/// Script: DQABAAECAwQFBgcICQoLDA0ODxAREhMUFRYXGBkaGxwdHh8gISIjJCUmJygpKissLS4vMDEyMzQ1Njc4OTo7PD0+P0BBQkNERUZHSElKS0xNTk9QUVJTVFVWV1hZWltcXV5fYGFiY2RlZmdoaWprbG1ub3BxcnN0dXZ3eHl6e3x9fn+AgYKDhIWGh4iJiouMjY6PkJGSk5SVlpeYmZqbnJ2en6ChoqOkpaanqKmqq6ytrq+wsbKztLW2t7i5uru8vb6/wMHCw8TFxsfIycrLzM3Oz9DR0tPU1dbX2Nna29zd3t/g4eLj5OXm5+jp6uvs7e7v8PHy8/T19vf4+fr7/P3+/0A=
/// 0000 : OpCode.PUSHDATA2 000102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F606162636465666768696A6B6C6D6E6F707172737475767778797A7B7C7D7E7F808182838485868788898A8B8C8D8E8F909192939495969798999A9B9C9D9E9FA0A1A2A3A4A5A6A7A8A9AAABACADAEAFB0B1B2B3B4B5B6B7B8B9BABBBCBDBEBFC0C1C2C3C4C5C6C7C8C9CACBCCCDCECFD0D1D2D3D4D5D6D7D8D9DADBDCDDDEDFE0E1E2E3E4E5E6E7E8E9EAEBECEDEEEFF0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF [512 datoshi]
/// 0103 : OpCode.RET [0 datoshi]
/// </remarks>
Expand All @@ -36,7 +36,7 @@ public abstract class Contract_ByteString(Neo.SmartContract.Testing.SmartContrac
/// Unsafe method
/// </summary>
/// <remarks>
/// Script: DOS9oOWlvQDCgMO/QA==
/// Script: DAvkvaDlpb0AwoDDv0A=
/// 00 : OpCode.PUSHDATA1 E4BDA0E5A5BD00C280C3BF [8 datoshi]
/// 0D : OpCode.RET [0 datoshi]
/// </remarks>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class Contract_NEP11(Neo.SmartContract.Testing.SmartContractInit
/// <summary>
/// Optimization: "All"
/// </summary>
public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable<Neo.SmartContract.NefFile>(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATA7znO4OTpJcbCoGp54UQN2G/OrAtkZXNlcmlhbGl6ZQEAAQ/A7znO4OTpJcbCoGp54UQN2G/OrAlzZXJpYWxpemUBAAEP/aP6Q0bqUyolj8SX3a3bZDfJ/f8LZ2V0Q29udHJhY3QBAAEPG/V1qxGJaIQTYQo1oSiGzeC2bHIGc2hhMjU2AQABDwAA/T4DDARURVNUQBBAWNgmFwwBAEH2tGviQZJd6DFK2CYERRBKY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IGFyZ3VtZW50ICJ0byIgaXMgaW52YWxpZC46ExGIThBR0EGb9mfOEsBweWjBRVOLUEGSXegxNwAAcWkQznJqQfgn7IwkBAlAaniYJiV4SmkQUdBFaTcBAEp5aMFFU4tQQeY/GIRFD3lqNA8ReXg0Cnp5eGo0RQhAVwIDeng1z/3//0VBm/ZnzhQRiE4QUdBQEsBweHmL2yhxehC3JhAQaWjBRVOLUEHmPxiEQGlowUVTi1BBL1jF7UBXAQR6EXl4FMAMCFRyYW5zZmVyQZUBb2F5cGjYJgUJIgp5NwIAcGjYqiYge3oReBTAHwwOb25ORVAxMVBheW1lbnR5QWJ9W1JFQFYBQPYrNes="));

#endregion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class Contract_SupportedStandard11Enum(Neo.SmartContract.Testing
/// <summary>
/// Optimization: "All"
/// </summary>
public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable<Neo.SmartContract.NefFile>(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAATA7znO4OTpJcbCoGp54UQN2G/OrAtkZXNlcmlhbGl6ZQEAAQ/A7znO4OTpJcbCoGp54UQN2G/OrAlzZXJpYWxpemUBAAEP/aP6Q0bqUyolj8SX3a3bZDfJ/f8LZ2V0Q29udHJhY3QBAAEPG/V1qxGJaIQTYQo1oSiGzeC2bHIGc2hhMjU2AQABDwAA/T4DQBBAWNgmFwwBAEH2tGviQZJd6DFK2CYERRBKYEBXAQF4cGjYJgUIIg14StkoUMoAFLOrqiYlDCBUaGUgYXJndW1lbnQgIm93bmVyIiBpcyBpbnZhbGlkLjpBm/ZnzhERiE4QUdBQEsBweGjBRVOLUEGSXegxStgmBEUQ2yFAVwICQZv2Z84REYhOEFHQUBLAcHhowUVTi1BBkl3oMUrYJgRFENshcWl5nnFpELUmBAlAabEkEHhowUVTi1BBL1jF7SIPaXhowUVTi1BB5j8YhAhAVwMBeMoAQLcmPAw3VGhlIGFyZ3VtZW50ICJ0b2tlbklkIiBzaG91bGQgYmUgNjQgb3IgbGVzcyBieXRlcyBsb25nLjoTEYhOEFHQQZv2Z84SwHB4aMFFU4tQQZJd6DFK2CY0RQwuVGhlIHRva2VuIHdpdGggZ2l2ZW4gInRva2VuSWQiIGRvZXMgbm90IGV4aXN0LjpxaTcAAHJqEM5AVwIBExGIThBR0EGb9mfOEsBweGjBRVOLUEGSXegxNwAAcchKDARuYW1laRHO0EBXAQATEYhOEFHQQZv2Z84SwHATaMFFQd8wuJpAVwEBeHBo2CYFCCINeErZKFDKABSzq6omJAwfVGhlIGFyZ3VtZW50ICJvd25lciIgaXMgaW52YWxpZDoUEYhOEFHQQZv2Z84SwHATeGjBRVOLUEHfMLiaQFcDA3hwaNgmBQgiDXhK2ShQygAUs6uqJiIMHVRoZSBhcmd1bWVudCAidG8iIGlzIGludmFsaWQuOhMRiE4QUdBBm/ZnzhLAcHlowUVTi1BBkl3oMTcAAHFpEM5yakH4J+yMJAQJQGp4mCYleEppEFHQRWk3AQBKeWjBRVOLUEHmPxiERQ95ajQPEXl4NAp6eXhqNEUIQFcCA3p4Nc/9//9FQZv2Z84UEYhOEFHQUBLAcHh5i9socXoQtyYQEGlowUVTi1BB5j8YhEBpaMFFU4tQQS9Yxe1AVwEEehF5eBTADAhUcmFuc2ZlckGVAW9heXBo2CYFCSIKeTcCAHBo2KomIHt6EXgUwB8MDm9uTkVQMTFQYXltZW50eUFifVtSRUAIQFcABEBWAUAqM5uy"));
public static Neo.SmartContract.NefFile Nef => Neo.IO.Helper.AsSerializable<Neo.SmartContract.NefFile>(Convert.FromBase64String(@"TkVGM1Rlc3RpbmdFbmdpbmUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPA7znO4OTpJcbCoGp54UQN2G/OrAtkZX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