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. Inlined Code Question

Inlined Code Question

Scheduled Pinned Locked Moved C#
questioncsharpdatabaseregex
7 Posts 4 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.
  • R Offline
    R Offline
    Richard Blythe
    wrote on last edited by
    #1

    Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?

    /// <summary>
    /// Returns a value indicating if the path contains the specified node type.
    /// </summary>
    /// <param name="nodeType">Specifies the node type.</param>
    /// <returns></returns>
    public bool Contains(BindingPathNodeType nodeType)
    {
    return IndexOf(nodeType) != -1;
    }

    /// <summary>
    /// Attempts to locate the first (zero-based) index position of the specified node type.
    /// </summary>
    /// <param name="nodeType">Specifies the node type.</param>
    /// <returns></returns>
    public int IndexOf(BindingPathNodeType nodeType)
    {
    int intCount = parsedPathNodes.Length;
    for (int i = 0; i < intCount; i++)
    if (parsedPathNodes[i].NodeType == nodeType)
    return i;
    return -1;
    }

    The mind is like a parachute. It doesn’t work unless it’s open.

    N P L 3 Replies Last reply
    0
    • R Richard Blythe

      Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?

      /// <summary>
      /// Returns a value indicating if the path contains the specified node type.
      /// </summary>
      /// <param name="nodeType">Specifies the node type.</param>
      /// <returns></returns>
      public bool Contains(BindingPathNodeType nodeType)
      {
      return IndexOf(nodeType) != -1;
      }

      /// <summary>
      /// Attempts to locate the first (zero-based) index position of the specified node type.
      /// </summary>
      /// <param name="nodeType">Specifies the node type.</param>
      /// <returns></returns>
      public int IndexOf(BindingPathNodeType nodeType)
      {
      int intCount = parsedPathNodes.Length;
      for (int i = 0; i < intCount; i++)
      if (parsedPathNodes[i].NodeType == nodeType)
      return i;
      return -1;
      }

      The mind is like a parachute. It doesn’t work unless it’s open.

      N Offline
      N Offline
      Not Active
      wrote on last edited by
      #2

      http://www.ademiller.com/blogs/tech/2008/08/c-inline-methods-and-optimization/[^] "How to determine what get’s inlined? The short answer is that you can’t."


      I know the language. I've read a book. - _Madmatt

      1 Reply Last reply
      0
      • R Richard Blythe

        Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?

        /// <summary>
        /// Returns a value indicating if the path contains the specified node type.
        /// </summary>
        /// <param name="nodeType">Specifies the node type.</param>
        /// <returns></returns>
        public bool Contains(BindingPathNodeType nodeType)
        {
        return IndexOf(nodeType) != -1;
        }

        /// <summary>
        /// Attempts to locate the first (zero-based) index position of the specified node type.
        /// </summary>
        /// <param name="nodeType">Specifies the node type.</param>
        /// <returns></returns>
        public int IndexOf(BindingPathNodeType nodeType)
        {
        int intCount = parsedPathNodes.Length;
        for (int i = 0; i < intCount; i++)
        if (parsedPathNodes[i].NodeType == nodeType)
        return i;
        return -1;
        }

        The mind is like a parachute. It doesn’t work unless it’s open.

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        # DEFINE Contains(x) (IndexOf(x) != -1) :-D

        R 1 Reply Last reply
        0
        • P PIEBALDconsult

          # DEFINE Contains(x) (IndexOf(x) != -1) :-D

          R Offline
          R Offline
          Richard Blythe
          wrote on last edited by
          #4

          What is this? Some kind of shorthand?

          The mind is like a parachute. It doesn’t work unless it’s open.

          P 1 Reply Last reply
          0
          • R Richard Blythe

            Below is sample code from one of my recent apps. I used this pattern a lot when designing searchable objects. I know that by appearances, there will be more than two method calls involved. However, I've read that the C# compiler inlines short blocks of code. Will the "Contained" method be inlined at compile time?

            /// <summary>
            /// Returns a value indicating if the path contains the specified node type.
            /// </summary>
            /// <param name="nodeType">Specifies the node type.</param>
            /// <returns></returns>
            public bool Contains(BindingPathNodeType nodeType)
            {
            return IndexOf(nodeType) != -1;
            }

            /// <summary>
            /// Attempts to locate the first (zero-based) index position of the specified node type.
            /// </summary>
            /// <param name="nodeType">Specifies the node type.</param>
            /// <returns></returns>
            public int IndexOf(BindingPathNodeType nodeType)
            {
            int intCount = parsedPathNodes.Length;
            for (int i = 0; i < intCount; i++)
            if (parsedPathNodes[i].NodeType == nodeType)
            return i;
            return -1;
            }

            The mind is like a parachute. It doesn’t work unless it’s open.

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

            If BindingPathNodeType is a reference type then there seems to be nothing that prevents inlining. But, as has been said, you still don't know for sure whether it will be inlined. What has not been said though is that it also depends on whether you NGEN, and whether it's 64bit, and 64bit NGEN is completely different from anything else, IIRC not even created by the same team (instead of being developed by the CLR team, it was developed by the Visual C++ team) 'Contains' is not a leaf method, while I have read nothing about non-leaf methods not being inlined, in other compilers inlining is often done on leaf methods first.

            R 1 Reply Last reply
            0
            • L Lost User

              If BindingPathNodeType is a reference type then there seems to be nothing that prevents inlining. But, as has been said, you still don't know for sure whether it will be inlined. What has not been said though is that it also depends on whether you NGEN, and whether it's 64bit, and 64bit NGEN is completely different from anything else, IIRC not even created by the same team (instead of being developed by the CLR team, it was developed by the Visual C++ team) 'Contains' is not a leaf method, while I have read nothing about non-leaf methods not being inlined, in other compilers inlining is often done on leaf methods first.

              R Offline
              R Offline
              Richard Blythe
              wrote on last edited by
              #6

              Thanks for the heads up. I don't NGEN any of my code so I'm probably in trouble. :) I hate having to enter the same code block twice because: 1. It looks dirtier. 2. More lines to (possibly) debug.

              The mind is like a parachute. It doesn’t work unless it’s open.

              1 Reply Last reply
              0
              • R Richard Blythe

                What is this? Some kind of shorthand?

                The mind is like a parachute. It doesn’t work unless it’s open.

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Yeah, kinda. :~

                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