Skip to content

Commit d943452

Browse files
committed
add comments for orderedSet
1 parent 4c5bc35 commit d943452

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

ghcide/session-loader/Development/IDE/Session.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ incrementVersion state = modifyVar' (version state) succ
509509

510510
-- | Get files from the pending file set
511511
getPendingFiles :: SessionState -> IO (HashSet FilePath)
512-
getPendingFiles state = atomically $ Set.fromList <$> S.toUnOrderedList (pendingFiles state)
512+
getPendingFiles state = atomically $ S.toHashSet (pendingFiles state)
513513

514514
-- | Handle errors during session loading by recording file as having error and removing from pending
515515
handleSingleFileProcessingError' :: SessionState -> Maybe FilePath -> FilePath -> PackageSetupException -> SessionM ()

ghcide/session-loader/Development/IDE/Session/OrderedSet.hs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Control.Concurrent.STM (STM, TQueue, flushTQueue,
55
import Control.Concurrent.STM.TQueue (readTQueue, writeTQueue)
66
import Control.Monad (when)
77
import Data.Hashable (Hashable)
8+
import qualified Data.HashSet
89
import qualified Focus
910
import qualified ListT as LT
1011
import qualified StmContainers.Set as S
@@ -13,6 +14,14 @@ import StmContainers.Set (Set)
1314

1415
type OrderedSet a = (TQueue a, Set a)
1516

17+
-- | Insert an element into the ordered set.
18+
-- If the element is not already present, it is added to both the queue and set.
19+
-- If the element already exists, it is moved to the end of the queue to maintain
20+
-- most-recently-inserted ordering semantics.
21+
-- It take O(n), not very good.
22+
23+
-- Alternative: could preserve original position.
24+
-- I am not sure which one is better.
1625
insert :: Hashable a => a -> OrderedSet a -> STM ()
1726
insert a (que, s) = do
1827
(_, inserted) <- S.focus (Focus.testingIfInserts $ Focus.insert ()) a s
@@ -23,14 +32,16 @@ insert a (que, s) = do
2332
mapM_ (writeTQueue que) items
2433
return ()
2534
writeTQueue que a
26-
-- when que $ writeTQueue que a
2735

2836
newIO :: Hashable a => IO (OrderedSet a)
2937
newIO = do
3038
que <- newTQueueIO
3139
s <- S.newIO
3240
return (que, s)
3341

42+
-- | Read the first element from the queue.
43+
-- If an element is not in the set, it means it has been deleted,
44+
-- so we retry until we find a valid element that exists in the set.
3445
readQueue :: Hashable a => OrderedSet a -> STM a
3546
readQueue rs@(que, s) = do
3647
f <- readTQueue que
@@ -41,8 +52,11 @@ readQueue rs@(que, s) = do
4152
lookup :: Hashable a => a -> OrderedSet a -> STM Bool
4253
lookup a (_, s) = S.lookup a s
4354

55+
-- | Delete an element from the set.
56+
-- The queue is not modified directly; stale entries are filtered out lazily
57+
-- during reading operations (see 'readQueue').
4458
delete :: Hashable a => a -> OrderedSet a -> STM ()
4559
delete a (_, s) = S.delete a s
4660

47-
toUnOrderedList :: Hashable a => OrderedSet a -> STM [a]
48-
toUnOrderedList (_, s) = LT.toList $ S.listT s
61+
toHashSet :: Hashable a => OrderedSet a -> Data.HashSet a
62+
toHashSet (_, s) = TreeSet.fromList $ LT.toList $ S.listT s

0 commit comments

Comments
 (0)