Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jruby/jruby
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 3b556f6cb228
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b9bcb01ecd81
Choose a head ref
  • 5 commits
  • 3 files changed
  • 2 contributors

Commits on Sep 12, 2016

  1. avoid eager secure random initialization on every new Ruby thread all…

    …ocation
    
    shaves off 10-15% of Thread.new/start :
    
    ```
    Rehearsal ----------------------------------------------------------
    Thread.new [100000x]    12.800000   6.850000  19.650000 ( 10.392600)
    Thread.start [100000x]  12.670000   5.680000  18.350000 (  9.625846)
    ------------------------------------------------ total: 38.000000sec
    
                                 user     system      total        real
    Thread.new [100000x]    12.780000   5.760000  18.540000 (  9.620000)
    Thread.start [100000x]  12.490000   5.810000  18.300000 (  9.524975)
    ```
    
    ```
    Rehearsal ----------------------------------------------------------
    Thread.new [100000x]    11.040000   5.560000  16.600000 (  8.708792)
    Thread.start [100000x]  10.740000   5.520000  16.260000 (  8.400990)
    ------------------------------------------------ total: 32.860000sec
    
                                 user     system      total        real
    Thread.new [100000x]    10.840000   5.620000  16.460000 (  8.506339)
    Thread.start [100000x]  10.460000   5.560000  16.020000 (  8.323819)
    ```
    kares authored and headius committed Sep 12, 2016
    Copy the full SHA
    8fb4b93 View commit details
  2. Copy the full SHA
    4e076c1 View commit details
  3. Actually settle on SHA1PRNG if it's available. #1405

    I botched the previous patch a bit by unconditionally re-assigning
    the secureRandom local to a default JDK new SecureRandom. This
    could cause systems without the default preferred PRNG
    (NativePRNGNonBlocking, Java 8+) to have slower thread startup and/or
    random number generation.
    headius committed Sep 12, 2016
    Copy the full SHA
    2af75fa View commit details

Commits on Sep 13, 2016

  1. Copy the full SHA
    09dcb93 View commit details

Commits on Sep 14, 2016

  1. Merge pull request #4149 from headius/backport_securerandom

    Backport securerandom
    headius authored Sep 14, 2016
    Copy the full SHA
    b9bcb01 View commit details
Original file line number Diff line number Diff line change
@@ -37,7 +37,7 @@ public static IRubyObject uuid(ThreadContext context, IRubyObject self) {
}

private static byte[] nextBytes(ThreadContext context, IRubyObject n) {
int size = n.isNil() ? 16 : (int)n.convertToInteger().getLongValue();
int size = n.isNil() ? 16 : (int) n.convertToInteger().getLongValue();

return nextBytes(context, size);
}
@@ -46,7 +46,7 @@ private static byte[] nextBytes(ThreadContext context, int size) {
if (size < 0) throw context.runtime.newArgumentError("negative argument: " + size);

byte[] bytes = new byte[size];
context.secureRandom.nextBytes(bytes);
context.getSecureRandom().nextBytes(bytes);

return bytes;
}
49 changes: 38 additions & 11 deletions core/src/main/java/org/jruby/runtime/ThreadContext.java
Original file line number Diff line number Diff line change
@@ -59,6 +59,7 @@
import org.jruby.runtime.scope.ManyVarsDynamicScope;
import org.jruby.util.RecursiveComparator;
import org.jruby.util.RubyDateFormatter;
import org.jruby.util.cli.Options;
import org.jruby.util.log.Logger;
import org.jruby.util.log.LoggerFactory;

@@ -135,21 +136,47 @@ public static ThreadContext newContext(Ruby runtime) {

IRubyObject lastExitStatus;

public final SecureRandom secureRandom;
/**
* This fields is no longer initialized, is null by default!
* Use {@link #getSecureRandom()} instead.
* @deprecated
*/
@Deprecated
public transient SecureRandom secureRandom;

private static boolean tryPreferredPRNG = true;
private static boolean trySHA1PRNG = true;

{
SecureRandom sr;
try {
sr = trySHA1PRNG ?
SecureRandom.getInstance("SHA1PRNG") :
new SecureRandom();
} catch (Exception e) {
trySHA1PRNG = false;
sr = new SecureRandom();
@SuppressWarnings("deprecation")
public SecureRandom getSecureRandom() {
SecureRandom secureRandom = this.secureRandom;

// Try preferred PRNG, which defaults to NativePRNGNonBlocking
if (secureRandom == null && tryPreferredPRNG) {
try {
secureRandom = SecureRandom.getInstance(Options.PREFERRED_PRNG.load());
} catch (Exception e) {
tryPreferredPRNG = false;
}
}

// Try SHA1PRNG
if (secureRandom == null && trySHA1PRNG) {
try {
secureRandom = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
trySHA1PRNG = false;
}
}

// Just let JDK do whatever it does
if (secureRandom == null) {
secureRandom = new SecureRandom();
}
secureRandom = sr;

this.secureRandom = secureRandom;

return secureRandom;
}

/**
1 change: 1 addition & 0 deletions core/src/main/java/org/jruby/util/cli/Options.java
Original file line number Diff line number Diff line change
@@ -146,6 +146,7 @@ public class Options {
public static final Option<Boolean> REIFY_VARIABLES = bool(MISCELLANEOUS, "reify.variables", false, "Attempt to expand instance vars into Java fields");
public static final Option<Boolean> PREFER_IPV4 = bool(MISCELLANEOUS, "net.preferIPv4", true, "Prefer IPv4 network stack");
public static final Option<Boolean> FCNTL_LOCKING = bool(MISCELLANEOUS, "file.flock.fcntl", true, "Use fcntl rather than flock for File#flock");
public static final Option<String> PREFERRED_PRNG = string(MISCELLANEOUS, "preferred.prng", "NativePRNGNonBlocking", "Set the preferred JDK-supported random number generator to use.");

public static final Option<Boolean> DEBUG_LOADSERVICE = bool(DEBUG, "debug.loadService", false, "Log require/load file searches.");
public static final Option<Boolean> DEBUG_LOADSERVICE_TIMING = bool(DEBUG, "debug.loadService.timing", false, "Log require/load parse+evaluate times.");