Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Did the compiler inline my method?

Did the compiler inline my method?

Scheduled Pinned Locked Moved C#
question
10 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    HosamAly
    wrote on last edited by
    #1

    Hello, Is there a (relatively simple) way to know whether the compiler has inlined my method? (Actually I want to make sure that it did...)

    L 1 Reply Last reply
    0
    • H HosamAly

      Hello, Is there a (relatively simple) way to know whether the compiler has inlined my method? (Actually I want to make sure that it did...)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      No, and you will have no guarantee that a function will be inlined. Actually, it's not job of the C# compiler, but rather the JIT that does the inlining at runtime, if it decides to do so. But you have no control over it and can not rely on a function being inlined. regards

      P H 2 Replies Last reply
      0
      • L Lost User

        No, and you will have no guarantee that a function will be inlined. Actually, it's not job of the C# compiler, but rather the JIT that does the inlining at runtime, if it decides to do so. But you have no control over it and can not rely on a function being inlined. regards

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Greeeg wrote:

        But you have no control over it and can not rely on a function being inlined.

        Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles | MoXAML PowerToys

        L 1 Reply Last reply
        0
        • L Lost User

          No, and you will have no guarantee that a function will be inlined. Actually, it's not job of the C# compiler, but rather the JIT that does the inlining at runtime, if it decides to do so. But you have no control over it and can not rely on a function being inlined. regards

          H Offline
          H Offline
          HosamAly
          wrote on last edited by
          #4

          Well, thanks a lot. But since, I'm very concerned about performance, I'd appreciate more of your expertise. There are some methods that I'm writing just for modularity reasons. Each is just a single line of code. For example:

          class X {
          public int Value { get { return getExternalValue(); } }
          private int getExternalValue() {
          // call another method in just one line
          }
          }

          I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true? Or should I inline the method myself? Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better

          L G 2 Replies Last reply
          0
          • P Pete OHanlon

            Greeeg wrote:

            But you have no control over it and can not rely on a function being inlined.

            Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.

            Deja View - the feeling that you've seen this post before.

            My blog | My articles | MoXAML PowerToys

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Pete O'Hanlon wrote:

            Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.

            The first thing I'd do with this new job would be to introduce some kind of forceinline directive :-D

            P 1 Reply Last reply
            0
            • H HosamAly

              Well, thanks a lot. But since, I'm very concerned about performance, I'd appreciate more of your expertise. There are some methods that I'm writing just for modularity reasons. Each is just a single line of code. For example:

              class X {
              public int Value { get { return getExternalValue(); } }
              private int getExternalValue() {
              // call another method in just one line
              }
              }

              I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true? Or should I inline the method myself? Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              hosamaly wrote:

              I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true?

              Nobody (apart from the Microsoft CLR team) knows exactly under which circumstances the JIT optimizes code. While the JIT probably is intelligent enough to see that the function can be inlined, it still might decide not to do so based on other parameters. So in your case, if you're really concerned about performance you probably should inline it yourself - it'll always be a compromise between modularity/readability and performance. This article[^] and this blog entry[^] talk a bit about the JIT method inlining, you may want to take a look at them. regards

              1 Reply Last reply
              0
              • H HosamAly

                Well, thanks a lot. But since, I'm very concerned about performance, I'd appreciate more of your expertise. There are some methods that I'm writing just for modularity reasons. Each is just a single line of code. For example:

                class X {
                public int Value { get { return getExternalValue(); } }
                private int getExternalValue() {
                // call another method in just one line
                }
                }

                I am thinking that the JIT should be intelligent enough to actually inline both methods. But is this assumption true? Or should I inline the method myself? Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better

                G Offline
                G Offline
                Guffa
                wrote on last edited by
                #7

                You are micro-optimising. In any normal situation it will not make any real difference if the method is inlined or not. If you really need that litte extra performance, you should probably not only inline this method, but the method that you are calling also...

                Despite everything, the person most likely to be fooling you next is yourself.

                H 1 Reply Last reply
                0
                • G Guffa

                  You are micro-optimising. In any normal situation it will not make any real difference if the method is inlined or not. If you really need that litte extra performance, you should probably not only inline this method, but the method that you are calling also...

                  Despite everything, the person most likely to be fooling you next is yourself.

                  H Offline
                  H Offline
                  HosamAly
                  wrote on last edited by
                  #8

                  You're right. I'm just trying to write micro-optimized code that is maintainable to some level. I'm introducing a wrapper layer for some external functions, but performance is at premium, so I'm trying to optimize it as much as possible, without exposing the external interfaces to other layers.

                  Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better

                  G 1 Reply Last reply
                  0
                  • L Lost User

                    Pete O'Hanlon wrote:

                    Well, there is one sure fire way, but that involves getting hired by Microsoft in a position on the CLR team and writing code to do this.

                    The first thing I'd do with this new job would be to introduce some kind of forceinline directive :-D

                    P Offline
                    P Offline
                    Paul Conrad
                    wrote on last edited by
                    #9

                    Greeeg wrote:

                    introduce some kind of forceinline directive

                    That would be nice.

                    "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

                    1 Reply Last reply
                    0
                    • H HosamAly

                      You're right. I'm just trying to write micro-optimized code that is maintainable to some level. I'm introducing a wrapper layer for some external functions, but performance is at premium, so I'm trying to optimize it as much as possible, without exposing the external interfaces to other layers.

                      Many thanks to all helpful guys here in CP! I really appreciate your help. :rose: Know me better

                      G Offline
                      G Offline
                      Guffa
                      wrote on last edited by
                      #10

                      If you really need to optimise the code so much that you need the methods to be inlined, you are way beyond maintainable code. If you are still thinking of maintainable code, you should not bother with the tiny performance difference that inlining methods means.

                      Despite everything, the person most likely to be fooling you next is yourself.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups