{"action":"created","issue":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/21377","repository_url":"https://api.github.com/repos/dotnet/coreclr","labels_url":"https://api.github.com/repos/dotnet/coreclr/issues/21377/labels{/name}","comments_url":"https://api.github.com/repos/dotnet/coreclr/issues/21377/comments","events_url":"https://api.github.com/repos/dotnet/coreclr/issues/21377/events","html_url":"https://github.com/dotnet/coreclr/issues/21377","id":387727469,"node_id":"MDU6SXNzdWUzODc3Mjc0Njk=","number":21377,"title":"Allow CLR to get/invoke direct pointer to a virtual method (interface method)","user":{"login":"dmitriyse","id":10322215,"node_id":"MDQ6VXNlcjEwMzIyMjE1","avatar_url":"https://avatars3.githubusercontent.com/u/10322215?v=4","gravatar_id":"","url":"https://api.github.com/users/dmitriyse","html_url":"https://github.com/dmitriyse","followers_url":"https://api.github.com/users/dmitriyse/followers","following_url":"https://api.github.com/users/dmitriyse/following{/other_user}","gists_url":"https://api.github.com/users/dmitriyse/gists{/gist_id}","starred_url":"https://api.github.com/users/dmitriyse/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dmitriyse/subscriptions","organizations_url":"https://api.github.com/users/dmitriyse/orgs","repos_url":"https://api.github.com/users/dmitriyse/repos","events_url":"https://api.github.com/users/dmitriyse/events{/privacy}","received_events_url":"https://api.github.com/users/dmitriyse/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2018-12-05T12:08:07Z","updated_at":"2018-12-05T12:47:18Z","closed_at":null,"author_association":"NONE","body":"Current CLR implementation have two OpCodes that works with virtual methods: \r\nldvirtftn -> \"vtable offset\"\r\n\"vtable offset\" -> callvirt\r\n\r\nIn this feature request new OpCode is introduced: \"ldvirtftnptr\".\r\nThis opcode should resolve direct pointer to the virtual/interface method.\r\nldvirtftnptr -> \"pointer to method\"\r\n\"pointer to method\" -> call (with load \"this\" object as a first argument)\r\n\r\nWith this CLR improvement, compilers will be able to eliminate performance penalty of virtual method call.\r\n\r\nFor example:\r\n```CSharp\r\npublic class A\r\n{\r\n    public virtual int GetValue()\r\n    {\r\n         return 1;\r\n    }\r\n}\r\n\r\npublic class B: A\r\n{\r\n    public override int GetValue()\r\n    {\r\n         return base.GetValue() * 2;\r\n    }\r\n}\r\n\r\n// Client code that is sensitive to a virtual method call performance.\r\nA b = new B();\r\nint sum = 0;\r\n\r\n// resolve direct pointer to machine code of b.GetValue() implementation and store it in the stack\r\n// IL:   ldvirtftnptr  b.GetValue() \r\nfor (int i = 0; i < 100; i++)\r\n{\r\n     // IL:  load \"b\" as the first argument to subsequent call\r\n     // IL:   call (0x28 opcode)  with the direct pointer to machine code of b.GetValue() implementation\r\n     sum += b.GetValue();\r\n}\r\n\r\n```\r\nIt's a kind of devirtualization.\r\nThe same idea is described here http://blog.llvm.org/2017/03/devirtualization-in-llvm-and-clang.html and implemented in LLVM since version 4.0.\r\nIt's referenced here as \"Devirtualization of function calls inside loops\"\r\n\r\nLINQ to objects will receive huge performance improvement from this feature."},"comment":{"url":"https://api.github.com/repos/dotnet/coreclr/issues/comments/444473245","html_url":"https://github.com/dotnet/coreclr/issues/21377#issuecomment-444473245","issue_url":"https://api.github.com/repos/dotnet/coreclr/issues/21377","id":444473245,"node_id":"MDEyOklzc3VlQ29tbWVudDQ0NDQ3MzI0NQ==","user":{"login":"benaadams","id":1142958,"node_id":"MDQ6VXNlcjExNDI5NTg=","avatar_url":"https://avatars1.githubusercontent.com/u/1142958?v=4","gravatar_id":"","url":"https://api.github.com/users/benaadams","html_url":"https://github.com/benaadams","followers_url":"https://api.github.com/users/benaadams/followers","following_url":"https://api.github.com/users/benaadams/following{/other_user}","gists_url":"https://api.github.com/users/benaadams/gists{/gist_id}","starred_url":"https://api.github.com/users/benaadams/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/benaadams/subscriptions","organizations_url":"https://api.github.com/users/benaadams/orgs","repos_url":"https://api.github.com/users/benaadams/repos","events_url":"https://api.github.com/users/benaadams/events{/privacy}","received_events_url":"https://api.github.com/users/benaadams/received_events","type":"User","site_admin":false},"created_at":"2018-12-05T12:47:18Z","updated_at":"2018-12-05T12:47:18Z","author_association":"COLLABORATOR","body":"Ah you mean hoist the resolution of the virtual call outside the loop; then use the resolved call in the loop"}}
{"actor":{"display_login":"benaadams"}}