Minimize transient garbage creation identified by allocation profiling

* UURIFactory, canonicalize/**, PathologicalPathDecideRule, ExtractorHTML
    use recycled matchers via TextUtils
* CrawlURI, KeyedProperties, OverlayContext, SheetOverlaysManager
    use ArrayList and indexed access rather than LinkedList and iterator instances
This commit is contained in:
gojomo committed 2010-11-05 21:34:53 +00:00
1 parent acedca1788
commit f4d69b37da
12 files changed
+64 -66

No files matched your search

@@ -372,17 +372,20 @@ public class UURIFactory extends URI {
// Test for the case of more than two slashes after the http(s) scheme.
// Replace with two slashes as mozilla does if found.
// See [ 788219 ] URI Syntax Errors stop page parsing.
Matcher matcher = HTTP_SCHEME_SLASHES.matcher(uri);
// Matcher matcher = HTTP_SCHEME_SLASHES.matcher(uri);
Matcher matcher = TextUtils.getMatcher(HTTP_SCHEME_SLASHES.pattern(), uri);
if (matcher.matches()) {
uri = matcher.group(1) + matcher.group(2);
}
TextUtils.recycleMatcher(matcher);
// now, minimally escape any whitespace
uri = escapeWhitespace(uri);
// For further processing, get uri elements. See the RFC2396REGEX
// comment above for explanation of group indices used in the below.
matcher = RFC2396REGEX.matcher(uri);
// matcher = RFC2396REGEX.matcher(uri);
matcher = TextUtils.getMatcher(RFC2396REGEX.pattern(), uri);
if (!matcher.matches()) {
throw new URIException("Failed parse of " + uri);
}
@@ -392,6 +395,7 @@ public class UURIFactory extends URI {
String uriPath = checkUriElement(matcher.group(6));
String uriQuery = checkUriElement(matcher.group(8));
// UNUSED String uriFragment = checkUriElement(matcher.group(10));
TextUtils.recycleMatcher(matcher); matcher = null;
// Test if relative URI. If so, need a base to resolve against.
if (uriScheme == null || uriScheme.length() <= 0) {
@@ -701,7 +705,8 @@ public class UURIFactory extends URI {
*/
private String checkPort(String uriAuthority)
throws URIException {
Matcher m = PORTREGEX.matcher(uriAuthority);
// Matcher m = PORTREGEX.matcher(uriAuthority);
Matcher m = TextUtils.getMatcher(PORTREGEX.pattern(), uriAuthority);
if (m.matches()) {
String no = m.group(2);
if (no != null && no.length() > 0) {
@@ -726,6 +731,7 @@ public class UURIFactory extends URI {
}
}
}
TextUtils.recycleMatcher(m);
return uriAuthority;
}
@@ -21,7 +21,6 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -56,19 +55,21 @@ public class KeyedProperties extends ConcurrentHashMap<String,Object> {
* @return discovered override, or local value
*/
public Object get(String key) {
if(!threadOverrides.get().isEmpty()) {
for(OverlayContext ocontext: threadOverrides.get()) {
for(String name: ocontext.getOverlayNames()) {
Map<String,Object> m = ocontext.getOverlayMap(name);
for(String ok : getOverrideKeys(key)) {
Object val = m.get(ok);
if(val!=null) {
return val;
}
ArrayList<OverlayContext> overlays = threadOverrides.get();
for(int i = overlays.size()-1; i>=0; i--) {
OverlayContext ocontext = overlays.get(i);
for(int j = ocontext.getOverlayNames().size()-1; j>=0; j--) {
String name = ocontext.getOverlayNames().get(j);
Map<String,Object> m = ocontext.getOverlayMap(name);
for(String ok : getOverrideKeys(key)) {
Object val = m.get(ok);
if(val!=null) {
return val;
}
}
}
}
return super.get(key);
}
@@ -95,10 +96,10 @@ public class KeyedProperties extends ConcurrentHashMap<String,Object> {
/**
* ThreadLocal (contextual) collection of pushed override maps
*/
static ThreadLocal<LinkedList<OverlayContext>> threadOverrides =
new ThreadLocal<LinkedList<OverlayContext>>() {
protected LinkedList<OverlayContext> initialValue() {
return new LinkedList<OverlayContext>();
static ThreadLocal<ArrayList<OverlayContext>> threadOverrides =
new ThreadLocal<ArrayList<OverlayContext>>() {
protected ArrayList<OverlayContext> initialValue() {
return new ArrayList<OverlayContext>();
}
};
/**
@@ -106,7 +107,7 @@ public class KeyedProperties extends ConcurrentHashMap<String,Object> {
* @param m Map to add
*/
static public void pushOverrideContext(OverlayContext ocontext) {
threadOverrides.get().addFirst(ocontext);
threadOverrides.get().add(ocontext);
}
/**
@@ -115,7 +116,7 @@ public class KeyedProperties extends ConcurrentHashMap<String,Object> {
*/
static public OverlayContext popOverridesContext() {
// TODO maybe check that pop is as expected
return threadOverrides.get().removeFirst();
return threadOverrides.get().remove(threadOverrides.get().size()-1);
}
static public void clearAllOverrideContexts() {
@@ -19,7 +19,7 @@
package org.archive.spring;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Map;
/**
@@ -32,7 +32,7 @@ public interface OverlayContext {
* (even if in fact no overlays were added) */
public boolean haveOverlayNamesBeenSet();
/** return a list of the names of overlay maps to consider */
LinkedList<String> getOverlayNames();
ArrayList<String> getOverlayNames();
/** get the map corresponding to the overlay name */
Map<String,Object> getOverlayMap(String name);
}