classpath
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

debug output problem in ObjectInputStream


From: Per Bothner
Subject: debug output problem in ObjectInputStream
Date: Tue, 02 Apr 2002 13:31:24 -0800
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:0.9.9+) Gecko/20020328

(This problem was reported to me by L. Peter Deutsch.)

The class java/io/ObjectInputStream.java contains a lot
of calls to dumpElement and dumpElementln, which prints
the argument is debugging is enabled. By default the
DEBUG flag is false, so debugging is disabled.  Ideally
a compiler will be able to inline and eliminate the
calls to dumpElement[ln].

The problem is that the argument to dumpElement[ln] is
still evaluated, even when DEBUG is false.  In many cases
these arguments are non-trivial string concatenation expressions,
so we will get a temporary StringBuffer allocated, appended
to and thrown away.  A smart compiler might be able to
figure out that the result of the concatenation is unused,
and so avoid it.  Even worse though is that it calls
toString for each element of an array (and also for exceptions).
Since toString of an unknown class can do anything, the
compiler can't eliminate the call to toString.  It is even
possible that toString for a large (or cyclic) data structure
could do a large(or even an infinite) amount of work.

I've appended one fix, but I'm not sure what the best approach is.
The simplest would be to just rip out all the dumpElement code.
Next simplest is to move all the Configuration.DEBUG && dump
tests out of dumpElement[ln] and to the call sites.  One could
even do what my patches does, and convert the string concatenations
to multiple print calls.  Having dumpElement[ln] as separate
methods has the advantage that it could over overridden, but
it seems like poinles generality.  So if we want to keep the
debugging printout, I suggest:

  if (Configuration.DEBUG && dump)
    for (int i=0, len=Array.getLength(array); i < len; i++)
      System.out.println("  ELEMENT[" + i + "]=" + Array.get(array, i));

This makes it possible for a compiler to do the optimization to
multiple print calls that my patch does.

Alteratively, I suggest just removing all the dumpElement calls.
--
        --Per Bothner
address@hidden   http://www.bothner.com/per/
Index: ObjectInputStream.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/io/ObjectInputStream.java,v
retrieving revision 1.11
diff -u -r1.11 ObjectInputStream.java
--- ObjectInputStream.java      2002/01/22 22:40:14     1.11
+++ ObjectInputStream.java      2002/04/02 20:45:49
@@ -243,8 +243,16 @@
        Object array = Array.newInstance (componentType, length);
        int handle = assignNewHandle (array);
        readArrayElements (array, componentType);
-       for (int i=0, len=Array.getLength(array); i < len; i++)
-         dumpElementln ("  ELEMENT[" + i + "]=" + Array.get(array, 
i).toString());
+       if (Configuration.DEBUG && dump)
+         {
+           for (int i=0, len=Array.getLength(array); i < len; i++)
+             {
+               System.out.print("  ELEMENT[");
+               System.out.print(i);
+               System.out.print("]=");
+               System.out.println(Array.get(array, i));
+             }
+         }
        ret_val = processResolution (array, handle);
        break;
       }

reply via email to

[Prev in Thread] Current Thread [Next in Thread]