Bug 38479 - Performance improvements for string properties
Summary: Performance improvements for string properties
Status: CONFIRMED
Alias: None
Product: Android
Classification: Xamarin
Component: General (show other bugs)
Version: unspecified
Hardware: All All
: Normal enhancement
Target Milestone: ---
Assignee: Jonathan Pryor
URL:
Depends on:
Blocks:
 
Reported: 2016-02-05 05:28 UTC by Vyacheslav Volkov
Modified: 2017-06-29 04:31 UTC (History)
3 users (show)

See Also:
Tags:
Is this bug a regression?: ---
Last known good build:


Attachments
Performance test (33.40 KB, application/x-zip-compressed)
2016-02-05 05:28 UTC, Vyacheslav Volkov
Details

Description Vyacheslav Volkov 2016-02-05 05:28:02 UTC
Created attachment 14908 [details]
Performance test

I noticed that default Xamarin implementation creates and release new Java.Lang.String on every text change, can be replaced with direct method call:
    public string Text
    {
      get
      {
        if (this.TextFormatted == null)
          return (string) null;
        return this.TextFormatted.ToString();
      }
      set
      {
        Java.Lang.String @string = value != null ? new Java.Lang.String(value) : (Java.Lang.String) null;
        this.TextFormatted = (ICharSequence) @string;
        if (@string == null)
          return;
        @string.Dispose();
      }
    }

You can reduce the amount of memory allocation for strings by replacing it at a direct method call:
      set
      {
	if (TextView.id_setText_Ljava_lang_CharSequence_ == IntPtr.Zero)
          TextView.id_setText_Ljava_lang_CharSequence_ = JNIEnv.GetMethodID(TextView.class_ref, "setText", "(Ljava/lang/CharSequence;)V");
        IntPtr jobject = JNIEnv.NewString(value); //Instead of create String wrapper using direct handle
        try
        {
          JValue* parms = stackalloc JValue[1];
          *parms = new JValue(jobject);
          JNIEnv.CallVoidMethod(this.Handle, TextView.id_setText_Ljava_lang_CharSequence_, parms);
        }
        finally
        {
          JNIEnv.DeleteLocalRef(jobject);
        }
      }
I think you can improve not only setter but also the getter but it will be a little harder because you need to call ToString method on ICharSequence handle.

I tested it on my device performance has grown by 5 times.
I added project with performance test.
Comment 1 Brad Chase 2016-10-13 12:48:06 UTC
Has anyone looked into this yet?  I would love to see some more performance data on this considering the discrepancies between android the the other platforms in performance.

Note You need to log in before you can comment on or make changes to this bug.