Substring 在BCL和CLR里面搞了啥( 二 )


这点可以通过汇编验证:
System_Private_CoreLib!System.String.InternalSubString+0xc:00007ffd`9a86132c 418bc8movecx,r8d0:000> tSystem_Private_CoreLib!System.String.InternalSubString+0xf:00007ffd`9a86132f ff15b39f7e00callqword ptr [System_Private_CoreLib+0x9cb2e8 (00007ffd`9b04b2e8)] ds:00007ffd`9b04b2e8={coreclr!AllocateStringFastMP_InlineGetThread (00007ffd`9b20b3a0)}0:000> tcoreclr!AllocateStringFastMP_InlineGetThread:00007ffd`9b20b3a0 4c8b0d090d3400movr9,qword ptr [coreclr!g_pStringClass (00007ffd`9b54c0b0)] ds:00007ffd`9b54c0b0=00007ffd3b6ed698callqword ptr [System_Private_CoreLib+0x9cb2e8 (00007ffd`9b04b2e8)] ds:00007ffd`9b04b2e8={coreclr!AllocateStringFastMP_InlineGetThread (00007ffd`9b20b3a0)}就是直接调用了AllocateStringFastMP_InlineGetThread,然后跳转到后者的地址AllocateStringFastMP_InlineGetThread这个函数的作用实际上是申请内存,比如你 new 一个对象的时候,又或者本例,你需要一个新的字符串对象来存储截取的字符串 。

Substring 在BCL和CLR里面搞了啥

文章插图
作者:江湖评谈版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可 。
Substring 在BCL和CLR里面搞了啥

文章插图

经验总结扩展阅读