Eclipse的字符串分区共享优化机制
http://tech.ddvip.com 2006年11月20日 社区交流
本文详细介绍Eclipse的字符串分区共享优化机制
StringPoolJob 类型是分区任务的代码所在,其底层实现是通过 Eclipse 的任务调度机制。关于 Eclipse 的任务调度,有兴趣的朋友可以参考 Michael Valenta (IBM) 的 On the Job: The Eclipse Jobs API 一文。
这里需要了解的是 Job 在 Eclipse 里,被作为一个异步后台任务进行调度,在时间或资源就绪的情况下,通过调用其 Job.run 方法执行。可以说 Job 非常类似一个线程,只不过是基于条件进行调度,可通过后台线程池进行优化罢了。而这里任务被调度的条件,一方面是任务自身的调度时间因素,另一方面是通过 ISchedulingRule 接口提供的任务资源依赖关系。如果一个任务与当前正在运行的任务传统,则将被挂起直到冲突被缓解。而 ISchedulingRule 接口本身可以通过 composite 模式进行组合,描述复杂的任务依赖关系。
在具体完成任务的 StringPoolJob.run 方法中,将对所有字符串缓冲分区的调度条件进行合并,以便在条件允许的情况下,调用 StringPoolJob.shareStrings 方法完成实际工作。
代码:
//
// org.eclipse.core.internal.runtime.StringPoolJob
//
public class StringPoolJob extends Job
{
private static final long RESCHEDULE_DELAY = 300000;//five minutes
protected IStatus run(IProgressMonitor monitor)
{
//copy current participants to handle concurrent additions and removals to map
Map.Entry[] entries = (Map.Entry[]) participants.entrySet().toArray(new Map.Entry[0]);
ISchedulingRule[] rules = new ISchedulingRule[entries.length];
IStringPoolParticipant[] toRun = new IStringPoolParticipant[entries.length];
for (int i = 0; i < toRun.length; i++) {
toRun[i] = (IStringPoolParticipant) entries[i].getKey();
rules[i] = (ISchedulingRule) entries[i].getValue();
}
// 将所有字符串缓冲分区的调度条件进行合并
final ISchedulingRule rule = MultiRule.combine(rules);
// 在调度条件允许的情况下调用 shareStrings 方法执行优化
try {
Platform.getJobManager().beginRule(rule, monitor); // 阻塞直至调度条件允许
shareStrings(toRun, monitor);
} finally {
Platform.getJobManager().endRule(rule);
}
// 重新调度任务自己,以便进行下一次优化
long scheduleDelay = Math.max(RESCHEDULE_DELAY, lastDuration*100);
schedule(scheduleDelay);
return Status.OK_STATUS;
}
}StringPoolJob.shareStrings 方法只是简单的遍历所有分区,调用其根节点的 IStringPoolParticipant.shareStrings 方法,进行前面所述的优化工作,并最终返回分区的优化效果。而缓冲池本身,只是作为一个优化工具,完成后直接被放弃。
代码:
private int shareStrings(IStringPoolParticipant[] toRun, IProgressMonitor monitor) {
final StringPool pool = new StringPool();
for (int i = 0; i < toRun.length; i++) {
if (monitor.isCanceled()) // 操作是否被取消
break;
final IStringPoolParticipant current = toRun[i];
Platform.run(new ISafeRunnable() { // 安全执行
public void handleException(Throwable exception) {
//exceptions are already logged, so nothing to do
}
public void run() {
current.shareStrings(pool); // 进行字符串重用优化
}
});
}
return pool.getSavedStringCount(); // 返回优化效果
}
}通过上面的分析我们可以看到,Eclipse 实现的基于字符串缓冲分区的优化机制,相对于 JVM 的 String.intern() 来说:
1.控制的粒度更细,可以指定要对哪些对象进行优化;
2.优化效果可度量,可以大概估算出优化能节省的空间;
3.不存在性能瓶颈,不存在集中的字符串缓冲池,因此不会因为大量字符串导致性能波动;
4.不会长期占内存,缓冲池只在优化执行时存在,完成后中间结果被抛弃;
5.优化策略可选择,通过定义调度条件,可选择性执行不同的优化策略
来源:JAVAEYE 责编:豆豆技术应用