<commit_before>package com.cueup.hegemon.compilation;

import com.google.common.base.Objects;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;

import java.lang.ref.WeakReference;

/**
 * In-memory cache for script compilation.
 */
public class CachedScriptCompilation implements ScriptCompilation {

  /**
   * We cache compiled scripts to save time during initialization and tests.
   */
  private class CompilationKey {

    private final WeakReference<Context> c;
    private final String source;
    private final String name;
    private final int hash;

    public CompilationKey(Context c, String source, String name) {
      this.c = new WeakReference<Context>(c);
      this.source = source;
      this.name = name;
      this.hash = Objects.hashCode(System.identityHashCode(c), source, name);
    }


    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      CompilationKey other = (CompilationKey) o;

      return this.hash == other.hash
          && this.c.get() == other.c.get() && this.c.get() != null
          && this.name.equals(other.name)
          && this.source.equals(other.source);
    }


    @Override
    public int hashCode() {
      return this.hash;
    }
  }

  private final LoadingCache<CompilationKey, Script> compilationCache;


  public CachedScriptCompilation(final ScriptCompilation compilation) {
    this.compilationCache = CacheBuilder.newBuilder()
        .maximumSize(500)
        .build(new CacheLoader<CompilationKey, Script>() {
          @Override
          public org.mozilla.javascript.Script load(CompilationKey key) throws Exception {
            return compilation.compile(key.c.get(), key.name, key.source);
          }
        });
  }


  @Override
  public Script compile(Context c, String name, String source) {
    return this.compilationCache.getUnchecked(new CompilationKey(c, source, name));
  }

}<commit_msg>Don't key script objects by context<commit_after>package com.cueup.hegemon.compilation;

import com.google.common.base.Objects;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Script;

import java.lang.ref.WeakReference;

/**
 * In-memory cache for script compilation.
 */
public class CachedScriptCompilation implements ScriptCompilation {

  /**
   * We cache compiled scripts to save time during initialization and tests.
   */
  private class CompilationKey {

    private final WeakReference<Context> context;
    private final String source;
    private final String name;
    private final int hash;

    public CompilationKey(Context context, String source, String name) {
      this.context = new WeakReference<Context>(context);
      this.source = source;
      this.name = name;
      this.hash = Objects.hashCode(source, name);
    }


    @Override
    public boolean equals(Object o) {
      if (this == o) {
        return true;
      }
      if (o == null || getClass() != o.getClass()) {
        return false;
      }

      CompilationKey other = (CompilationKey) o;

      return this.hash == other.hash
          && this.name.equals(other.name)
          && this.source.equals(other.source);
    }


    @Override
    public int hashCode() {
      return this.hash;
    }
  }

  private final LoadingCache<CompilationKey, Script> compilationCache;


  public CachedScriptCompilation(final ScriptCompilation compilation) {
    this.compilationCache = CacheBuilder.newBuilder()
        .maximumSize(500)
        .build(new CacheLoader<CompilationKey, Script>() {
          @Override
          public org.mozilla.javascript.Script load(CompilationKey key) throws Exception {
            return compilation.compile(key.context.get(), key.name, key.source);
          }
        });
  }


  @Override
  public Script compile(Context c, String name, String source) {
    return this.compilationCache.getUnchecked(new CompilationKey(c, source, name));
  }

}
{'fraction_non_alphanumeric': 0.1040745052386496, 'fraction_numerical': 0.0013969732246798604, 'mean_word_length': 3.0300187617260788, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4744416', 'n_tokens_mistral': 1227, 'n_tokens_neox': 1174, 'n_words': 300}