Skip to content

Commit

Permalink
rollback NFP interning (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepeiborra authored Jul 3, 2021
1 parent 08899eb commit ab1aec7
Showing 1 changed file with 2 additions and 21 deletions.
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)

0 comments on commit ab1aec7

Please sign in to comment.