<commit_before>package com.cliqz.jsengine;

import android.content.Context;
import android.util.Log;

import com.cliqz.jsengine.v8.JSApiException;
import com.cliqz.jsengine.v8.JSConsole;
import com.cliqz.jsengine.v8.Timers;
import com.cliqz.jsengine.v8.V8Engine;
import com.cliqz.jsengine.v8.api.ChromeUrlHandler;
import com.cliqz.jsengine.v8.api.Crypto;
import com.cliqz.jsengine.v8.api.FileIO;
import com.cliqz.jsengine.v8.api.HttpRequestPolicy;
import com.cliqz.jsengine.v8.api.SystemLoader;
import com.cliqz.jsengine.v8.api.WebRequest;
import com.eclipsesource.v8.utils.MemoryManager;

import org.json.JSONObject;

import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

public class Engine {

    private final static String TAG = Engine.class.getSimpleName();
    final V8Engine jsengine;
    private final Context context;
    private final Set<Object> jsApis = new HashSet<>();
    final SystemLoader system;
    public final WebRequest webRequest;
    private boolean mIsRunning = false;
    boolean debugMode = false;

    private static final String BUILD_PATH = "build";

    public Engine(final Context context, final boolean mobileDataEnabled) throws JSApiException {
        this.context = context.getApplicationContext();
        jsengine = new V8Engine();
        // load js APIs
        jsApis.add(new Timers(jsengine));
        jsApis.add(new JSConsole(jsengine));
        jsApis.add(new FileIO(jsengine, this.context));
        jsApis.add(new Crypto(jsengine));
        webRequest = new WebRequest(jsengine, this.context);
        system = new SystemLoader(jsengine, this.context, BUILD_PATH + "/modules");
        final HttpRequestPolicy policy = mobileDataEnabled ? HttpRequestPolicy.ALWAYS_ALLOWED : new HttpRequestPolicy.AllowOnWifi(this.context);
        jsApis.add(new ChromeUrlHandler(jsengine, policy, system));
    }

    public boolean isRunning() {
        return mIsRunning;
    }

    public synchronized void startup(Map<String, Object> defaultPrefs) throws ExecutionException {
        if (!isRunning()) {
            try {
                // load config
                String config = system.readSourceFile(BUILD_PATH + "/config/cliqz.json");
                jsengine.executeScript("var __CONFIG__ = JSON.parse(\"" + config.replace("\"", "\\\"").replace("\n", "") + "\");");
                jsengine.executeScript("var __DEFAULTPREFS__ = JSON.parse('" + new JSONObject(defaultPrefs).toString() + "');");
                system.callVoidFunctionOnModule("platform/startup", "startup");
                mIsRunning = true;
            } catch (IOException e) {
                throw new ExecutionException(e);
            }
        }
    }

    public void startup() throws ExecutionException {
        startup(new HashMap<String, Object>());
    }

    public void shutdown() throws ExecutionException {
        shutdown(false);
    }

    public void shutdown(boolean strict) throws ExecutionException {
        try {
            system.callVoidFunctionOnModule("platform/startup", "shutdown");
        } finally {
            jsengine.shutdown(strict);
            mIsRunning = false;
        }
    }

    public synchronized void setPref(final String prefName, final Object value) throws ExecutionException {
        system.callVoidFunctionOnModuleAttribute("core/utils", new String[] {"default"}, "setPref", prefName, value);
    }

    public synchronized Object getPref(String prefName) throws ExecutionException {
        return system.callFunctionOnModuleDefault("core/utils", "getPref");
    }

    public void setLoggingEnabled(boolean enabled) throws ExecutionException {
        setPref("showConsoleLogs", enabled);
    }

}<commit_msg>It should fix performance problems<commit_after>package com.cliqz.jsengine;

import android.content.Context;

import com.cliqz.jsengine.v8.JSApiException;
import com.cliqz.jsengine.v8.JSConsole;
import com.cliqz.jsengine.v8.Timers;
import com.cliqz.jsengine.v8.V8Engine;
import com.cliqz.jsengine.v8.api.ChromeUrlHandler;
import com.cliqz.jsengine.v8.api.Crypto;
import com.cliqz.jsengine.v8.api.FileIO;
import com.cliqz.jsengine.v8.api.HttpRequestPolicy;
import com.cliqz.jsengine.v8.api.SystemLoader;
import com.cliqz.jsengine.v8.api.WebRequest;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Engine {

    private final static String TAG = Engine.class.getSimpleName();
    final V8Engine jsengine;
    private final Context context;
    private final Set<Object> jsApis = new HashSet<>();
    final SystemLoader system;
    public final WebRequest webRequest;
    private volatile boolean mIsRunning = false;
    private final ExecutorService service;

    private static final String BUILD_PATH = "build";

    public Engine(final Context context, final boolean mobileDataEnabled) throws JSApiException {
        this.context = context.getApplicationContext();
        this.service = Executors.newSingleThreadExecutor();
        jsengine = new V8Engine();
        // load js APIs
        jsApis.add(new Timers(jsengine));
        jsApis.add(new JSConsole(jsengine));
        jsApis.add(new FileIO(jsengine, this.context));
        jsApis.add(new Crypto(jsengine));
        webRequest = new WebRequest(jsengine, this.context);
        system = new SystemLoader(jsengine, this.context, BUILD_PATH + "/modules");
        final HttpRequestPolicy policy = mobileDataEnabled ? HttpRequestPolicy.ALWAYS_ALLOWED : new HttpRequestPolicy.AllowOnWifi(this.context);
        jsApis.add(new ChromeUrlHandler(jsengine, policy, system));
    }

    public void startup(final Map<String, Object> prefs) throws ExecutionException {
        synchronized (this) {
            if (mIsRunning) {
                return;
            }
            mIsRunning = true;
        }
        service.submit(new Runnable() {
            @Override
            public void run() {
                try {
                    // load config
                    String config = system.readSourceFile(BUILD_PATH + "/config/cliqz.json");
                    jsengine.executeScript("var __CONFIG__ = JSON.parse(\"" + config.replace("\"", "\\\"").replace("\n", "") + "\");");
                    jsengine.executeScript("var __DEFAULTPREFS__ = JSON.parse('" + new JSONObject(prefs).toString() + "');");
                    system.callVoidFunctionOnModule("platform/startup", "startup");
                    mIsRunning = true;
                } catch (Throwable e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public void startup() throws ExecutionException {
        startup(new HashMap<String, Object>());
    }

    public void shutdown() throws ExecutionException {
        shutdown(false);
    }

    public void shutdown(boolean strict) throws ExecutionException {
        synchronized (this) {
            if (!mIsRunning) {
                return;
            }
            try {
                system.callVoidFunctionOnModule("platform/startup", "shutdown");
            } finally {
                jsengine.shutdown(strict);
                service.shutdownNow();
                mIsRunning = false;
            }
        }
    }

    public void setPref(final String prefName, final Object value) {
        service.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    system.callVoidFunctionOnModuleAttribute("core/utils", new String[] {"default"}, "setPref", prefName, value);
                } catch (ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    public Object getPref(final String prefName) {
        final Future<Object> future = service.submit(new Callable<Object>() {
            @Override
            public Object call() throws Exception {
                return system.callFunctionOnModuleDefault("core/utils", "getPref", prefName);
            }
        });
        try {
            return future.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void setLoggingEnabled(boolean enabled) throws ExecutionException {
        setPref("showConsoleLogs", enabled);
    }

}
{'fraction_non_alphanumeric': 0.10503130071875724, 'fraction_numerical': 0.0031300718757245535, 'mean_word_length': 2.6679421768707483, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '680033', 'n_tokens_mistral': 2418, 'n_tokens_neox': 2370, 'n_words': 523}