serialising Arraylists

R

Roedy Green

I wondered if anyone has experimented with serialising ArrayLists.
Are they substantially more compact if you convert them to arrays
first?

Are there dummy elements past the last used? Is there substantially
more type information embedded?

I will likely to some experiments myself to find out. I just thought
I would start the conversation about it.
 
R

Roedy Green

I wondered if anyone has experimented with serialising ArrayLists.
Are they substantially more compact if you convert them to arrays
first?

Are there dummy elements past the last used? Is there substantially
more type information embedded?

I will likely to some experiments myself to find out. I just thought
I would start the conversation about it.

The good news is there is almost no difference. An ArrayList 1000 long
containing 100 objects is only 4 bytes longer than an array containing
100 specific objects.

I wondered if serialized interned strings are guaranteed to
reconstitute as interned. It is one thing to do an experiment and
discover they are, but quite another know the behaviour is prescribed.

Ditto, do unique strings coming back necessarily have map onto
different copies.
 
T

Tim Tyler

: I wondered if serialized interned strings are guaranteed to
: reconstitute as interned. It is one thing to do an experiment and
: discover they are, but quite another know the behaviour is prescribed.

: Ditto, do unique strings coming back necessarily have map onto
: different copies.

I have no idea - but I wouldn't write code that depends on it.

Relying on String interning is usually a sign of taking performance
optimisation beyond the point where others are able to make sense
of your code.
 
R

Roedy Green

Relying on String interning is usually a sign of taking performance
optimisation beyond the point where others are able to make sense
of your code.

People use String interning unconsciously and get away with it
comparing Strings that come from String literals. All of a sudden
this code breaks when they get them from a different source. This is
why I decided to make my MultiProperties intern by default, since it
reap laces hard coded static final literals.

All code that takes advantage of string interning clearly has to be
documented, but it is not rocket science. The only thing you have to
document is where you relied on == rather than equals and which
strings have to be interned.

It has two advantages, space saving when you have many objects
pointing to the same string, and faster comparisons of long strings.
You can do a quick equality test.
 
T

Tim Tyler

[String interning]

: It has two advantages, space saving when you have many objects
: pointing to the same string, and faster comparisons of long strings.
: You can do a quick equality test.

Saving space is an advantage of the compiler performing interning.

It is not an advantage to using "==" rather than ".equals(" for
string comparisons.

As you note, this latter practice has some associated dangers.

Unless you are doing performance optimisations, I reckon there's a good
case to be made for using a lint tool to warn you whenever "==" is used
with a String.

That will catch most of the problems before they happen.
 
C

Chris Uppal

Tim said:
Unless you are doing performance optimisations, I reckon there's a
good case to be made for using a lint tool to warn you whenever "=="
is used with a String.

Maybe not for when comparing two String valued variables, but certainly for any
code like:

if (someString == "Hello")
{
...

The chance of that being correct and viable code must be about 0.0

This string interning thing is a real mess-up IMO. It'd have been better if
the language design had required the compiler/runtime to produce a *different*
String object for each string literal (or String valued compile-time
expression).

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top