1

String compile time constants (internalized-strings) and literals can be compared with the ==, as they are assigned the same reference at compile-time if they are equal somehow.

Does this mean that compiling code consisting of n String literals takes n log(n) time to compile?

I ask this question here because someone might already know the answer and I'm not certain I can write a test that measures the effect in a reliable, reproducable or significant way. Or that this test would reflect real world constrains etc..

I'm going to publish any test cases I can come up with though, feel free to suggest some, I will implement them as soon as I can find the time.

HopefullyHelpful
  • 1,652
  • 3
  • 21
  • 37
  • 1
    I remember that the last time I tried to put about a hundred kB of text into a String, it didn't end too well. I believe that the compilation time is also dependent on the String length, but not sure about the trend on that – Sameer Puri Aug 08 '16 at 18:41
  • 1
    Why would compilation time be the difference here? The comparison still takes place at runtime. – Dave Newton Aug 08 '16 at 18:47
  • It's just a good guess nothing more but I think string literals like `"this"` will just take up space and consume the disk read and write time, but string constants like `final String s = "Something";` will consume some compile time too and if too much with much use cases, there will be impact on compile time. – Masked Man Aug 08 '16 at 18:48
  • 1
    @DaveNewton String comparison using == depends on strings being interned, which I *think* would happen at compile time for string literals. – hyde Aug 08 '16 at 18:48
  • 1
    The interning happens at compile time, I'm asking why it would be `n log(n)` compile time if `n` is the number of internable strings. In any case, any additional time would be insignificant for reasonably-sized strings. – Dave Newton Aug 08 '16 at 18:50
  • This would be interesting to test on 7 vs 8... – Sameer Puri Aug 08 '16 at 18:53
  • 1
    @hyde, https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5 – Lew Bloch Aug 08 '16 at 19:02
  • 3
    Where does the notion of `n log(n)` come from? Looking up interned strings is `O(1)`, so the time to intern string literals is `O(n)` where `n` is the number of *distinct* strings to intern. The compiler doesn't intern the strings, it simply embeds the strings into the class image. Interning happens at class initialization. The JLS specifies that it is done by `String.intern`. https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.10.5 – Lew Bloch Aug 08 '16 at 19:09
  • @SameerPuri Interesting why? Do you have some evidence that the 'problem' exists at all, let alone in one or other of those versions? – user207421 Oct 14 '16 at 23:16

1 Answers1

1

Do String constants/literals in code slow down compiling considerably?

No.

String compile time constants (internalized-strings) and literals can be compared with the ==, as they are assigned the same reference at compile-time if they are equal somehow.

No they aren't. They are pooled at compile time into the constants area of the .class file, and they are interned at class-loading time, when the reference is assigned.

Does this mean that compiling code consisting of n String literals takes n log(n) time to compile?

No. There is no inherently O(N log(N)) process here. Pooling is O(1) via a hash table in any sensible implementation, and interning ditto.

[It would of course be possible to construct a compiler or intern() method that was O(N log(N)), or O(N^3) or worse, but it isn't entailed by anything in your question.]

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @HopefullyHelpful Every string *is* stored in the hash table at compilation time. Or are you asking about runtime? – user207421 Oct 14 '16 at 23:06
  • 1
    So you *are* asking about runtime? It helps to answer questions you're asked. You need to consider space costs well as time costs. The runtime string pool wasn't garbage-collected until somewhere around Java 1.3, so it would have been an immense waste of space until then, hence the `intern()` design. Half the objects in most executing Java programs are char arrays and Strings that contain them – user207421 Oct 14 '16 at 23:12
  • Yes, in any sensible implementation, as I already stated in my answer. – user207421 Oct 14 '16 at 23:30