Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9637

Improve the performance of GString

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Fixed
    • None
    • 4.0.0-alpha-1
    • None
    • None

    Description

      GString will runĀ toString() whenever its literal string is needed, e.g. methods like equals, hashCode are called, but unfortunately its literal string will be re-constructed for each time and the process costs quite a little of time.

      So I propose to check whether GString values are all of immutable type, e.g. primitive types and their boxed type, String, etc. If yes, use the cached string literal, otherwise re-construct the literal string.

      Here is the test script, Groovy 3.0.5 costs about 8700ms, and PR1329 costs about 290ms on my machine. About 96.7% time is reduced.

      def gstr = makeGString()
      long b = System.currentTimeMillis()
      for (int i = 0; i < 10000000; i++) {
      	gstr.toString()
      }
      long e = System.currentTimeMillis()
      println "${e - b}ms"
      
      //@groovy.transform.CompileStatic
      def makeGString() {
          def now = java.time.LocalDateTime.now()
          "integer: ${1}, double: ${1.2d}, string: ${'x'}, class: ${Map.class}, boolean: ${true}, now: ${now}"
      }
      

      Attachments

        Activity

          People

            paulk Paul King
            daniel_sun Daniel Sun
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - Not Specified
                Not Specified
                Remaining:
                Remaining Estimate - 0h
                0h
                Logged:
                Time Spent - 9h 10m
                9h 10m