Eclipse的字符串分区共享优化机制
http://tech.ddvip.com 2006年11月20日 社区交流
本文详细介绍Eclipse的字符串分区共享优化机制
代码:
//
// org.eclipse.core.runtime.StringPool
//
public final class StringPool {
private int savings;
private final HashMap map = new HashMap();
public StringPool() {
super();
}
public String add(String string) {
if (string == null)
return string;
Object result = map.get(string);
if (result != null) {
if (result != string)
savings += 44 + 2 * string.length();
return (String) result;
}
map.put(string, string);
return string;
}
// 获取优化能节省多少空间的大致估算值
public int getSavedStringCount() {
return savings;
}
}不过这里的估算值在某些情况下可能并不准确,例如缓冲池中包括字符串 S1,此时提交一个与之内容相同但物理位置不同的字符串 S2,则如果 S2 被提交多次,会导致错误的高估优化效果。当然如果需要得到精确值,也可以对其进行重构,通过一个 Set 跟踪每个字符串优化的过程,获得精确优化度量,但需要损失一定效率。
在了解了需优化字符串的提交流程,以及字符串提交后的优化流程后,我们接着看看 Eclipse 核心是如何将这两者整合到一起的。
前面提到 Workspace.open 方法会调用 InternalPlatform.addStringPoolParticipant 方法,将一个字符串缓冲池分区的根节点,添加到全局性的优化任务队列中。
代码:
//
// org.eclipse.core.internal.runtime.InternalPlatform
//
public final class InternalPlatform {
private StringPoolJob stringPoolJob;
public void addStringPoolParticipant(IStringPoolParticipant participant, ISchedulingRule rule) {
if (stringPoolJob == null)
stringPoolJob = new StringPoolJob(); // Singleton 模式
stringPoolJob.addStringPoolParticipant(participant, rule);
}
}
//
// org.eclipse.core.internal.runtime.StringPoolJob
//
public class StringPoolJob extends Job
{
private static final long INITIAL_DELAY = 10000;//five seconds
private Map participants = Collections.synchronizedMap(new HashMap(10));
public void addStringPoolParticipant(IStringPoolParticipant participant, ISchedulingRule rule) {
participants.put(participant, rule);
if (sleep())
wakeUp(INITIAL_DELAY);
}
public void removeStringPoolParticipant(IStringPoolParticipant participant) {
participants.remove(participant);
}
}此任务将在合适的时候,为每个注册的分区进行共享优化。
来源:JAVAEYE 责编:豆豆技术应用
正在加载评论...