[Mono-dev] [PATCH] Optimize Encoding.GetByteCount

Atsushi Eno atsushi at ximian.com
Wed Oct 25 17:06:28 EDT 2006


Ben Maurer wrote:
> Hey guys,
> 
> On the 2.0 profile, Encoding uses the char*/byte* version of encoding
> methods to avoid allocating memory. One code path missed this
> optimization, I've attached a fix.

nice :-) Comment for patch below.

> - It might pay to do something on the 1.0 profile as well.
> - Paolo, can you comment on how this kind of change works with the moving gc?

To my understanding, fixed pointers do not participate GC target. And
- locally-allocated array anyways lives until its conversion finishes
   (and probably immediately disposed depending on the JIT optimization)
- Usually this conversion do not take long time
So I guess fixed pointer would work better than possibly being moved
from nursery area.


> +		if (s == null)
>  			throw new ArgumentNullException ("s");
> +#if NET_2_0
> +		unsafe {
> +			fixed (char* cptr = s) {
> +				return GetByteCount (cptr, s.Length);
> +			}
>  		}
> +#else
> +		char[] chars = s.ToCharArray ();
> +		return GetByteCount (chars, 0, chars.Length);
> +#endif

Before fixing the pointer you have to make sure that s is non-zero length.

Atsushi Eno



More information about the Mono-devel-list mailing list