Live data from Hacker News

JEP254: proposal to represent Java Strings as ISO-8859-1

openjdk.java.net

31–40 of 55 posts

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#31

Most of software developers don't care about encodings all day. This will give them enough rope to hang half of the world. Memory is dirt cheap these days anyway, why now? And if you have mounds of text, compress it.

If I can fit nearly twice as many strings in my L1 / L2 caches, that might help sometimes.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#32
post #6

This sounds like a ridiculous idea. Java treats text as all modern systems should: as a sequence of the open-ended Universal Character Set (UCS). This proposal doesn't challenge that claim, nor could it. Java strings are sequences of Unicode code points, logically at least. This proposal is just about how to encode such a sequence internally. There are numerous alternatives for encoding sequences of Unicode code poin…

...but, if I've understood the proposal correctly, the public interface isn't changing. The user of String will never know, nor care, whether their string is implemented as UTF-8 or ISO-8859-1. This is purely an implementation detail.

Personally, I'd be astonished if JVMs didn't already do this: way back when when I was working for a company that did embedded JVMs, it was one of the first optimisations we did. When class files were loaded, we'd look at each string, and if they were ISO-8859-1 we'd use a byte-based representation rather than a word-based one. (Programmatically created strings were always words, of course.)

Even in an environment where most of our customers were Asian the savings were huge. Java uses a lot of identifier strings.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#33
post #26

Most of software developers don't care about encodings all day. This will give them enough rope to hang half of the world. Memory is dirt cheap these days anyway, why now? And if you have mounds of text, compress it.

Umm.. This will be completely transparent to developers, strings will fall back to Javas weirdo UCS2/UTF16 when necessary automatically.

This might actually be slower (you have a lot of strings which have UCS2 characters down the line, have to reallocate array and copy everything over)

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#34
post #17

Earlier quoted context omitted.

>With UTF-8 you would give up some constant time operations like character lookups if Java had a non-broken string API, even with the current UCS-2 based solution they couldn't do constant-time character lookups. And even if Java went the extra mile of using characters big enough to encode every code point of a string, constant lookup would still not be possible because users are often interested graphemes, not chara…

That is sadly true, but at least for the Basic Multilingual Plane, charAt() and codePointAt() return correct results in constant time. The only non-broken methods are afaik codePointCount() and offsetByCodePoint(), which, as you pointed out, are not that interesting anyway. Do you know of an example of a well-designed String API? I'd be interested in what trade-offs other language designers made (newer ones, like Rus…

IMHO Python gets strings very right. The internal representation is variable, depending on the string's content (to save memory), and the external API only gives you access to code points.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#35

pointless. Its the number of objects not the size of primitive fields (e.g. the char array) which hurts GC and consumes memory. This proposal will save <10% on an average short string instance but probably waste performance.

I assume this is motivated by heap analysis done by Oracle on their could/SAAS/... applications. They may have quite a few large strings in old gen. To give you an example, for every application deployed Tomcat builds an retains a 200kb String. Other candidates are SQL queries, manifests or in-heap caches.

But I agree with you on the performance side. My impression is that a lot of Java applications are simple data pumps. They read data from a database and send it to a client. It's hard to see how this JEP helps in that case:

- read bytes from the network (probably UTF-8)

- convert bytes to Java Strings

- compress Java String (ASCII, Latin-1, UTF-8) new

- "render" String to Writer (HTML, XML, JSON, ...)

- decompress Java String for Writer new

- encode to again UTF-8 for OutputStream/browser

In this case this would increase allocation rates and increase CPU load for a potentially smaller old gen.

The only real way to optimize this would be to redesign the String class to be encoding aware and update the Writer classes accordingly. This is unlikely to happen and would hurt other use cases.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#36
post #8
post #7

Earlier quoted context omitted.

Firefox did this recently, with great success. It's a simple solution that will reduce memory usage (and this, garbage collection). It makes perfect sense.

HTML is ISO 8859-1 by default, which privileges that legacy encoding within conformant browsers and web servers. That's the reason for all the "character entity" stuff. The same is not true of Java.

HTML is a locale specific encoding by default. (No, really.) Most locales default to windows-1252, not ISO-8859-1.

That said, all conforming HTML documents must now use UTF-8.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#37
post #34

Earlier quoted context omitted.

That is sadly true, but at least for the Basic Multilingual Plane, charAt() and codePointAt() return correct results in constant time. The only non-broken methods are afaik codePointCount() and offsetByCodePoint(), which, as you pointed out, are not that interesting anyway. Do you know of an example of a well-designed String API? I'd be interested in what trade-offs other language designers made (newer ones, like Rus…

IMHO Python gets strings very right. The internal representation is variable, depending on the string's content (to save memory), and the external API only gives you access to code points.

That's only Python 3.3 and above; before that it was an under-defined mess, in CPython depending on compile-time options and often broken with the same UCS-2/UTF-16 mess as numerous other languages.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#38
post #6

This sounds like a ridiculous idea. Java treats text as all modern systems should: as a sequence of the open-ended Universal Character Set (UCS). This proposal doesn't challenge that claim, nor could it. Java strings are sequences of Unicode code points, logically at least. This proposal is just about how to encode such a sequence internally. There are numerous alternatives for encoding sequences of Unicode code poin…

Why is this even being discussed? It's only an implementation optimization, no change to the language interface. I don't recall such a fuss around compressed pointers. This is so much better than the number of software stacks that introduce ByteString types specifically for this benefit. If String just did this when it could, then it could just be knowingly used in place of each roll your own.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#39
post #9
post #6

This sounds like a ridiculous idea. Java treats text as all modern systems should: as a sequence of the open-ended Universal Character Set (UCS). This proposal doesn't challenge that claim, nor could it. Java strings are sequences of Unicode code points, logically at least. This proposal is just about how to encode such a sequence internally. There are numerous alternatives for encoding sequences of Unicode code poin…

> Java treats text as all modern systems should: as a sequence of the open-ended Universal Character Set (UCS) unfortunately, Java treats text as data encoded in UCS-2 which uses a maximum of two bytes to represent a character and all the Java string APIs assume the UCS-2 encoding. The problem with UCS-2 is that it doesn't allow to encode characters outside of the Unicode BMP. In order to solve this, Java actually us…

> The only well-known contemporary languages that get this (somewhat) right are...

and Ceylon.

Re: JEP254: proposal to represent Java Strings as ISO-8859-1

#40
post #26

Earlier quoted context omitted.

Umm.. This will be completely transparent to developers, strings will fall back to Javas weirdo UCS2/UTF16 when necessary automatically.

This might actually be slower (you have a lot of strings which have UCS2 characters down the line, have to reallocate array and copy everything over)

Aren't Java Strings immutable? Once built, they won't be modified.
Post reply on HN