<commit_before>package org.opentripplanner.profile;

import com.google.protobuf.CodedOutputStream;
import gnu.trove.iterator.TIntIntIterator;
import gnu.trove.iterator.TObjectIntIterator;
import gnu.trove.map.TIntIntMap;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TIntIntHashMap;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.vertextype.StreetVertex;
import org.opentripplanner.routing.vertextype.TransitStop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;

public class RaptorWorker {

    private static final Logger LOG = LoggerFactory.getLogger(RaptorWorker.class);
    public static final int UNREACHED = Integer.MAX_VALUE;
    static final int MAX_ROUNDS = 8;
    static final int MAX_DURATION = 90 * 60;

    int max_time = 0;
    int round = 0;
    List<int[]> timesPerStopPerRound;
    int[] timesPerStop;
    int[] bestTimes;
    int[] transferResults;
    RaptorWorkerData data;

    BitSet stopsTouched;
    BitSet patternsTouched;
    
    private ProfileRequest req;

    public RaptorWorker(RaptorWorkerData data, ProfileRequest req) {
        this.data = data;
        this.bestTimes = new int[data.nStops];
        stopsTouched = new BitSet(data.nStops);
        patternsTouched = new BitSet(data.nPatterns);
        this.req = req; 
        Arrays.fill(bestTimes, UNREACHED); // initialize once here and reuse on subsequent iterations.
    }

    public void advance () {
        round++;
//        timesPerStop = new int[data.nStops];
//        Arrays.fill(timesPerStop, UNREACHED);
//        timesPerStopPerRound.add(timesPerStop);
    }

    public PropagatedTimesStore runRaptor (Graph graph, TObjectIntMap<TransitStop> accessTimes, int[] walkTimes) {
        long beginCalcTime = System.currentTimeMillis();
        long totalPropagationTime = 0;
        TIntIntMap initialStops = new TIntIntHashMap();
        TObjectIntIterator<TransitStop> initialIterator = accessTimes.iterator();
        while (initialIterator.hasNext()) {
            initialIterator.advance();
            TransitStop tstop = initialIterator.key();
            int accessTime = initialIterator.value();
            int stopIndex = data.indexForStop.get(tstop.getStop());
            if (stopIndex == -1) {
                continue; // stop not used;
            }
            initialStops.put(stopIndex, accessTime);
        }
        PropagatedTimesStore propagatedTimesStore = new PropagatedTimesStore(graph);
        
        int iterations = (req.toTime - req.fromTime - 60) / 60 + 1;
        
        // Iterate backward through minutes (range-raptor) taking a snapshot of router state after each call
        int[][] timesAtTargetsEachMinute = new int[iterations][walkTimes.length];
        
        for (int departureTime = req.toTime - 60, n = 0; departureTime >= req.fromTime; departureTime -= 60, n++) {
            if (n % 15 == 0) {
                LOG.info("minute {}", n);
            }
            this.runRaptor(initialStops, departureTime);
            long beginPropagationTime = System.currentTimeMillis();
            int[] timesAtTargets = timesAtTargetsEachMinute[n];
            
            for (int i = 0; i < timesAtTargets.length; i++) {
            	timesAtTargets[i] = walkTimes[i];
            }
            
            for (int s = 0; s < data.nStops; s++) {
                int baseTimeSeconds = bestTimes[s];
                if (baseTimeSeconds != UNREACHED) {
                    baseTimeSeconds -= departureTime; // convert to travel time rather than clock time
                    int[] targets = data.targetsForStop.get(s);
                    for (int i = 0; i < targets.length; i++) {
                        int targetIndex = targets[i++]; // increment i after read
                        int distance = targets[i]; // i will be incremented at loop end
                        // distance in meters over walk speed in meters per second --> seconds
                        int egressWalkTimeSeconds = (int) (distance / req.walkSpeed);
                        int propagated_time = baseTimeSeconds + egressWalkTimeSeconds;
                        if (timesAtTargets[targetIndex] == 0 || timesAtTargets[targetIndex] > propagated_time) {
                            timesAtTargets[targetIndex] = propagated_time; // if greater OR ZERO replace TODO initialize arrays to MAX_VALUE?
                        }
                    }
                }
            }
            totalPropagationTime += (System.currentTimeMillis() - beginPropagationTime);
        }
        long calcTime = System.currentTimeMillis() - beginCalcTime;
        LOG.info("calc time {}sec", calcTime / 1000.0);
        LOG.info("  propagation {}sec", totalPropagationTime / 1000.0);<commit_msg>use arraycopy for maximum efficiency.<commit_after>package org.opentripplanner.profile;

import com.google.protobuf.CodedOutputStream;
import gnu.trove.iterator.TIntIntIterator;
import gnu.trove.iterator.TObjectIntIterator;
import gnu.trove.map.TIntIntMap;
import gnu.trove.map.TObjectIntMap;
import gnu.trove.map.hash.TIntIntHashMap;
import org.opentripplanner.routing.graph.Graph;
import org.opentripplanner.routing.vertextype.StreetVertex;
import org.opentripplanner.routing.vertextype.TransitStop;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.BitSet;
import java.util.List;

public class RaptorWorker {

    private static final Logger LOG = LoggerFactory.getLogger(RaptorWorker.class);
    public static final int UNREACHED = Integer.MAX_VALUE;
    static final int MAX_ROUNDS = 8;
    static final int MAX_DURATION = 90 * 60;

    int max_time = 0;
    int round = 0;
    List<int[]> timesPerStopPerRound;
    int[] timesPerStop;
    int[] bestTimes;
    int[] transferResults;
    RaptorWorkerData data;

    BitSet stopsTouched;
    BitSet patternsTouched;
    
    private ProfileRequest req;

    public RaptorWorker(RaptorWorkerData data, ProfileRequest req) {
        this.data = data;
        this.bestTimes = new int[data.nStops];
        stopsTouched = new BitSet(data.nStops);
        patternsTouched = new BitSet(data.nPatterns);
        this.req = req; 
        Arrays.fill(bestTimes, UNREACHED); // initialize once here and reuse on subsequent iterations.
    }

    public void advance () {
        round++;
//        timesPerStop = new int[data.nStops];
//        Arrays.fill(timesPerStop, UNREACHED);
//        timesPerStopPerRound.add(timesPerStop);
    }

    public PropagatedTimesStore runRaptor (Graph graph, TObjectIntMap<TransitStop> accessTimes, int[] walkTimes) {
        long beginCalcTime = System.currentTimeMillis();
        long totalPropagationTime = 0;
        TIntIntMap initialStops = new TIntIntHashMap();
        TObjectIntIterator<TransitStop> initialIterator = accessTimes.iterator();
        while (initialIterator.hasNext()) {
            initialIterator.advance();
            TransitStop tstop = initialIterator.key();
            int accessTime = initialIterator.value();
            int stopIndex = data.indexForStop.get(tstop.getStop());
            if (stopIndex == -1) {
                continue; // stop not used;
            }
            initialStops.put(stopIndex, accessTime);
        }
        PropagatedTimesStore propagatedTimesStore = new PropagatedTimesStore(graph);
        
        int iterations = (req.toTime - req.fromTime - 60) / 60 + 1;
        
        // Iterate backward through minutes (range-raptor) taking a snapshot of router state after each call
        int[][] timesAtTargetsEachMinute = new int[iterations][walkTimes.length];
        
        for (int departureTime = req.toTime - 60, n = 0; departureTime >= req.fromTime; departureTime -= 60, n++) {
            if (n % 15 == 0) {
                LOG.info("minute {}", n);
            }
            this.runRaptor(initialStops, departureTime);
            long beginPropagationTime = System.currentTimeMillis();
            int[] timesAtTargets = timesAtTargetsEachMinute[n];

            System.arraycopy(walkTimes, 0, timesAtTargets, 0, walkTimes.length);
            
            for (int i = 0; i < timesAtTargets.length; i++) {
            	timesAtTargets[i] = walkTimes[i];
            }
            
            for (int s = 0; s < data.nStops; s++) {
                int baseTimeSeconds = bestTimes[s];
                if (baseTimeSeconds != UNREACHED) {
                    baseTimeSeconds -= departureTime; // convert to travel time rather than clock time
                    int[] targets = data.targetsForStop.get(s);
                    for (int i = 0; i < targets.length; i++) {
                        int targetIndex = targets[i++]; // increment i after read
                        int distance = targets[i]; // i will be incremented at loop end
                        // distance in meters over walk speed in meters per second --> seconds
                        int egressWalkTimeSeconds = (int) (distance / req.walkSpeed);
                        int propagated_time = baseTimeSeconds + egressWalkTimeSeconds;
                        if (timesAtTargets[targetIndex] == 0 || timesAtTargets[targetIndex] > propagated_time) {
                            timesAtTargets[targetIndex] = propagated_time; // if greater OR ZERO replace TODO initialize arrays to MAX_VALUE?
                        }
                    }
                }
            }
            totalPropagationTime += (System.currentTimeMillis() - beginPropagationTime);
        }
        long calcTime = System.currentTimeMillis() - beginCalcTime;
        LOG.info("calc time {}sec", calcTime / 1000.0);
        LOG.info("  propagation {}sec", totalPropagationTime / 1000.0);
{'fraction_non_alphanumeric': 0.0955337472461446, 'fraction_numerical': 0.007810935309433206, 'mean_word_length': 2.344608171466845, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '255075', 'n_tokens_mistral': 2891, 'n_tokens_neox': 2767, 'n_words': 676}