Repository navigation
perf: memoize env lookups across top-level forms #1599
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
eriksvedang
wants to merge
5
commits into
master
Choose a base branch
from
avoid-billions-of-allocations
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8329fab
optimization: reuse envs & compare by binding path
eriksvedang 2e84308
performance: look up blit/delete implementations once per memory pass
eriksvedang 4649ac4
chore: ignore .stack-work-prof dir
eriksvedang cd93e12
much optimization
eriksvedang 1f718e4
Merge branch 'master' into avoid-billions-of-allocations
eriksvedang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| *.output.actual | ||
|
|
||
| .stack-work/ | ||
| .stack-work-prof/ | ||
|
|
||
| /CarpHask-exe.prof | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,6 +45,7 @@ library | |
| Interfaces, | ||
| Managed, | ||
| Map, | ||
| Memo, | ||
| Memory, | ||
| Meta, | ||
| Obj, | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| -- | Memoization on the identity of immutable values. | ||
| -- | ||
| -- The compiler repeatedly derives the same data from the same environment: | ||
| -- an environment only changes between top-level forms, but lookups against | ||
| -- it happen many thousands of times per form. Threading caches through all | ||
| -- of those call sites is impractical, so these helpers remember the results | ||
| -- for the most recently seen arguments instead. | ||
| -- | ||
| -- This is safe because the arguments are immutable: the same heap object | ||
| -- always gives the same result. A different object, even an equal one, is | ||
| -- simply a cache miss and the result is computed as usual. | ||
| module Memo | ||
| ( MemoCache, | ||
| newMemoCache, | ||
| memoOnIdentity, | ||
| memoOnIdentity2, | ||
| ) | ||
| where | ||
|
|
||
| import Control.Exception (evaluate) | ||
| import Data.IORef | ||
| import System.IO.Unsafe (unsafePerformIO) | ||
| import System.Mem.StableName | ||
|
|
||
| -- | Remembers the results for the most recently seen keys, newest first. | ||
| newtype MemoCache k v = MemoCache (IORef [(k, v)]) | ||
|
|
||
| -- | How many results a cache remembers. Lookups often alternate between a | ||
| -- few environments (e.g. a local one and the global one), so one isn't enough. | ||
| cacheSize :: Int | ||
| cacheSize = 8 | ||
|
|
||
| -- | Create a cache. Bind each one to a top-level NOINLINE value. | ||
| newMemoCache :: IO (MemoCache k v) | ||
| newMemoCache = MemoCache <$> newIORef [] | ||
|
|
||
| lookupOrCompute :: Eq k => MemoCache k v -> k -> v -> IO v | ||
| lookupOrCompute (MemoCache ref) key result = do | ||
| entries <- readIORef ref | ||
| case lookup key entries of | ||
| Just cached -> pure cached | ||
| Nothing -> do | ||
| writeIORef ref (take cacheSize ((key, result) : entries)) | ||
| pure result | ||
|
|
||
| -- | @memoOnIdentity cache f x@ is @f x@, reusing an earlier result when @x@ | ||
| -- is the same object as a recently seen argument. | ||
| memoOnIdentity :: MemoCache (StableName a) v -> (a -> v) -> a -> v | ||
| memoOnIdentity cache f x = unsafePerformIO $ do | ||
| x' <- evaluate x -- The stable names of a thunk and of its value differ. | ||
| name <- makeStableName x' | ||
| lookupOrCompute cache name (f x') | ||
| {-# NOINLINE memoOnIdentity #-} | ||
|
|
||
| -- | Like 'memoOnIdentity', for a function of two arguments. | ||
| memoOnIdentity2 :: MemoCache (StableName a, StableName b) v -> (a -> b -> v) -> a -> b -> v | ||
| memoOnIdentity2 cache f x y = unsafePerformIO $ do | ||
| x' <- evaluate x | ||
| y' <- evaluate y | ||
| nameX <- makeStableName x' | ||
| nameY <- makeStableName y' | ||
| lookupOrCompute cache (nameX, nameY) (f x' y') | ||
| {-# NOINLINE memoOnIdentity2 #-} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think this means i cannot override deleters anymore if i already had an instance. concretely, the new check skips an existing specialization without checking whether its source changed.
i specced out a repro with codex: