<commit_before>//
//  Benchmark.swift
//  SwiftLint
//
//  Created by <NAME> on 1/25/16.
//  Copyright © 2016 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

struct BenchmarkEntry {
    let id: String // swiftlint:disable:this identifier_name
    let time: Double
}

struct Benchmark {
    private let name: String
    private var entries = [BenchmarkEntry]()

    init(name: String) {
        self.name = name
    }

    // swiftlint:disable:next identifier_name
    mutating func record(id: String, time: Double) {
        entries.append(BenchmarkEntry(id: id, time: time))
    }

    mutating func record(file: File, from start: Date) {
        record(id: file.path ?? "<nopath>", time: -start.timeIntervalSinceNow)
    }

    func save() {
        let string = entries
            .reduce([String: Double]()) { accu, idAndTime in
                var accu = accu
                accu[idAndTime.id] = (accu[idAndTime.id] ?? 0) + idAndTime.time
                return accu
            }
            .sorted { $0.1 < $1.1 }
            .map { "\(numberFormatter.string(from: NSNumber(value: $0.1))!): \($0.0)" }
            .joined(separator: "\n")
        let data = (string + "\n").data(using: .utf8)
        let url = URL(fileURLWithPath: "benchmark_\(name)_\(timestamp).txt")
        try? data?.write(to: url, options: [.atomic])
    }
}

private let numberFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.minimumFractionDigits = 3
    return formatter
}()

private let timestamp: String = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy_MM_dd_HH_mm_ss"
    return formatter.string(from: Date())
}()<commit_msg>decompose Benchmark.save() to improve compile times<commit_after>//
//  Benchmark.swift
//  SwiftLint
//
//  Created by <NAME> on 1/25/16.
//  Copyright © 2016 Realm. All rights reserved.
//

import Foundation
import SourceKittenFramework

struct BenchmarkEntry {
    let id: String // swiftlint:disable:this identifier_name
    let time: Double
}

struct Benchmark {
    private let name: String
    private var entries = [BenchmarkEntry]()

    init(name: String) {
        self.name = name
    }

    // swiftlint:disable:next identifier_name
    mutating func record(id: String, time: Double) {
        entries.append(BenchmarkEntry(id: id, time: time))
    }

    mutating func record(file: File, from start: Date) {
        record(id: file.path ?? "<nopath>", time: -start.timeIntervalSinceNow)
    }

    func save() {
        // Decomposed to improve compile times
        let entriesDict: [String: Double] = entries.reduce([String: Double]()) { accu, idAndTime in
            var accu = accu
            accu[idAndTime.id] = (accu[idAndTime.id] ?? 0) + idAndTime.time
            return accu
        }
        let entriesKeyValues: [(String, Double)] = entriesDict.sorted { $0.1 < $1.1 }
        let lines: [String] = entriesKeyValues.map { id, time -> String in
            return "\(numberFormatter.string(from: NSNumber(value: time))!): \(id)"
        }
        let string: String = lines.joined(separator: "\n") + "\n"
        let url = URL(fileURLWithPath: "benchmark_\(name)_\(timestamp).txt")
        try? string.data(using: .utf8)?.write(to: url, options: [.atomic])
    }
}

private let numberFormatter: NumberFormatter = {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.minimumFractionDigits = 3
    return formatter
}()

private let timestamp: String = {
    let formatter = DateFormatter()
    formatter.dateFormat = "yyyy_MM_dd_HH_mm_ss"
    return formatter.string(from: Date())
}()
{'fraction_non_alphanumeric': 0.1279826464208243, 'fraction_numerical': 0.009761388286334056, 'mean_word_length': 2.6743027888446216, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 8, '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': '2875618', 'n_tokens_mistral': 1188, 'n_tokens_neox': 1134, 'n_words': 293}