C# 2.0 Generics and collections

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Giagnocavo [MVP]

    #16
    Re: C# 2.0 Generics and collections

    For ValueTypes, a specialized type class is created. For RefTypes, only one
    is created and it's reused. This solves the boxing issue (preliminary
    results tell me it's at least 5 times faster in a tight loop that woult
    otherwise box and unbox).

    -mike
    MVP

    "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
    news:MPG.1a1b18 2f87021faa989a8 0@msnews.micros oft.com...[color=blue]
    > Keith Patrick <richard_keith_ patrick@hotmail .com> wrote:[color=green]
    > > A generic wouldn't solve the problem of the speed of boxing, would it?[/color][/color]
    I[color=blue][color=green]
    > > haven't read about how they are implemented, but I would think that[/color][/color]
    integers[color=blue][color=green]
    > > would still have to be treated a objects by the generic.[/color]
    >
    > I don't believe so. I believe each generic class is compiled into a
    > type-specific class when it's first used, and that can take advantage
    > of anything type-specific such as size. (Note that this *isn't* the way
    > generics will be working in Java, AFAIK.)
    >
    > --
    > Jon Skeet - <skeet@pobox.co m>
    > http://www.pobox.com/~skeet
    > If replying to the group, please do not mail me too[/color]


    Comment

    • Michael Giagnocavo [MVP]

      #17
      Re: C# 2.0 Generics and collections

      Jon's answer is about Generics in general. The .NET implementation will
      eliminate boxing (support at the IL level). The Java implementation doesn't
      get around this.
      -mike
      MVP

      "Joe Mayo" <jmayo@ddiieess ppaammeerrssddi iee.com> wrote in message
      news:OMBh8bHqDH A.488@tk2msftng p13.phx.gbl...[color=blue]
      > "Keith Patrick" <richard_keith_ patrick@hotmail .com> wrote in message
      > news:ecZhcwGqDH A.1948@TK2MSFTN GP12.phx.gbl...[color=green]
      > > A generic wouldn't solve the problem of the speed of boxing, would it?[/color][/color]
      I[color=blue][color=green]
      > > haven't read about how they are implemented, but I would think that[/color]
      > integers[color=green]
      > > would still have to be treated a objects by the generic.[/color]
      >
      > Hi Keith,
      >
      > In the September MSDN, Jason Clark writes about Generics. In the
      > Performance paragraph he states "This is because boxing of the values is
      > avoided completely.", but you should read the whole paragraph for context:
      >
      > http://msdn.microsoft.com/msdnmag/issues/03/09/NET/
      >
      > There is also a follow-up article in the October edition that talks more
      > about the implementation:
      >
      > http://msdn.microsoft.com/msdnmag/issues/03/10/NET/
      >
      > Joe
      > --
      > http://www.csharp-station.com
      >
      >[/color]


      Comment

      • Gary van der Merwe

        #18
        Re: C# 2.0 Generics and collections

        Hi

        Dose the runtime casting from object -> string not cause performence
        problems?

        Would this improve with the use of generics?

        Gary

        "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
        news:uWbY5zHqDH A.2964@tk2msftn gp13.phx.gbl...[color=blue]
        > StringCollectio n doesn't have any boxing going on, but the other
        > object-based ones would suffer.
        > -mike
        > MVP
        >
        > "Gary van der Merwe" <garyvdm@hotmai l.com> wrote in message
        > news:OS74QoFqDH A.2440@TK2MSFTN GP10.phx.gbl...[color=green]
        > > Hi
        > >
        > > I think the refrence to bugs was regarding the fact that the old[/color]
        > collections[color=green]
        > > are slow because you have to continualy box objects.
        > >
        > > Gary
        > >
        > >
        > > "Christoph Nahr" <christoph.nahr @kynosarges.de> wrote in message
        > > news:ekp1rvsv0e h59m6r8lm3aann0 vhseml2vt@4ax.c om...[color=darkred]
        > > > On Tue, 11 Nov 2003 14:26:57 +0100, "Thomas Tomiczek \(MVP\)"
        > > > <t.tomiczek@tho na-consulting.com> wrote:
        > > >
        > > > >SADLY (so to say) the old non-generic collections are still there.
        > > > >
        > > > >Which just keeps, porapbly, existing bugs around.
        > > >
        > > > Well, I haven't noticed any bugs in StringCollectio n so far, and
        > > > besides they could re-implement it as a pre-created generic collection
        > > > in .NET 2.0, couldn't they?
        > > > --
        > > > http://www.kynosarges.de[/color]
        > >
        > >[/color]
        >
        >
        >[/color]


        Comment

        • Christoph Nahr

          #19
          Re: C# 2.0 Generics and collections

          On Tue, 11 Nov 2003 20:16:28 +0200, "Gary van der Merwe"
          <garyvdm@hotmai l.com> wrote:
          [color=blue]
          >Dose the runtime casting from object -> string not cause performence
          >problems?[/color]

          No. String is a reference type, so there's no boxing/unboxing going
          on, so the performance penalty will be negligible. (At least compared
          to what you'd have to pay for value types!)
          --
          Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

          Comment

          • Michael Giagnocavo [MVP]

            #20
            Re: C# 2.0 Generics and collections

            Right, storing a reftype in an object costs nothing (whereas a boxing costs
            quite a bit). A downcast costs about 4 times less than a box.
            -mike
            MVP

            "Christoph Nahr" <christoph.nahr @kynosarges.de> wrote in message
            news:1pl2rv0blq cir7p48mvf7d0qo bohr1892o@4ax.c om...[color=blue]
            > On Tue, 11 Nov 2003 20:16:28 +0200, "Gary van der Merwe"
            > <garyvdm@hotmai l.com> wrote:
            >[color=green]
            > >Dose the runtime casting from object -> string not cause performence
            > >problems?[/color]
            >
            > No. String is a reference type, so there's no boxing/unboxing going
            > on, so the performance penalty will be negligible. (At least compared
            > to what you'd have to pay for value types!)
            > --
            > http://www.kynosarges.de[/color]


            Comment

            • Eric Gunnerson [MS]

              #21
              Re: C# 2.0 Generics and collections

              A bit of a clarification.

              In the Java implementation, the casting will be removed from the user code,
              but it will still be present at the bytecode level.
              --
              Eric Gunnerson

              Visit the C# product team at http://www.csharp.net
              Eric's blog is at http://blogs.gotdotnet.com/ericgu/

              This posting is provided "AS IS" with no warranties, and confers no rights.
              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
              news:MPG.1a1b26 0360d9b930989a8 2@msnews.micros oft.com...[color=blue]
              > Gary van der Merwe <garyvdm@hotmai l.com> wrote:[color=green]
              > > No. Thats the whole point of them. With Generics, the type checking[/color][/color]
              happens[color=blue][color=green]
              > > when you complile, not at runtime.[/color]
              >
              > That doesn't *necessarily* get rid of boxing though. For instance, in
              > the Java implementation of generics, the casting will be (largely)
              > removed, but boxing won't be.
              >
              > --
              > Jon Skeet - <skeet@pobox.co m>
              > http://www.pobox.com/~skeet
              > If replying to the group, please do not mail me too[/color]


              Comment

              • Eric Gunnerson [MS]

                #22
                Re: C# 2.0 Generics and collections

                Anders did some timings on this for his PDC presentation (which we're
                working to get up on the C# dev center on MSDN).

                IIRC, you save around 10% of the loop overhead in the reference type case
                (by avoiding the cast), and something like 50% on the value type case. This
                was for essentially empty loops.

                --
                Eric Gunnerson

                Visit the C# product team at http://www.csharp.net
                Eric's blog is at http://blogs.gotdotnet.com/ericgu/

                This posting is provided "AS IS" with no warranties, and confers no rights.
                "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
                news:%23$rPGXKq DHA.1488@TK2MSF TNGP12.phx.gbl. ..[color=blue]
                > Right, storing a reftype in an object costs nothing (whereas a boxing[/color]
                costs[color=blue]
                > quite a bit). A downcast costs about 4 times less than a box.
                > -mike
                > MVP
                >
                > "Christoph Nahr" <christoph.nahr @kynosarges.de> wrote in message
                > news:1pl2rv0blq cir7p48mvf7d0qo bohr1892o@4ax.c om...[color=green]
                > > On Tue, 11 Nov 2003 20:16:28 +0200, "Gary van der Merwe"
                > > <garyvdm@hotmai l.com> wrote:
                > >[color=darkred]
                > > >Dose the runtime casting from object -> string not cause performence
                > > >problems?[/color]
                > >
                > > No. String is a reference type, so there's no boxing/unboxing going
                > > on, so the performance penalty will be negligible. (At least compared
                > > to what you'd have to pay for value types!)
                > > --
                > > http://www.kynosarges.de[/color]
                >
                >[/color]


                Comment

                • Eric Gunnerson [MS]

                  #23
                  Re: C# 2.0 Generics and collections

                  We certainly will not get rid of the existing types, as lots of programs
                  depend upon them.

                  We had talked about auto-migration, but it's somewhat unpalatable as we've
                  taken the opportunity to fix some problems with the ArrayList and Hashtable
                  classes, but the fixes might break existing code in some rare cases. In the
                  generics world, you'll use List<T> and Dictionary<K, V> instead.

                  --
                  Eric Gunnerson

                  Visit the C# product team at http://www.csharp.net
                  Eric's blog is at http://blogs.gotdotnet.com/ericgu/

                  This posting is provided "AS IS" with no warranties, and confers no rights.
                  "Gary van der Merwe" <garyvdm@hotmai l.com> wrote in message
                  news:%23cYB0YFq DHA.1496@TK2MSF TNGP11.phx.gbl. ..[color=blue]
                  > Hi
                  >
                  > When C# 2.0 arrives System.Collecti ons.Specialized .StringCollecti on will
                  > become "obsolete". Will the framework still contain this class, or will
                  > there be a C# 1.X to 2.0 conversion utility that will change
                  > System.Collecti ons.Specialized .StringCollecti on to
                  > System.Collecti ons.ArrayList <string> and ArrayList to ArrayList<objec t>
                  > ??
                  >
                  > Gary
                  >
                  >[/color]


                  Comment

                  • Michael Giagnocavo [MVP]

                    #24
                    Re: C# 2.0 Generics and collections

                    I'd love to see his benchmarks. I did mine by adding an item on each
                    iteration (requiring a box in, or for a reftype, just passing the ref) and
                    then taking it back out.

                    The generics version for the reftypes was barely noticable (2**24
                    iterations, something like 0.02 seconds speed up). Of course, this was
                    actually doing some real work. I timed the actual cast down to about 3ns
                    (on a P4c 3GHz), so I'm saving that each time.

                    For valuetypes, it was a whole different story, the memory requirements got
                    so high they slowed it down immensely (2**24 objects, since each int was
                    boxed).

                    So for "real world" apps, I'd imagine we'll see great (runtime performance)
                    benefits for valuetypes and an almost unnoticeable improvement for
                    reftypes...

                    -mike
                    MVP

                    "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> wrote in message
                    news:%23lFMqQLq DHA.2808@TK2MSF TNGP10.phx.gbl. ..[color=blue]
                    > Anders did some timings on this for his PDC presentation (which we're
                    > working to get up on the C# dev center on MSDN).
                    >
                    > IIRC, you save around 10% of the loop overhead in the reference type case
                    > (by avoiding the cast), and something like 50% on the value type case.[/color]
                    This[color=blue]
                    > was for essentially empty loops.
                    >
                    > --
                    > Eric Gunnerson
                    >
                    > Visit the C# product team at http://www.csharp.net
                    > Eric's blog is at http://blogs.gotdotnet.com/ericgu/
                    >
                    > This posting is provided "AS IS" with no warranties, and confers no[/color]
                    rights.[color=blue]
                    > "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
                    > news:%23$rPGXKq DHA.1488@TK2MSF TNGP12.phx.gbl. ..[color=green]
                    > > Right, storing a reftype in an object costs nothing (whereas a boxing[/color]
                    > costs[color=green]
                    > > quite a bit). A downcast costs about 4 times less than a box.
                    > > -mike
                    > > MVP
                    > >
                    > > "Christoph Nahr" <christoph.nahr @kynosarges.de> wrote in message
                    > > news:1pl2rv0blq cir7p48mvf7d0qo bohr1892o@4ax.c om...[color=darkred]
                    > > > On Tue, 11 Nov 2003 20:16:28 +0200, "Gary van der Merwe"
                    > > > <garyvdm@hotmai l.com> wrote:
                    > > >
                    > > > >Dose the runtime casting from object -> string not cause performence
                    > > > >problems?
                    > > >
                    > > > No. String is a reference type, so there's no boxing/unboxing going
                    > > > on, so the performance penalty will be negligible. (At least compared
                    > > > to what you'd have to pay for value types!)
                    > > > --
                    > > > http://www.kynosarges.de[/color]
                    > >
                    > >[/color]
                    >
                    >[/color]


                    Comment

                    • Willy Denoyette [MVP]

                      #25
                      Re: C# 2.0 Generics and collections

                      Michael,

                      Mind to share your code? My observations are on par with Anders timings.

                      Willy.

                      "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message news:uJuy9RNqDH A.1928@TK2MSFTN GP12.phx.gbl...[color=blue]
                      > I'd love to see his benchmarks. I did mine by adding an item on each
                      > iteration (requiring a box in, or for a reftype, just passing the ref) and
                      > then taking it back out.
                      >
                      > The generics version for the reftypes was barely noticable (2**24
                      > iterations, something like 0.02 seconds speed up). Of course, this was
                      > actually doing some real work. I timed the actual cast down to about 3ns
                      > (on a P4c 3GHz), so I'm saving that each time.
                      >
                      > For valuetypes, it was a whole different story, the memory requirements got
                      > so high they slowed it down immensely (2**24 objects, since each int was
                      > boxed).
                      >
                      > So for "real world" apps, I'd imagine we'll see great (runtime performance)
                      > benefits for valuetypes and an almost unnoticeable improvement for
                      > reftypes...
                      >
                      > -mike
                      > MVP
                      >
                      > "Eric Gunnerson [MS]" <ericgu@online. microsoft.com> wrote in message
                      > news:%23lFMqQLq DHA.2808@TK2MSF TNGP10.phx.gbl. ..[color=green]
                      > > Anders did some timings on this for his PDC presentation (which we're
                      > > working to get up on the C# dev center on MSDN).
                      > >
                      > > IIRC, you save around 10% of the loop overhead in the reference type case
                      > > (by avoiding the cast), and something like 50% on the value type case.[/color]
                      > This[color=green]
                      > > was for essentially empty loops.
                      > >
                      > > --
                      > > Eric Gunnerson
                      > >
                      > > Visit the C# product team at http://www.csharp.net
                      > > Eric's blog is at http://blogs.gotdotnet.com/ericgu/
                      > >
                      > > This posting is provided "AS IS" with no warranties, and confers no[/color]
                      > rights.[color=green]
                      > > "Michael Giagnocavo [MVP]" <mggUNSPAM@Atre vido.net> wrote in message
                      > > news:%23$rPGXKq DHA.1488@TK2MSF TNGP12.phx.gbl. ..[color=darkred]
                      > > > Right, storing a reftype in an object costs nothing (whereas a boxing[/color]
                      > > costs[color=darkred]
                      > > > quite a bit). A downcast costs about 4 times less than a box.
                      > > > -mike
                      > > > MVP
                      > > >
                      > > > "Christoph Nahr" <christoph.nahr @kynosarges.de> wrote in message
                      > > > news:1pl2rv0blq cir7p48mvf7d0qo bohr1892o@4ax.c om...
                      > > > > On Tue, 11 Nov 2003 20:16:28 +0200, "Gary van der Merwe"
                      > > > > <garyvdm@hotmai l.com> wrote:
                      > > > >
                      > > > > >Dose the runtime casting from object -> string not cause performence
                      > > > > >problems?
                      > > > >
                      > > > > No. String is a reference type, so there's no boxing/unboxing going
                      > > > > on, so the performance penalty will be negligible. (At least compared
                      > > > > to what you'd have to pay for value types!)
                      > > > > --
                      > > > > http://www.kynosarges.de
                      > > >
                      > > >[/color]
                      > >
                      > >[/color]
                      >
                      >[/color]


                      Comment

                      Working...