Uploaded image for project: 'ClojureScript'
  1. CLJS-2865

Optimize string expression concatenation

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Completed
    • Affects versions: None
    • Fix versions: None
    • Labels:
      None
    • Approval:
      Accepted
    • Patch:
      Code and Test

      Description

      Now that we have function return type inference ( CLJS-1997 Closed ), we can see that functions like namespace return strings. Additionally, we can easily make it so that expressions involving str are inferred as returning strings. With those two in place, with a mild extension to CLJS-2314 Closed , we can eliminate unnecessary string coercions in str macro expansions where arguments are inferred to be strings.

      This leads to performance gains which are akin to checked-if elimination (but perhaps even greater):

      (defn foo [x y]
        (str (+ x y)))
      
      (simple-benchmark [x :foo/bar] (str (namespace x) "-" (foo 3 2)) 10000000)
      

      Yields these speedups under :advanced

      V8: 3.2
      SpiderMonkey: 1.3
      JavaScriptCore: 4.2
      Nashorn: 2.5
      ChakraCore: 3.2
      GraalVM: 1.3

      This could be great for UIs that are heavy in string-concatenation, as well as for the self-hosted compiler.

      Details:

      Before:
      
      Benchmarking with V8
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 1686 msecs
      Benchmarking with SpiderMonkey
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 1845 msecs
      Benchmarking with JavaScriptCore
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 7973 msecs
      Benchmarking with Nashorn
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 37874 msecs
      Benchmarking with ChakraCore
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 4899 msecs
      Benchmarking with GraalVM
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 955 msecs
      
      After:
      
      Benchmarking with V8
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 518 msecs
      Benchmarking with SpiderMonkey
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 1340 msecs
      Benchmarking with JavaScriptCore
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 1859 msecs
      Benchmarking with Nashorn
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 15276 msecs
      Benchmarking with ChakraCore
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 1496 msecs
      Benchmarking with GraalVM
      [x :foo/bar], (str (namespace x) "-" (foo 3 2)), 10000000 runs, 683 msecs
      

        Attachments

        1. CLJS-2865.patch
          2 kB
        2. CLJS-2865-2.patch
          3 kB
        3. CLJS-2865-3.patch
          3 kB
        4. CLJS-2865-4.patch
          3 kB
        5. CLJS-2865-5.patch
          3 kB

          Activity

            People

            • Assignee:
              dnolen David Nolen
              Reporter:
              mfikes Mike Fikes
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: