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: 1df58b54c183
Choose a base ref
...
head repository: jruby/jruby
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 7583943f55b5
Choose a head ref
  • 3 commits
  • 2 files changed
  • 2 contributors

Commits on Mar 10, 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 committed Mar 10, 2016
    Copy the full SHA
    b144429 View commit details
  2. Copy the full SHA
    a69c0e4 View commit details

Commits on Mar 11, 2016

  1. Merge pull request #3723 from kares/test-lazy-secure-random

     lazy secure random
    kares committed Mar 11, 2016
    Copy the full SHA
    7583943 View commit details
Showing with 25 additions and 15 deletions.
  1. +3 −4 core/src/main/java/org/jruby/ext/securerandom/SecureRandomLibrary.java
  2. +22 −11 core/src/main/java/org/jruby/runtime/ThreadContext.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.jruby.ext.securerandom;

import org.jruby.CompatVersion;
import org.jruby.RubyString;
import org.jruby.anno.JRubyMethod;
import org.jruby.runtime.ThreadContext;
@@ -31,13 +30,13 @@ public static IRubyObject hex(ThreadContext context, IRubyObject self, IRubyObje
return RubyString.newStringNoCopy(context.runtime, ConvertBytes.twosComplementToHexBytes(nextBytes(context, n), false));
}

@JRubyMethod(meta = true, compat = CompatVersion.RUBY1_9)
@JRubyMethod(meta = true)
public static IRubyObject uuid(ThreadContext context, IRubyObject self) {
return RubyString.newStringNoCopy(context.runtime, ConvertBytes.bytesToUUIDBytes(nextBytes(context, 16), false));
}

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 +45,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;
}
33 changes: 22 additions & 11 deletions core/src/main/java/org/jruby/runtime/ThreadContext.java
Original file line number Diff line number Diff line change
@@ -132,21 +132,32 @@ public static ThreadContext newContext(Ruby runtime) {
private Block.Type currentBlockType; // See prepareBlockArgs code in IRRuntimeHelpers
private Throwable savedExcInLambda; // See handleBreakAndReturnsInLambda in IRRuntimeHelpers

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

private static boolean trySHA1PRNG = true;

private static SecureRandom getSecureRandom() {
SecureRandom sr;
try {
sr = trySHA1PRNG ?
SecureRandom.getInstance("SHA1PRNG") :
new SecureRandom();
} catch (Exception e) {
trySHA1PRNG = false;
sr = new SecureRandom();
public SecureRandom getSecureRandom() {
SecureRandom secureRandom = this.secureRandom;
if (secureRandom == null) {
if (trySHA1PRNG) {
try {
secureRandom = SecureRandom.getInstance("SHA1PRNG");
} catch (Exception e) {
trySHA1PRNG = false;
}
}
if (secureRandom == null) {
secureRandom = new SecureRandom();
}
this.secureRandom = secureRandom;
}
return sr;
return secureRandom;
}

/**