Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-16667

Reduce ClassUtils.forName overhead (in particular for annotation introspection purposes)

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 4.3.14, 5.0.4
    • Fix Version/s: 4.3.15, 5.0.5
    • Component/s: Core
    • Labels:
      None

      Description

      Hi,

      while analyzing the calls to ClassUtils.forName() in my Spring-Boot app I noticed that there might be an opportunity to optimize some more calls of commonly known classes. E.g. when looking for calls to Java classes I saw the following distribution for a total of 13000 calls (roughly 3000 coming from Java classes):

      Class Calls
      java.lang.Cloneable 1168
      java.io.Serializable 790
      java.lang.String 473
      java.lang.Enum 292
      java.util.List 150
      java.lang.Object 82
      java.lang.Long 74
      java.util.Map 52
      java.util.Collection 52
      java.lang.Class 41
      java.util.Optional 39
      java.lang.Integer 32
      ... ...

      As you notice especially Serializable and Cloneable take up a big portion here, which are missing from the common class cache. Also Enum and the Collection framework interfaces contribute to the calls.

      An isolated JMH benchmark for Serializable shows the following results:

      Benchmark Mode Cnt Score Error Units
      MyBenchmark.testNew thrpt 10 102632276,261 ± 37687656,076 ops/s
      MyBenchmark.testNew: gc.alloc.rate thrpt 10 0,001 ± 0,001 MB/sec
      MyBenchmark.testNew: gc.alloc.rate.norm thrpt 10 ? 10??   B/op
      MyBenchmark.testOld thrpt 10 1072135,584 ± 38640,911 ops/s
      MyBenchmark.testOld:·gc.alloc.rate thrpt 10 138,990 ± 5,010 MB/sec
      MyBenchmark.testOld:·gc.alloc.rate.norm thrpt 10 136,001 ± 0,001 B/op

      I understand that drawing the line is a difficult thing to do - in fact I had the same problem - but I'd argue we could add the Collection interfaces, Enum and Optional and the interfaces contained in the javaLanguageInterfaces field to the common class cache. That seems like a reasonable collection of commonly known classes.

      I'd be happy to hear your opinion on the attached PR.

      Cheers,
      Christoph

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              christoph.dreis Christoph Dreis
              Last updater:
              Spring Issues Spring Issues
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Days since last comment:
                3 years, 30 weeks, 3 days ago