站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Eclipse 专题Eclipse 的字符串分区共享优化机制

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

摘要: 在 Java/C# 这样基于引用语义处理字符串的语言中,作为不可变对象存在的字符串,如果内容相同,则可以通过某种机制实现重用。
  在 Java/C# 这样基于引用语义处理字符串的语言中,作为不可变对象存在的字符串,如果内容相同,则可以通过某种机制实现重用。因为对这类语言来说,指向内存中两块内存位置不同内容相同的字符串,与同时指向一个字符串并没有任何区别。特别是对大量使用字符串的 XML 文件解析类似场合,这样的优化能够很大程度上降低程序的内存占用,如 SAX 解析引擎标准中就专门定义了一个 http://xml.org/sax/features/string-interning 特性用于字符串重用。

  在语言层面,Java/C# 中都直接提供了 String.Intern 的支持。而对 Java 来说,实现上的非常类似。由 String.intern 方法,将当前字符串以内容为键,对象引用为值,放入一个全局性的哈希表中。

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td>//
// java/lang/String.java
//

public final class String
{
 //...
 public native String intern(); // 使用 JNI 函数实现以保障效率
}

//
// hotspot/src/share/vm/prims/jvm.cpp
//

JVM_ENTRY(jstring, JVM_InternString(JNIEnv *env, jstring str))
JVMWrapper("JVM_InternString");
if (str == NULL) return NULL;
 oop string = JNIHandles::resolve_non_null(str); // 将引用解析为内部句柄
 oop result = StringTable::intern(string, CHECK_0); // 进行实际的字符串 intern 操作
 return (jstring) JNIHandles::make_local(env, result); // 获取内部句柄的引用
 JVM_END
 //
 // hotspot/src/share/vm/memory/symbolTable.cpp
 //
 oop StringTable::intern(oop string, TRAPS)
 {
  if (string == NULL) return NULL;
  ResourceMark rm(THREAD); // 保护线程资源区域
  int length;
  Handle h_string (THREAD, string);
  jchar* chars = java_lang_String::as_unicode_string(string, length); // 获取实际字符串内容
  oop result = intern(h_string, chars, length, CHECK_0); // 完成字符串 intern 操作
  return result;
 }
 oop StringTable::intern(Handle string_or_null, jchar* name, int len, TRAPS)
 {
  int hashValue = hash_string(name, len); // 首先根据字符串内容计算哈希值
  stringTableBucket* bucket = bucketFor(hashValue); // 根据哈希值获取目标容器
  oop string = bucket->lookup(name, len); // 然后检测字符串是否已经存在
  // Found
  if (string != NULL) return string;
  // Otherwise, add to symbol to table
  return basic_add(string_or_null, name, len, hashValue, CHECK_0); // 将字符串放入哈希表
 }</td></tr></table>
  对全局字符串表中的字符串,是没有办法显式手动清除的。只能在不使用此字符串后,由垃圾回收线程在进行不可达对象标记时进行分析,并最终调用 StringTable::unlink 方法去遍历清除。

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td>//
// hotspot/src/share/vm/memory/genMarkSweep.cpp
//

void GenMarkSweep::mark_sweep_phase1(...)
{
 //...
 StringTable::unlink();
}

//
// hotspot/src/share/vm/memory/symbolTable.cpp
//

void StringTable::unlink() {
 // Readers of the string table are unlocked, so we should only be
 // removing entries at a safepoint.
 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint")
 for (stringTableBucket* bucket = firstBucket(); bucket <= lastBucket(); bucket++) {
  for (stringTableEntry** p = bucket->entry_addr(); *p != NULL;) {
   stringTableEntry* entry = *p;
   assert(entry->literal_string() != NULL, "just checking");
   if (entry->literal_string()->is_gc_marked()) { // 字符串对象是否可达
    // Is this one of calls those necessary only for verification? (DLD)
    entry->oops_do(&MarkSweep::follow_root_closure);
    p = entry->next_addr();
   } else { // 如不可达则将其内存块回收到内存池中
    *p = entry->next();
    entry->set_next(free_list);
    free_list = entry;
   }
  }
 }
}</td></tr></table>
  通过上面的代码,我们可以直观了解到,对 JVM (Sun JDK 1.4.2) 来说,String.intern 提供的是全局性的基于哈希表的共享支持。这样的实现虽然简单,并能够在最大限度上进行字符串共享;但同时也存在共享粒度太大,优化效果无法度量,大量字符串可能导致全局字符串表性能降低等问题。

  为此 Eclipse 舍弃了 JVM 一级的字符串共享优化机制,而通过提供细粒度、完全可控、可测量的字符串分区共享优化机制,一定程度上缓解此问题。Eclipse 核心的 IStringPoolParticipant 接口由使用者显式实现,在其 shareStrings 方法中提交需要共享的字符串。

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td>//
// org.eclipse.core.runtime.IStringPoolParticipant
//

public interface IStringPoolParticipant {
 /**
 * Instructs this participant to share its strings in the provided
 * pool.
 */
 public void shareStrings(StringPool pool);
}</td></tr></table>
  例如 MarkerInfo 类型实现了 IStringPoolParticipant 接口,在其 shareStrings 方法中,提交自己需要共享的字符串 type,并通知其下级节点进行相应的提交。

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td>//
// org.eclipse.core.internal.resources.MarkerInfo
//

public class MarkerInfo implements ..., IStringPoolParticipant
{
 public void shareStrings(StringPool set) {
  type = set.add(type);
  Map map = attributes;
  if (map instanceof IStringPoolParticipant)
  ((IStringPoolParticipant) map).shareStrings(set);
 }
}</td></tr></table>
  这样一来,只要一个对象树各级节点选择性实现 IStringPoolParticipant 接口,就可以一次性将所有需要共享的字符串,通过递归提交到一个字符串缓冲池中进行复用优化。如 Workspace 就是这样一个字符串共享根入口,其 open 方法在完成工作区打开操作后,将需要进行字符串共享优化的缓存管理对象,加入到全局字符串缓冲区分区优化列表中。

  代码:

<table borderColor=#cccccc width="90%" align=center bgColor=#e7e9e9 border=1> <tr> <td>//
// 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;
 }
}</td></tr></table>

↑返回目录
前一篇: Netbeans大战Eclipse 谁将走向祭坛?
后一篇: 在Eclipse中构建备忘单