Skip to content
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

Rollback NFP interning #344

Merged
merged 1 commit into from
Jul 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 2 additions & 21 deletions lsp-types/src/Language/LSP/Types/Uri.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@ import Control.DeepSeq
import qualified Data.Aeson as A
import Data.Binary (Binary, Get, put, get)
import Data.Hashable
import qualified Data.HashMap.Strict as HM
import Data.IORef (atomicModifyIORef', newIORef)
import Data.List (stripPrefix)
import Data.String (IsString, fromString)
import Data.Text (Text)
import qualified Data.Text as T
import Data.Tuple (swap)
import GHC.Generics
import Network.URI hiding (authority)
import qualified System.FilePath as FP
import qualified System.FilePath.Posix as FPP
import qualified System.FilePath.Windows as FPW
import qualified System.Info
import System.IO.Unsafe (unsafePerformIO)

newtype Uri = Uri { getUri :: Text }
deriving (Eq,Ord,Read,Show,Generic,A.FromJSON,A.ToJSON,Hashable,A.ToJSONKey,A.FromJSONKey)
Expand Down Expand Up @@ -175,7 +171,7 @@ instance Binary NormalizedFilePath where

-- | A smart constructor that performs UTF-8 encoding and hash consing
normalizedFilePath :: NormalizedUri -> FilePath -> NormalizedFilePath
normalizedFilePath nuri nfp = intern $ NormalizedFilePath nuri nfp
normalizedFilePath nuri nfp = NormalizedFilePath nuri nfp

-- | Internal helper that takes a file path that is assumed to
-- already be normalized to a URI. It is up to the caller
Expand Down Expand Up @@ -211,19 +207,4 @@ normalizedFilePathToUri (NormalizedFilePath uri _) = uri

uriToNormalizedFilePath :: NormalizedUri -> Maybe NormalizedFilePath
uriToNormalizedFilePath nuri = fmap (normalizedFilePath nuri) mbFilePath
where mbFilePath = platformAwareUriToFilePath System.Info.os (fromNormalizedUri nuri)

---------------------------------------------------------------------------
-- Unsafe hashcons of NFP
internIO :: (Eq a, Hashable a) => IO (a -> IO a)
internIO = do
tableRef <- newIORef mempty
let f x = atomicModifyIORef' tableRef $ swap . flip HM.alterF x (\case
Just res -> (res, Just res)
Nothing -> (x, Just x)
)
return f

{-# NOINLINE intern #-}
intern :: NormalizedFilePath -> NormalizedFilePath
intern = let f = unsafePerformIO internIO in \x -> unsafePerformIO (f x)
where mbFilePath = platformAwareUriToFilePath System.Info.os (fromNormalizedUri nuri)