Browse Source

Reduce memory consumption by using lazy initialization.

Speed things up a little by using unsynchronized collections.
PR: 21296


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@274758 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 22 years ago
parent
commit
348ff7bdea
3 changed files with 111 additions and 44 deletions
  1. +69
    -18
      src/main/org/apache/tools/ant/RuntimeConfigurable.java
  2. +29
    -21
      src/main/org/apache/tools/ant/Target.java
  3. +13
    -5
      src/main/org/apache/tools/ant/UnknownElement.java

+ 69
- 18
src/main/org/apache/tools/ant/RuntimeConfigurable.java View File

@@ -54,11 +54,16 @@


package org.apache.tools.ant; package org.apache.tools.ant;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Locale;
import java.util.Vector;
import java.util.HashMap;
import java.util.Hashtable; import java.util.Hashtable;
import java.io.Serializable;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;


import org.xml.sax.AttributeList; import org.xml.sax.AttributeList;
import org.xml.sax.helpers.AttributeListImpl; import org.xml.sax.helpers.AttributeListImpl;
@@ -76,7 +81,7 @@ public class RuntimeConfigurable implements Serializable {
private String elementTag = null; private String elementTag = null;
/** List of child element wrappers. */ /** List of child element wrappers. */
private Vector children = new Vector();
private List/*<RuntimeConfigurable>*/ children = null;
/** The element to configure. It is only used during /** The element to configure. It is only used during
* maybeConfigure. * maybeConfigure.
@@ -94,14 +99,15 @@ public class RuntimeConfigurable implements Serializable {
* exact order. The following code is copied from AttributeImpl. * exact order. The following code is copied from AttributeImpl.
* We could also just use SAX2 Attributes and convert to SAX1 ( DOM * We could also just use SAX2 Attributes and convert to SAX1 ( DOM
* attribute Nodes can also be stored in SAX2 Attributges ) * attribute Nodes can also be stored in SAX2 Attributges )
* XXX under JDK 1.4 you can just use a LinkedHashMap for this purpose -jglick
*/ */
private Vector attributeNames = new Vector();
private List/*<String>*/ attributeNames = null;
/** Map of attribute names to values */ /** Map of attribute names to values */
private Hashtable attributeMap = new Hashtable();
private Map/*<String,String>*/ attributeMap = null;


/** Text appearing within the element. */ /** Text appearing within the element. */
private StringBuffer characters = new StringBuffer();
private StringBuffer characters = null;
/** Indicates if the wrapped object has been configured */ /** Indicates if the wrapped object has been configured */
private boolean proxyConfigured = false; private boolean proxyConfigured = false;
@@ -164,7 +170,11 @@ public class RuntimeConfigurable implements Serializable {
* @param value the attribute's value. * @param value the attribute's value.
*/ */
public void setAttribute(String name, String value) { public void setAttribute(String name, String value) {
attributeNames.addElement(name);
if (attributeNames == null) {
attributeNames = new ArrayList();
attributeMap = new HashMap();
}
attributeNames.add(name);
attributeMap.put(name, value); attributeMap.put(name, value);
} }


@@ -173,7 +183,12 @@ public class RuntimeConfigurable implements Serializable {
* @return Attribute name to attribute value map * @return Attribute name to attribute value map
*/ */
public Hashtable getAttributeMap() { public Hashtable getAttributeMap() {
return attributeMap;
// Nobody calls this method, maybe it could just be deleted?
if (attributeMap != null) {
return new Hashtable(attributeMap);
} else {
return new Hashtable(1);
}
} }


/** /**
@@ -194,7 +209,10 @@ public class RuntimeConfigurable implements Serializable {
* Must not be <code>null</code>. * Must not be <code>null</code>.
*/ */
public void addChild(RuntimeConfigurable child) { public void addChild(RuntimeConfigurable child) {
children.addElement(child);
if (children == null) {
children = new ArrayList();
}
children.add(child);
} }


/** /**
@@ -206,7 +224,7 @@ public class RuntimeConfigurable implements Serializable {
* list. * list.
*/ */
RuntimeConfigurable getChild(int index) { RuntimeConfigurable getChild(int index) {
return (RuntimeConfigurable) children.elementAt(index);
return (RuntimeConfigurable) children.get(index);
} }


/** /**
@@ -215,7 +233,21 @@ public class RuntimeConfigurable implements Serializable {
* @since Ant 1.5.1 * @since Ant 1.5.1
*/ */
Enumeration getChildren() { Enumeration getChildren() {
return children.elements();
if (children != null) {
return Collections.enumeration(children);
} else {
return new EmptyEnumeration();
}
}
static final class EmptyEnumeration implements Enumeration {
public EmptyEnumeration() {}
public boolean hasMoreElements() {
return false;
}
public Object nextElement() throws NoSuchElementException {
throw new NoSuchElementException();
}
} }


/** /**
@@ -225,7 +257,14 @@ public class RuntimeConfigurable implements Serializable {
* Should not be <code>null</code>. * Should not be <code>null</code>.
*/ */
public void addText(String data) { public void addText(String data) {
characters.append(data);
if (data.length() == 0) {
return;
}
if (characters != null) {
characters.append(data);
} else {
characters = new StringBuffer(data);
}
} }


/** /**
@@ -238,7 +277,13 @@ public class RuntimeConfigurable implements Serializable {
* *
*/ */
public void addText(char[] buf, int start, int count) { public void addText(char[] buf, int start, int count) {
addText(new String(buf, start, count));
if (count == 0) {
return;
}
if (characters == null) {
characters = new StringBuffer(count);
}
characters.append(buf, start, count);
} }


/** Get the text content of this element. Various text chunks are /** Get the text content of this element. Various text chunks are
@@ -248,7 +293,11 @@ public class RuntimeConfigurable implements Serializable {
* @return the text content of this element. * @return the text content of this element.
*/ */
public StringBuffer getText() { public StringBuffer getText() {
return characters;
if (characters != null) {
return characters;
} else {
return new StringBuffer(0);
}
} }


/** /**
@@ -317,8 +366,9 @@ public class RuntimeConfigurable implements Serializable {
IntrospectionHelper ih = IntrospectionHelper ih =
IntrospectionHelper.getHelper(p, target.getClass()); IntrospectionHelper.getHelper(p, target.getClass());


if (attributeNames != null) {
for (int i = 0; i < attributeNames.size(); i++) { for (int i = 0; i < attributeNames.size(); i++) {
String name = (String) attributeNames.elementAt(i);
String name = (String) attributeNames.get(i);
String value = (String) attributeMap.get(name); String value = (String) attributeMap.get(name);


// reflect these into the target // reflect these into the target
@@ -334,12 +384,13 @@ public class RuntimeConfigurable implements Serializable {
} }
} }
id = (String) attributeMap.get("id"); id = (String) attributeMap.get("id");
}


if (characters.length() != 0) {
if (characters != null) {
ProjectHelper.addText(p, wrappedObject, characters.substring(0)); ProjectHelper.addText(p, wrappedObject, characters.substring(0));
} }


Enumeration enum = children.elements();
Enumeration enum = getChildren();
while (enum.hasMoreElements()) { while (enum.hasMoreElements()) {
RuntimeConfigurable child RuntimeConfigurable child
= (RuntimeConfigurable) enum.nextElement(); = (RuntimeConfigurable) enum.nextElement();


+ 29
- 21
src/main/org/apache/tools/ant/Target.java View File

@@ -1,7 +1,7 @@
/* /*
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2000-2002 The Apache Software Foundation. All rights
* Copyright (c) 2000-2003 The Apache Software Foundation. All rights
* reserved. * reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@@ -54,9 +54,12 @@


package org.apache.tools.ant; package org.apache.tools.ant;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration; import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer; import java.util.StringTokenizer;
import java.util.Vector;


/** /**
* Class to implement a target object with required parameters. * Class to implement a target object with required parameters.
@@ -72,9 +75,9 @@ public class Target implements TaskContainer {
/** The "unless" condition to test on execution. */ /** The "unless" condition to test on execution. */
private String unlessCondition = ""; private String unlessCondition = "";
/** List of targets this target is dependent on. */ /** List of targets this target is dependent on. */
private Vector dependencies = new Vector(2);
private List/*<String>*/ dependencies = null;
/** Children of this target (tasks and data types). */ /** Children of this target (tasks and data types). */
private Vector children = new Vector(5);
private List/*<Task|RuntimeConfigurable>*/ children = new ArrayList(5);
/** Project this target belongs to. */ /** Project this target belongs to. */
private Project project; private Project project;
/** Description of this target, if any. */ /** Description of this target, if any. */
@@ -166,7 +169,7 @@ public class Target implements TaskContainer {
* @param task The task to be added. Must not be <code>null</code>. * @param task The task to be added. Must not be <code>null</code>.
*/ */
public void addTask(Task task) { public void addTask(Task task) {
children.addElement(task);
children.add(task);
} }


/** /**
@@ -176,7 +179,7 @@ public class Target implements TaskContainer {
* Must not be <code>null</code>. * Must not be <code>null</code>.
*/ */
public void addDataType(RuntimeConfigurable r) { public void addDataType(RuntimeConfigurable r) {
children.addElement(r);
children.add(r);
} }


/** /**
@@ -185,18 +188,16 @@ public class Target implements TaskContainer {
* @return an array of the tasks currently within this target * @return an array of the tasks currently within this target
*/ */
public Task[] getTasks() { public Task[] getTasks() {
Vector tasks = new Vector(children.size());
Enumeration enum = children.elements();
while (enum.hasMoreElements()) {
Object o = enum.nextElement();
List tasks = new ArrayList(children.size());
Iterator it = children.iterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof Task) { if (o instanceof Task) {
tasks.addElement(o);
tasks.add(o);
} }
} }
Task[] retval = new Task[tasks.size()];
tasks.copyInto(retval);
return retval;
return (Task[])tasks.toArray(new Task[tasks.size()]);
} }


/** /**
@@ -206,7 +207,10 @@ public class Target implements TaskContainer {
* Must not be <code>null</code>. * Must not be <code>null</code>.
*/ */
public void addDependency(String dependency) { public void addDependency(String dependency) {
dependencies.addElement(dependency);
if (dependencies == null) {
dependencies = new ArrayList(2);
}
dependencies.add(dependency);
} }


/** /**
@@ -215,7 +219,11 @@ public class Target implements TaskContainer {
* @return an enumeration of the dependencies of this target * @return an enumeration of the dependencies of this target
*/ */
public Enumeration getDependencies() { public Enumeration getDependencies() {
return dependencies.elements();
if (dependencies != null) {
return Collections.enumeration(dependencies);
} else {
return new RuntimeConfigurable.EmptyEnumeration();
}
} }


/** /**
@@ -301,9 +309,9 @@ public class Target implements TaskContainer {
*/ */
public void execute() throws BuildException { public void execute() throws BuildException {
if (testIfCondition() && testUnlessCondition()) { if (testIfCondition() && testUnlessCondition()) {
Enumeration enum = children.elements();
while (enum.hasMoreElements()) {
Object o = enum.nextElement();
Iterator it = children.iterator();
while (it.hasNext()) {
Object o = it.next();
if (o instanceof Task) { if (o instanceof Task) {
Task task = (Task) o; Task task = (Task) o;
task.perform(); task.perform();
@@ -352,7 +360,7 @@ public class Target implements TaskContainer {
void replaceChild(Task el, RuntimeConfigurable o) { void replaceChild(Task el, RuntimeConfigurable o) {
int index; int index;
while ((index = children.indexOf(el)) >= 0) { while ((index = children.indexOf(el)) >= 0) {
children.setElementAt(o, index);
children.set(index, o);
} }
} }


@@ -367,7 +375,7 @@ public class Target implements TaskContainer {
void replaceChild(Task el, Task o) { void replaceChild(Task el, Task o) {
int index; int index;
while ((index = children.indexOf(el)) >= 0) { while ((index = children.indexOf(el)) >= 0) {
children.setElementAt(o, index);
children.set(index, o);
} }
} }




+ 13
- 5
src/main/org/apache/tools/ant/UnknownElement.java View File

@@ -54,8 +54,10 @@


package org.apache.tools.ant; package org.apache.tools.ant;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Vector;
import java.io.IOException; import java.io.IOException;


/** /**
@@ -87,7 +89,7 @@ public class UnknownElement extends Task {
/** /**
* List of child elements (UnknownElements). * List of child elements (UnknownElements).
*/ */
private Vector children = new Vector();
private List/*<UnknownElement>*/ children = null;


/** /**
* Creates an UnknownElement for the given element name. * Creates an UnknownElement for the given element name.
@@ -281,7 +283,10 @@ public class UnknownElement extends Task {
* @param child The child element to add. Must not be <code>null</code>. * @param child The child element to add. Must not be <code>null</code>.
*/ */
public void addChild(UnknownElement child) { public void addChild(UnknownElement child) {
children.addElement(child);
if (children == null) {
children = new ArrayList();
}
children.add(child);
} }


/** /**
@@ -307,9 +312,11 @@ public class UnknownElement extends Task {
Class parentClass = parent.getClass(); Class parentClass = parent.getClass();
IntrospectionHelper ih = IntrospectionHelper.getHelper(parentClass); IntrospectionHelper ih = IntrospectionHelper.getHelper(parentClass);


for (int i = 0; i < children.size(); i++) {
if (children != null) {
Iterator it = children.iterator();
for (int i = 0; it.hasNext(); i++) {
RuntimeConfigurable childWrapper = parentWrapper.getChild(i); RuntimeConfigurable childWrapper = parentWrapper.getChild(i);
UnknownElement child = (UnknownElement) children.elementAt(i);
UnknownElement child = (UnknownElement) it.next();
// backwards compatibility - element names of nested // backwards compatibility - element names of nested
// elements have been all lower-case in Ant, except for // elements have been all lower-case in Ant, except for
@@ -327,6 +334,7 @@ public class UnknownElement extends Task {
} }
} }
} }
}
} }


/** /**


Loading…
Cancel
Save