<commit_before>package org.javacs.kt

import org.jetbrains.kotlin.com.intellij.openapi.Disposable
import org.jetbrains.kotlin.com.intellij.openapi.vfs.StandardFileSystems
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileManager
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileSystem
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.cli.jvm.compiler.CliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.container.ComponentProvider
import org.jetbrains.kotlin.container.ValueDescriptor
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.BindingTraceContext
import org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer
import org.jetbrains.kotlin.resolve.TopDownAnalysisMode
import org.jetbrains.kotlin.resolve.calls.callUtil.getParentResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfoFactory
import org.junit.Test
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.runner.Runner
import org.openjdk.jmh.runner.RunnerException
import org.openjdk.jmh.runner.options.Options
import org.openjdk.jmh.runner.options.OptionsBuilder
import org.javacs.kt.util.LoggingMessageCollector

import java.lang.reflect.Type
import java.net.URL

class OneFilePerformance {
    @State(Scope.Thread)
    class ReusableParts {
        internal var config = CompilerConfiguration()
        init {
            config.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME)
            config.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, LoggingMessageCollector)
        }
        internal var env = KotlinCoreEnvironment.createForProduction(object: Disposable {
            override fun dispose() {
                // Do nothing
            }
        }, config, EnvironmentConfigFiles.JVM_CONFIG_FILES)
        internal var parser = KtPsiFactory(env.project)
        internal var fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL)
        internal var bigFile = openFile("/kotlinCompilerPerformance/BigFile.kt")
        
        internal fun openFile(resourcePath: String?): KtFile {
            val locate = OneFilePerformance::class.java.getResource(resourcePath)
            val file = fileSystem.findFileByPath(locate.path)
            return PsiManager.getInstance(env.project).findFile(file!!) as KtFile
        }
        
        internal fun compile(compile: Collection<KtFile>, sourcePath: Collection<KtFile>): BindingTraceContext {
            val trace = CliBindingTrace()
            val container = CompilerFixtures.createContainer(
                env.project,
                sourcePath,
                trace,
                env.configuration,
                env::createPackagePartProvider
            )
            val analyze = container.create(LazyTopDownAnalyzer::class.java)
            analyze.analyzeDeclarations(TopDownAnalysisMode.TopLevelDeclarations, compile, DataFlowInfoFactory.EMPTY)
            return trace
        }
    }
    
    @Benchmark
    fun recompileBigFile(state: ReusableParts) {
        val path = listOf(state.bigFile)
        state.compile(path, path)
    }
    
    @Benchmark
    fun recompileSmallFile(state: ReusableParts) {
        val smallFile = state.openFile("/kotlinCompilerPerformance/ReferencesBigFile.kt")
        val analyze = state.compile(listOf(smallFile), listOf(smallFile, state.bigFile))
        // Check reference
        val ref = PsiTreeUtil.getNonStrictParentOfType(smallFile.findElementAt(80), KtElement::class.java)
        val call = ref.getParentResolvedCall(analyze.getBindingContext(), false)
        if (!call!!.getCandidateDescriptor().getName().asString().equals("max"))
            throw RuntimeException("Expected BigFile.max but found " + call)
    }
    
    @Benchmark
    fun recompileBoth(state: ReusableParts) {
        val bigFile = state.openFile("/kotlinCompilerPerformance/BigFile.kt")
        val smallFile = state.openFile("/kotlinCompilerPerformance/ReferencesBigFile.kt")
        val analyze = state.compile(listOf(smallFile, bigFile), listOf(smallFile, bigFile))
        // Check reference
        val ref = PsiTreeUtil.getNonStrictParentOfType(smallFile.findElementAt(80), KtElement::class.java)
        val call = ref.getParentResolvedCall(analyze.getBindingContext(), false)
        if (!call!!.getCandidateDescriptor().getName().asString().equals("max"))
            throw RuntimeException("Expected BigFile.max but found " + call)
    }
    
    @Test
    fun checkRecompileBoth() {
        val state = ReusableParts()
        recompileBoth(state)
    }
    
    @Test
    fun checkRecompileOne() {
        val state = ReusableParts()
        state.compile(setOf(state.bigFile), setOf(state.bigFile))
        recompileSmallFile(state)
    }
    <commit_msg>Fix potential memory leak in OneFilePerformance<commit_after>package org.javacs.kt

import org.jetbrains.kotlin.com.intellij.openapi.Disposable
import org.jetbrains.kotlin.com.intellij.openapi.util.Disposer
import org.jetbrains.kotlin.com.intellij.openapi.vfs.StandardFileSystems
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFile
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileManager
import org.jetbrains.kotlin.com.intellij.openapi.vfs.VirtualFileSystem
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.cli.jvm.compiler.CliBindingTrace
import org.jetbrains.kotlin.cli.jvm.compiler.EnvironmentConfigFiles
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCoreEnvironment
import org.jetbrains.kotlin.cli.common.CLIConfigurationKeys
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.config.CompilerConfiguration
import org.jetbrains.kotlin.container.ComponentProvider
import org.jetbrains.kotlin.container.ValueDescriptor
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.psi.KtElement
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.KtPsiFactory
import org.jetbrains.kotlin.resolve.BindingTraceContext
import org.jetbrains.kotlin.resolve.LazyTopDownAnalyzer
import org.jetbrains.kotlin.resolve.TopDownAnalysisMode
import org.jetbrains.kotlin.resolve.calls.callUtil.getParentResolvedCall
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfoFactory
import org.junit.Test
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.State
import org.openjdk.jmh.annotations.TearDown
import org.openjdk.jmh.annotations.Level
import org.openjdk.jmh.runner.Runner
import org.openjdk.jmh.runner.RunnerException
import org.openjdk.jmh.runner.options.Options
import org.openjdk.jmh.runner.options.OptionsBuilder
import org.javacs.kt.util.LoggingMessageCollector

import java.lang.reflect.Type
import java.net.URL
import java.io.Closeable

class OneFilePerformance {
    @State(Scope.Thread)
    class ReusableParts : Closeable {
        internal var config = CompilerConfiguration()
        init {
            config.put(CommonConfigurationKeys.MODULE_NAME, JvmAbi.DEFAULT_MODULE_NAME)
            config.put(CLIConfigurationKeys.MESSAGE_COLLECTOR_KEY, LoggingMessageCollector)
        }
        internal val disposable = Disposer.newDisposable()
        internal var env = KotlinCoreEnvironment.createForProduction(
            disposable, config, EnvironmentConfigFiles.JVM_CONFIG_FILES
        )
        internal var parser = KtPsiFactory(env.project)
        internal var fileSystem = VirtualFileManager.getInstance().getFileSystem(StandardFileSystems.FILE_PROTOCOL)
        internal var bigFile = openFile("/kotlinCompilerPerformance/BigFile.kt")

        internal fun openFile(resourcePath: String?): KtFile {
            val locate = OneFilePerformance::class.java.getResource(resourcePath)
            val file = fileSystem.findFileByPath(locate.path)
            return PsiManager.getInstance(env.project).findFile(file!!) as KtFile
        }

        internal fun compile(compile: Collection<KtFile>, sourcePath: Collection<KtFile>): BindingTraceContext {
            val trace = CliBindingTrace()
            val container = CompilerFixtures.createContainer(
                env.project,
                sourcePath,
                trace,
                env.configuration,
                env::createPackagePartProvider
            )
            val analyze = container.create(LazyTopDownAnalyzer::class.java)
            analyze.analyzeDeclarations(TopDownAnalysisMode.TopLevelDeclarations, compile, DataFlowInfoFactory.EMPTY)
            return trace
        }

        @TearDown(Level.Trial)
        override fun close() {
            Disposer.dispose(disposable)
        }
    }

    @Benchmark
    fun recompileBigFile(state: ReusableParts) {
        val path = listOf(state.bigFile)
        state.compile(path, path)
    }

    @Benchmark
    fun recompileSmallFile(state: ReusableParts) {
        val smallFile = state.openFile("/kotlinCompilerPerformance/ReferencesBigFile.kt")
        val analyze = state.compile(listOf(smallFile), listOf(smallFile, state.bigFile))
        // Check reference
        val ref = PsiTreeUtil.getNonStrictParentOfType(smallFile.findElementAt(80), KtElement::class.java)
        val call = ref.getParentResolvedCall(analyze.getBindingContext(), false)
        if (!call!!.getCandidateDescriptor().getName().asString().equals("max"))
            throw RuntimeException("Expected BigFile.max but found " + call)
    }

    @Benchmark
    fun recompileBoth(state: ReusableParts) {
        val bigFile = state.openFile("/kotlinCompilerPerformance/BigFile.kt")
        val smallFile = state.openFile("/kotlinCompilerPerformance/ReferencesBigFile.kt")
        val analyze = state.compile(listOf(smallFile, bigFile), listOf(smallFile, bigFile))
        // Check reference
        val ref = PsiTreeUtil.getNonStrictParentOfType(smallFile.findElementAt(80), KtElement::class.java)
        val call = ref.getParentResolvedCall(analyze.getBindingContext(), false)
        if (!call!!.getCandidateDescriptor().getName().asString().equals("max"))
            throw RuntimeException("Expected BigFile.max but found " + call)
    }

    @Test
    fun checkRecompileBoth() {
        ReusableParts().use { state ->
            recompileBoth(state)
        }
    }

    @Test
    fun checkRecompileOne() {
        ReusableParts().use { state ->
            state.compile(setOf(state.bigFile), setOf(state.bigFile))
            recompileSmallFile(state)
        }
    }

{'fraction_non_alphanumeric': 0.0912494558119286, 'fraction_numerical': 0.000696560731388768, 'mean_word_length': 3.6653127538586516, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5710039', 'n_tokens_mistral': 3490, 'n_tokens_neox': 3299, 'n_words': 504}