Files
openide/json/src/com/jetbrains/jsonSchema/impl/jsonSchemaValidityCache.kt
Nikita Katkov 23fb60afd8 [json] IJPL-63554 Implemented fast exit for json schema validators
- If requested, validation will stop as soon as any error is found. This is extremelly important performance optimisation that plays well with the recenty introduced if-else branch computation. The number of calls to JsonSchemaResolver.isCorrect() increased dramatically, even more json-schema subsystem refactoring was demanded.

The existing API didn't assume any kind of laziness or cancellability. The refactoring is performed in a way to cause minimal number of changes in code and API. It'd be great to rewrite the entire validation code to sequence/analogs once and drop complicated JsonAnnotationsCollectionMode

GitOrigin-RevId: 4e62f7db76ed6b4071accbe1b80151c4b4664342
2024-07-16 00:37:45 +00:00

48 lines
1.8 KiB
Kotlin

// Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.jetbrains.jsonSchema.impl
import com.intellij.openapi.util.Key
import com.intellij.psi.util.CachedValue
import com.intellij.psi.util.CachedValueProvider
import com.intellij.psi.util.CachedValuesManager
import com.jetbrains.jsonSchema.extension.JsonAnnotationsCollectionMode
import com.jetbrains.jsonSchema.extension.adapters.JsonValueAdapter
import com.jetbrains.jsonSchema.ide.JsonSchemaService
import java.util.concurrent.ConcurrentHashMap
private val JSON_SCHEMA_VALIDATION_MAP = Key.create<CachedValue<MutableMap<JsonSchemaObject, Boolean>>>("JSON_SCHEMA_VALIDATION_MAP")
internal fun getOrComputeAdapterValidityAgainstGivenSchema(value: JsonValueAdapter, schema: JsonSchemaObject): Boolean {
val delegatePsi = value.delegate
val cachedMap: MutableMap<JsonSchemaObject, Boolean> =
CachedValuesManager.getManager(delegatePsi.project).getCachedValue(
delegatePsi,
JSON_SCHEMA_VALIDATION_MAP,
CachedValueProvider {
CachedValueProvider.Result.create(
ConcurrentHashMap(),
delegatePsi.manager.modificationTracker.forLanguage(delegatePsi.language),
JsonSchemaService.Impl.get(delegatePsi.project)
)
},
false
)
val cachedValue = cachedMap[schema]
if (cachedValue != null) {
return cachedValue
}
val checker = JsonSchemaAnnotatorChecker(
value.delegate.project,
JsonComplianceCheckerOptions(false,
false,
false,
JsonAnnotationsCollectionMode.FIND_FIRST)
)
checker.checkByScheme(value, schema)
val computedValue = checker.isCorrect
cachedMap[schema] = computedValue
return computedValue
}