org.jruby.Ruby.freezeAndDedupString()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(7.2k)|赞(0)|评价(0)|浏览(199)

本文整理了Java中org.jruby.Ruby.freezeAndDedupString方法的一些代码示例,展示了Ruby.freezeAndDedupString的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Ruby.freezeAndDedupString方法的具体详情如下:
包路径:org.jruby.Ruby
类名称:Ruby
方法名:freezeAndDedupString

Ruby.freezeAndDedupString介绍

[英]Given a Ruby string, cache a frozen, duplicated copy of it, or find an existing copy already prepared. This is used to reduce in-memory duplication of pre-frozen or known-frozen strings. Note that this cache does some sync against the Ruby instance. This could cause contention under heavy concurrent load, so a reexamination of this design might be warranted. Because RubyString.equals does not consider encoding, and MRI's logic for deduplication does need to consider encoding, we use a wrapper object as the key. These wrappers need to be used on all get operations, so if we don't need to insert anything we reuse that wrapper the next time. The logic here reads like this: 1. If the string is not a natural String object, just freeze and return it. 2. Use the wrapper from the thread-local cache or create and set a new one. 3. Use the wrapper to look up the deduplicated string. 4. If there's a dedup in the cache, clear the wrapper for next time and return the dedup. 5. Remove the wrapper from the threadlocal to avoid reusing it, since we'll insert it. 6. Atomically set the new entry or repair the GCed entry that already exists. 7. Return the newly-deduplicated string.
[中]给定一个Ruby字符串,缓存该字符串的冻结副本,或者查找已准备好的现有副本。这用于减少预冻结或已知冻结字符串在内存中的重复。注意,这个缓存对Ruby实例进行了一些同步。这可能会在高并发负载下引起争用,因此有必要重新检查此设计。因为鲁比斯特林。平等不考虑编码,MRI的重复删除逻辑需要考虑编码,我们使用包装对象作为关键。这些包装器需要在所有get操作中使用,所以如果我们不需要插入任何东西,我们下次会重用该包装器。这里的逻辑是这样的:1。如果字符串不是自然字符串对象,只需冻结并返回它即可。2.使用线程本地缓存中的包装器,或者创建并设置一个新的包装器。3.使用包装器查找已消除重复的字符串。4.如果缓存中存在重复数据消除,请在下次清除包装并返回重复数据消除。5.从threadlocal中移除包装,避免重复使用,因为我们将插入它。6.自动设置新条目或修复已存在的GCed条目。7.返回新删除的重复字符串。

代码示例

代码示例来源:origin: org.jruby/jruby-complete

@JRubyMethod(name = "-@") // -'foo' returns frozen string
public final IRubyObject minus_at(ThreadContext context) {
  return isFrozen() ? this : context.runtime.freezeAndDedupString(this);
}

代码示例来源:origin: org.jruby/jruby-core

@JRubyMethod(name = "-@") // -'foo' returns frozen string
public final IRubyObject minus_at(ThreadContext context) {
  return isFrozen() ? this : context.runtime.freezeAndDedupString(this);
}

代码示例来源:origin: org.jruby/jruby-complete

private void initDefinedMessages() {
  for (DefinedMessage definedMessage : DefinedMessage.values()) {
    RubyString str = freezeAndDedupString(
      RubyString.newString(this, ByteList.create(definedMessage.getText())));
    definedMessages.put(definedMessage, str);
  }
}

代码示例来源:origin: org.jruby/jruby-core

public final RubyString getFrozenString(ThreadContext context, int bytelistIndex, int stringIndex, int codeRange) {
  RubyString str = frozenStrings[stringIndex];
  if (str == null) {
    str = frozenStrings[stringIndex] = context.runtime.freezeAndDedupString((RubyString) RubyString.newStringShared(context.runtime, getByteList(bytelistIndex), codeRange).freeze(context));
  }
  return str;
}

代码示例来源:origin: org.jruby/jruby-complete

public final RubyString getFrozenString(ThreadContext context, int bytelistIndex, int stringIndex, int codeRange) {
  RubyString str = frozenStrings[stringIndex];
  if (str == null) {
    str = frozenStrings[stringIndex] = context.runtime.freezeAndDedupString((RubyString) RubyString.newStringShared(context.runtime, getByteList(bytelistIndex), codeRange).freeze(context));
  }
  return str;
}

代码示例来源:origin: org.jruby/jruby-core

private void initDefinedMessages() {
  for (DefinedMessage definedMessage : DefinedMessage.values()) {
    RubyString str = freezeAndDedupString(
      RubyString.newString(this, ByteList.create(definedMessage.getText())));
    definedMessages.put(definedMessage, str);
  }
}

代码示例来源:origin: org.jruby/jruby-complete

@JRubyMethod(meta = true)
public static IRubyObject list(ThreadContext context, IRubyObject recv) {
  Ruby runtime = recv.getRuntime();
  RubyHash names;
  synchronized (recv) {
    names = (RubyHash) recv.getInternalVariables().getInternalVariable("signal_list");
    if (names == null) {
      names = RubyHash.newHash(runtime);
      for (Map.Entry<String, Integer> sig : RubySignal.list().entrySet()) {
        names.op_aset(context, runtime.freezeAndDedupString(runtime.newString(sig.getKey())), runtime.newFixnum(sig.getValue()));
      }
      names.op_aset(context, runtime.freezeAndDedupString(runtime.newString("EXIT")), runtime.newFixnum(0));
      recv.getInternalVariables().setInternalVariable("signal_list", names);
    } else {
      names.dup(context);
    }
  }
  
  return names;
}

代码示例来源:origin: org.jruby/jruby-core

@JRubyMethod(meta = true)
public static IRubyObject list(ThreadContext context, IRubyObject recv) {
  Ruby runtime = recv.getRuntime();
  RubyHash names;
  synchronized (recv) {
    names = (RubyHash) recv.getInternalVariables().getInternalVariable("signal_list");
    if (names == null) {
      names = RubyHash.newHash(runtime);
      for (Map.Entry<String, Integer> sig : RubySignal.list().entrySet()) {
        names.op_aset(context, runtime.freezeAndDedupString(runtime.newString(sig.getKey())), runtime.newFixnum(sig.getValue()));
      }
      names.op_aset(context, runtime.freezeAndDedupString(runtime.newString("EXIT")), runtime.newFixnum(0));
      recv.getInternalVariables().setInternalVariable("signal_list", names);
    } else {
      names.dup(context);
    }
  }
  
  return names;
}

代码示例来源:origin: org.jruby/jruby-core

public static RubyString newFrozenString(ThreadContext context, ByteList bytelist, int coderange, String file, int line) {
  Ruby runtime = context.runtime;
  RubyString string = RubyString.newString(runtime, bytelist, coderange);
  if (runtime.getInstanceConfig().isDebuggingFrozenStringLiteral()) {
    // stuff location info into the string and then freeze it
    RubyArray info = (RubyArray) runtime.newArray(runtime.newString(file).freeze(context), runtime.newFixnum(line)).freeze(context);
    string.setInstanceVariable(RubyString.DEBUG_INFO_FIELD, info);
    string.setFrozen(true);
  } else {
    string = runtime.freezeAndDedupString(string);
  }
  return string;
}

代码示例来源:origin: org.jruby/jruby-complete

public static RubyString newFrozenString(ThreadContext context, ByteList bytelist, int coderange, String file, int line) {
  Ruby runtime = context.runtime;
  RubyString string = RubyString.newString(runtime, bytelist, coderange);
  if (runtime.getInstanceConfig().isDebuggingFrozenStringLiteral()) {
    // stuff location info into the string and then freeze it
    RubyArray info = (RubyArray) runtime.newArray(runtime.newString(file).freeze(context), runtime.newFixnum(line)).freeze(context);
    string.setInstanceVariable(RubyString.DEBUG_INFO_FIELD, info);
    string.setFrozen(true);
  } else {
    string = runtime.freezeAndDedupString(string);
  }
  return string;
}

代码示例来源:origin: org.jruby/jruby-core

@JRubyMethod(name = "name=", required = 1)
public IRubyObject setName(IRubyObject name) {
  final Ruby runtime = getRuntime();
  if (!name.isNil()) {
    RubyString nameStr = StringSupport.checkEmbeddedNulls(runtime, name);
    Encoding enc = nameStr.getEncoding();
    if (!enc.isAsciiCompatible()) {
      throw runtime.newArgumentError("ASCII incompatible encoding (" + enc + ")");
    }
    threadImpl.setRubyName(runtime.freezeAndDedupString(nameStr).asJavaString());
  } else {
    threadImpl.setRubyName(null);
  }
  return name;
}

代码示例来源:origin: org.jruby/jruby-complete

@JRubyMethod(name = "name=", required = 1)
public IRubyObject setName(IRubyObject name) {
  final Ruby runtime = getRuntime();
  if (!name.isNil()) {
    RubyString nameStr = StringSupport.checkEmbeddedNulls(runtime, name);
    Encoding enc = nameStr.getEncoding();
    if (!enc.isAsciiCompatible()) {
      throw runtime.newArgumentError("ASCII incompatible encoding (" + enc + ")");
    }
    threadImpl.setRubyName(runtime.freezeAndDedupString(nameStr).asJavaString());
  } else {
    threadImpl.setRubyName(null);
  }
  return name;
}

相关文章

Ruby类方法