Eclipse的字符串分区共享优化机制

http://tech.ddvip.com   2006年11月20日    社区交流

本文详细介绍Eclipse的字符串分区共享优化机制

  代码:

//
// org.eclipse.core.internal.resources
//
public class Workspace ...
{
 protected SaveManager saveManager;
 public IStatus open(IProgressMonitor monitor) throws CoreException
 {
  // 打开工作空间
  // 最终注册一个新的字符串缓冲池分区
  InternalPlatform.getDefault().addStringPoolParticipant(saveManager, getRoot());
  return Status.OK_STATUS;
 }
}
  对需要优化的类型 SaveManager 来说,只需要实现 IStringPoolParticipant 接口,并在被调用的时候提交自己与子元素的需优化字符串即可。其子元素甚至都不需要实现 IStringPoolParticipant 接口,只需将提交行为一级一级传递下去即可,如:

  代码:

  //
// org.eclipse.core.internal.resources.SaveManager
//
public class SaveManager implements ..., IStringPoolParticipant
{
 protected ElementTree lastSnap;
 public void shareStrings(StringPool pool)
 {
  lastSnap.shareStrings(pool);
 }
}
//
// org.eclipse.core.internal.watson.ElementTree
//
public class ElementTree
{
 protected DeltaDataTree tree;
 public void shareStrings(StringPool set) {
  tree.storeStrings(set);
 }
}
//
// org.eclipse.core.internal.dtree.DeltaDataTree
//
public class DeltaDataTree extends AbstractDataTree
{
 private AbstractDataTreeNode rootNode;
 private DeltaDataTree parent;
 public void storeStrings(StringPool set) {
  //copy field to protect against concurrent changes
  AbstractDataTreeNode root = rootNode;
  DeltaDataTree dad = parent;
  if (root != null)
   root.storeStrings(set);
  if (dad != null)
   dad.storeStrings(set);
 }
}
//
// org.eclipse.core.internal.dtree.AbstractDataTreeNode
//
public abstract class AbstractDataTreeNode
{
 protected AbstractDataTreeNode children[];
 protected String name;
 public void storeStrings(StringPool set) {
  name = set.add(name);
  //copy children pointer in case of concurrent modification
  AbstractDataTreeNode[] nodes = children;
  if (nodes != null)
   for (int i = nodes.length; --i >= 0;)
    nodes[i].storeStrings(set);
 }
}

  所有的需优化字符串,都会通过 StringPool.add 方法提交到统一的字符串缓冲池中。而这个缓冲池的左右,与 JVM 级的字符串表略有不同,它只是在进行字符串缓冲分区优化时,起到一个阶段性的整理作用,本身并不作为字符串引用的入口存在。因此在实现上它只是简单的对 HashMap 进行包装,并粗略计算优化能带来的额外空间,以提供优化效果的度量标准。

来源:JAVAEYE    责编:豆豆技术应用

正在加载评论...