Skip to content

Commit

Permalink
Move cache for modules into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
jfmengels committed Apr 14, 2023
1 parent e075e43 commit 02f3a30
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 76 deletions.
79 changes: 15 additions & 64 deletions src/Review/Cache.elm
Original file line number Diff line number Diff line change
@@ -1,70 +1,21 @@
module Review.Cache exposing (FinalProjectEvaluationCache, ModuleEntry, ProjectFileCache, createEntryForProjectFileCache, createFinalProjectEvaluationCache, createModuleEntry, errors, errorsForFinalProjectEvaluationCache, errorsForMaybeProjectFileCache, errorsFromProjectFileCache, match, matchFinalProjectEvaluationCache, matchProjectFileCache, outputContext, outputContextForProjectFileCache, outputContextHash, outputContextHashForProjectFileCache, setErrorsForFinalProjectEvaluationCache, setErrorsForMaybeProjectFileCache, setErrorsForModule)
module Review.Cache exposing
( FinalProjectEvaluationCache
, ProjectFileCache
, createEntryForProjectFileCache
, createFinalProjectEvaluationCache
, errorsForFinalProjectEvaluationCache
, errorsForMaybeProjectFileCache
, errorsFromProjectFileCache
, matchFinalProjectEvaluationCache
, matchProjectFileCache
, outputContextForProjectFileCache
, outputContextHashForProjectFileCache
, setErrorsForFinalProjectEvaluationCache
, setErrorsForMaybeProjectFileCache
)

import Review.Cache.ContentHash as ContentHash exposing (ContentHash)
import Review.Cache.ContextHash as ContextHash exposing (ComparableContextHash, ContextHash)
import Review.RequestedData exposing (RequestedData(..))


type ModuleEntry error context
= ModuleEntry
{ contentHash : ContentHash
, inputContextHashes : ComparableContextHash context
, isFileIgnored : Bool
, errors : List error
, outputContext : context
, outputContextHash : ContextHash context
}


createModuleEntry :
{ contentHash : ContentHash
, inputContextHashes : ComparableContextHash context
, isFileIgnored : Bool
, errors : List error
, outputContext : context
}
-> ModuleEntry error context
createModuleEntry entry =
ModuleEntry
{ contentHash = entry.contentHash
, inputContextHashes = entry.inputContextHashes
, isFileIgnored = entry.isFileIgnored
, errors = entry.errors
, outputContext = entry.outputContext
, outputContextHash = ContextHash.create entry.outputContext
}


outputContext : ModuleEntry error context -> context
outputContext (ModuleEntry entry) =
entry.outputContext


outputContextHash : ModuleEntry error context -> ContextHash context
outputContextHash (ModuleEntry entry) =
entry.outputContextHash


errors : ModuleEntry error context -> List error
errors (ModuleEntry entry) =
entry.errors


setErrorsForModule : List error -> ModuleEntry error context -> ModuleEntry error context
setErrorsForModule newErrors (ModuleEntry entry) =
ModuleEntry { entry | errors = newErrors }


match : ContentHash -> ComparableContextHash context -> ModuleEntry error context -> { isFileIgnored : Bool, requestedData : RequestedData } -> Bool
match contentHash inputContexts (ModuleEntry entry) { isFileIgnored, requestedData } =
ContentHash.areEqual contentHash entry.contentHash
&& (inputContexts == entry.inputContextHashes)
&& (not (ruleCaresAboutIgnoredFiles requestedData) || isFileIgnored == entry.isFileIgnored)


ruleCaresAboutIgnoredFiles : RequestedData -> Bool
ruleCaresAboutIgnoredFiles (RequestedData { ignoredFiles }) =
ignoredFiles


{-| Variant where the content may be absent
Expand Down
81 changes: 81 additions & 0 deletions src/Review/Cache/Module.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
module Review.Cache.Module exposing
( Entry, create
, match
, errors, setErrors
, outputContext, outputContextHash
)

{-| Cache for the result of the analysis of a single module.
@docs Entry, create
@docs match
@docs errors, setErrors
@docs outputContext, outputContextHash
-}

import Review.Cache.ContentHash as ContentHash exposing (ContentHash)
import Review.Cache.ContextHash as ContextHash exposing (ComparableContextHash, ContextHash)
import Review.RequestedData exposing (RequestedData(..))


type Entry error context
= Entry
{ contentHash : ContentHash
, inputContextHashes : ComparableContextHash context
, isFileIgnored : Bool
, errors : List error
, outputContext : context
, outputContextHash : ContextHash context
}


create :
{ contentHash : ContentHash
, inputContextHashes : ComparableContextHash context
, isFileIgnored : Bool
, errors : List error
, outputContext : context
}
-> Entry error context
create entry =
Entry
{ contentHash = entry.contentHash
, inputContextHashes = entry.inputContextHashes
, isFileIgnored = entry.isFileIgnored
, errors = entry.errors
, outputContext = entry.outputContext
, outputContextHash = ContextHash.create entry.outputContext
}


match : ContentHash -> ComparableContextHash context -> Entry error context -> { isFileIgnored : Bool, requestedData : RequestedData } -> Bool
match contentHash inputContexts (Entry entry) { isFileIgnored, requestedData } =
ContentHash.areEqual contentHash entry.contentHash
&& (inputContexts == entry.inputContextHashes)
&& (not (ruleCaresAboutIgnoredFiles requestedData) || isFileIgnored == entry.isFileIgnored)


ruleCaresAboutIgnoredFiles : RequestedData -> Bool
ruleCaresAboutIgnoredFiles (RequestedData { ignoredFiles }) =
ignoredFiles


errors : Entry error context -> List error
errors (Entry entry) =
entry.errors


setErrors : List error -> Entry error context -> Entry error context
setErrors newErrors (Entry entry) =
Entry { entry | errors = newErrors }


outputContext : Entry error context -> context
outputContext (Entry entry) =
entry.outputContext


outputContextHash : Entry error context -> ContextHash context
outputContextHash (Entry entry) =
entry.outputContextHash
25 changes: 13 additions & 12 deletions src/Review/Rule.elm
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ import Json.Encode as Encode
import Review.Cache as Cache
import Review.Cache.ContentHash exposing (ContentHash)
import Review.Cache.ContextHash as ContextHash exposing (ComparableContextHash, ContextHash)
import Review.Cache.Module as ModuleCache
import Review.ElmProjectEncoder
import Review.Error exposing (InternalError)
import Review.Exceptions as Exceptions exposing (Exceptions)
Expand Down Expand Up @@ -4154,7 +4155,7 @@ type alias GraphModule =


type alias ModuleCacheEntry projectContext =
Cache.ModuleEntry (Error {}) projectContext
ModuleCache.Entry (Error {}) projectContext


type alias ProjectFileCache projectContext =
Expand Down Expand Up @@ -4243,7 +4244,7 @@ computeFinalContextHashes schema cache =
case getFolderFromTraversal traversalAndFolder of
Just _ ->
Dict.foldl
(\_ cacheEntry acc -> Cache.outputContextHash cacheEntry :: acc)
(\_ cacheEntry acc -> ModuleCache.outputContextHash cacheEntry :: acc)
projectContextHash
cache.moduleContexts
|> ContextHash.toComparable
Expand Down Expand Up @@ -4273,7 +4274,7 @@ computeFinalContext schema cache =
case getFolderFromTraversal traversalAndFolder of
Just { foldProjectContexts } ->
Dict.foldl
(\_ cacheEntry acc -> foldProjectContexts (Cache.outputContext cacheEntry) acc)
(\_ cacheEntry acc -> foldProjectContexts (ModuleCache.outputContext cacheEntry) acc)
projectContext
cache.moduleContexts

Expand All @@ -4289,7 +4290,7 @@ setRuleName ruleName_ error_ =
errorsFromCache : ProjectRuleCache projectContext -> List (Error {})
errorsFromCache cache =
List.concat
[ Dict.foldl (\_ cacheEntry acc -> List.append (Cache.errors cacheEntry) acc) [] cache.moduleContexts
[ Dict.foldl (\_ cacheEntry acc -> List.append (ModuleCache.errors cacheEntry) acc) [] cache.moduleContexts
, Cache.errorsForMaybeProjectFileCache cache.elmJson
, Cache.errorsForMaybeProjectFileCache cache.readme
, Cache.errorsForMaybeProjectFileCache cache.dependencies
Expand Down Expand Up @@ -4946,7 +4947,7 @@ computeProjectContextHashes traversalAndFolder project cache incoming initial =
|> Maybe.andThen (\graphModule -> Dict.get graphModule.node.label cache)
of
Just importedModuleCache ->
Cache.outputContextHash importedModuleCache :: acc
ModuleCache.outputContextHash importedModuleCache :: acc

Nothing ->
acc
Expand Down Expand Up @@ -4981,7 +4982,7 @@ computeProjectContext traversalAndFolder project cache incoming initial =
|> Maybe.andThen (\graphModule -> Dict.get graphModule.node.label cache)
of
Just importedModuleCache ->
foldProjectContexts (Cache.outputContext importedModuleCache) accContext
foldProjectContexts (ModuleCache.outputContext importedModuleCache) accContext

Nothing ->
accContext
Expand Down Expand Up @@ -5452,7 +5453,7 @@ createRuleProjectVisitor schema initialProject ruleData initialCache =

-- TODO This is called at the wrong moment: This contains the state of the project with fixes that haven't been applied.
, getErrors = \() -> errorsFromCache (finalCacheMarker schema.name hidden.ruleData.ruleId cache)
, setErrorsForModule = \filePath newErrors -> raiseCache { cache | moduleContexts = Dict.update filePath (Maybe.map (\entry -> Cache.setErrorsForModule newErrors entry)) cache.moduleContexts }
, setErrorsForModule = \filePath newErrors -> raiseCache { cache | moduleContexts = Dict.update filePath (Maybe.map (\entry -> ModuleCache.setErrors newErrors entry)) cache.moduleContexts }
, setErrorsForElmJson = \newErrors -> raiseCache { cache | elmJson = Cache.setErrorsForMaybeProjectFileCache newErrors cache.elmJson }
, setErrorsForReadme = \newErrors -> raiseCache { cache | readme = Cache.setErrorsForMaybeProjectFileCache newErrors cache.readme }
, setErrorsForDependencies = \newErrors -> raiseCache { cache | dependencies = Cache.setErrorsForMaybeProjectFileCache newErrors cache.dependencies }
Expand Down Expand Up @@ -5778,9 +5779,9 @@ createModuleVisitorFromProjectVisitorHelp schema raise hidden traversalAndFolder
isFileIgnored =
not (Exceptions.isFileWeWantReportsFor hidden.ruleData.exceptions filePath)

shouldReuseCache : Cache.ModuleEntry error projectContext -> Bool
shouldReuseCache : ModuleCacheEntry projectContext -> Bool
shouldReuseCache cacheEntry =
Cache.match
ModuleCache.match
moduleContentHash
inputContextHashes
cacheEntry
Expand Down Expand Up @@ -5825,9 +5826,9 @@ createModuleVisitorFromProjectVisitorHelp schema raise hidden traversalAndFolder
Nothing ->
schema.initialProjectContext

cacheEntry : Cache.ModuleEntry (Error {}) projectContext
cacheEntry : ModuleCacheEntry projectContext
cacheEntry =
Cache.createModuleEntry
ModuleCache.create
{ contentHash = moduleContentHash
, errors = errors
, inputContextHashes = inputContextHashes
Expand All @@ -5849,7 +5850,7 @@ getErrorsForModule : ProjectRuleCache projectContext -> String -> List (Error {}
getErrorsForModule cache filePath =
case Dict.get filePath cache.moduleContexts of
Just cacheEntry ->
Cache.errors cacheEntry
ModuleCache.errors cacheEntry

Nothing ->
[]
Expand Down

0 comments on commit 02f3a30

Please sign in to comment.