zoukankan      html  css  js  c++  java
  • C# 接口持有结构体会导致装箱问题

    问题起源:void DistpatchEvent(EventType, IEventData)

    这里的 IEventData是一个接口,代表事件参数。

    为了避免每次发送事件产生GC,决定事件参数使用struct,但struct不能继承,所以这里只能使用接口作为基类。

    但将结构体传给接口时会发生装箱操作,产生GC。

    public class testStructGC : MonoBehaviour
    {
    
        // Start is called before the first frame update
        void Start()
        {
            var ot = new PlayerMoveCmd();
            ICmd ic = new PlayerMoveCmd();
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    }

    对应的IL代码,可以看到

    ICmd ic = new PlayerMoveCmd(); 
    这一行产生了装箱操作
        .method private hidebysig 
            instance void Start () cil managed 
        {
            // Method begins at RVA 0x20b0
            // Code size 25 (0x19)
            .maxstack 1
            .locals init (
                [0] valuetype PlayerMoveCmd ot,
                [1] class ICmd ic,
                [2] valuetype PlayerMoveCmd
            )
    
            // {
            IL_0000: nop
            // PlayerMoveCmd playerMoveCmd = default(PlayerMoveCmd);
            IL_0001: ldloca.s 0
            IL_0003: initobj PlayerMoveCmd
            // ICmd cmd = default(PlayerMoveCmd);
            IL_0009: ldloca.s 2
            IL_000b: initobj PlayerMoveCmd
            IL_0011: ldloc.2
            IL_0012: box PlayerMoveCmd
            IL_0017: stloc.1
            // }
            IL_0018: ret
        } // end of method testStructGC::Start


  • 相关阅读:
    JDK+Jmeter 环境搭建
    APP自动化中三大定位工具
    APP自动化环境配置
    pytest生成allure报告
    pytest怎么标记用例?
    pytest中怎么实现参数化?
    pytest中怎么引用前置中的变量
    pytest_前置后置
    toast文本提示信息元素获取
    js处理日历
  • 原文地址:https://www.cnblogs.com/timeObjserver/p/13637188.html
Copyright © 2011-2022 走看看