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)

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


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 38479 on Developer Community or GitHub if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

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.