Skip to content

Commit 0ac68c8

Browse files
Optimization work. (#163)
1 parent b091c32 commit 0ac68c8

6 files changed

Lines changed: 8 additions & 41 deletions

File tree

Decoder.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mp3Tool = "mpg123"
2121
------------------------------------------------------------------------
2222
-- Send commands to mpg123
2323

24-
data Cmd = Load ByteString | Jump (Fixed E2) | Pause | Quit
24+
data Cmd = Load !ByteString | Jump !(Fixed E2) | Pause | Quit
2525

2626
cmdToBS :: Cmd -> ByteString
2727
cmdToBS (Load f) = "L " <> f
@@ -32,10 +32,7 @@ cmdToBS Quit = "Q"
3232
------------------------------------------------------------------------
3333
-- Receive messages from mpg123
3434

35-
data Msg = I !Id3
36-
| S {-# UNPACK #-} !ByteString
37-
| F {-# UNPACK #-} !Frame
38-
| P !Status
35+
data Msg = I !Id3 | S !ByteString | F !Frame | P !Status
3936
deriving stock (Eq, Show)
4037

4138
-- ID3 info

State.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ data HState = HState
2828
, folders :: !DirArray
2929
, bootTime :: !TimeSpec
3030
, configPath :: !(Maybe FilePath) -- style.conf override (CLI)
31-
, histSize :: Int
31+
, histSize :: !Int
3232
-- These can
3333
, current :: !Int -- currently playing mp3
3434
, cursor :: !Int -- mp3 under the cursor
@@ -41,7 +41,7 @@ data HState = HState
4141
, minibuffer :: !Line -- contents of minibuffer
4242
, modal :: !(Maybe Modal) -- modal visible
4343
, miniFocused :: !Bool -- is the mini buffer focused?
44-
, folderCol :: Float -- portion of width for folders
44+
, folderCol :: !Float -- portion of width for folders
4545
, mode :: !Mode
4646
, uptime :: !ByteString
4747
, searchFw :: !Bool -- active search direction

Style.hs

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
-- Copyright (c) 2019-2022, 2026 Galen Huntington
33
-- SPDX-License-Identifier: GPL-2.0-or-later
44

5-
--
65
-- | Color manipulation
7-
--
86

97
module Style where
108

@@ -48,17 +46,15 @@ data Style = Style !Color !Color
4846
deriving stock (Eq,Ord)
4947

5048
-- | A styled UTF-8 ByteString segment.
51-
data Segment = Seg {-# UNPACK #-} !Style {-# UNPACK #-} !ByteString
49+
data Segment = Seg !Style {-# UNPACK #-} !ByteString
5250

5351
-- | A line of segments.
5452
type Line = [Segment]
5553

5654
------------------------------------------------------------------------
57-
--
5855
-- | Named colors for the config file and the built-in styles. The
5956
-- \"dark\" name of each pair is the normal-intensity hue; the plain name
6057
-- is its bright variant (so @red@ is bright, @darkred@ is normal).
61-
--
6258
stringToColor :: String -> Maybe Color
6359
stringToColor s = case map toLower s of
6460
"black" -> Just $ Color Normal Black
@@ -82,31 +78,21 @@ stringToColor s = case map toLower s of
8278
_ -> Nothing
8379

8480
------------------------------------------------------------------------
85-
--
8681
-- | Set some colours, perform an action, and then reset the colours
87-
--
8882
withStyle :: Style -> IO () -> IO ()
8983
withStyle sty fn = uiAttr sty >>= setAttribute >> fn >> reset
9084
{-# INLINE withStyle #-}
9185

92-
--
9386
-- | manipulate the current attributes of the standard screen
9487
-- Only set attr if it's different to the current one?
95-
--
9688
setAttribute :: (Curses.Attr, Curses.Pair) -> IO ()
9789
setAttribute = uncurry Curses.attrSet
98-
{-# INLINE setAttribute #-}
9990

100-
--
10191
-- | Reset the screen to normal values
102-
--
10392
reset :: IO ()
10493
reset = setAttribute (Curses.attr0, Curses.Pair 0)
105-
{-# INLINE reset #-}
10694

107-
--
10895
-- | And turn on the colours
109-
--
11096
initcolours :: UIStyle -> IO ()
11197
initcolours sty = do
11298
let ls = [sty.modals, sty.warnings, sty.window,
@@ -119,7 +105,6 @@ initcolours sty = do
119105
uiAttr sty.window >>= \(_,p) -> Curses.bkgrndSet nullA p
120106

121107
------------------------------------------------------------------------
122-
--
123108
-- | Set up the ui attributes, given a ui style record
124109
--
125110
-- Returns an association list of pairs for foreground and bg colors,
@@ -138,24 +123,20 @@ initUiColors stys = do
138123
pure (sty, (a `Curses.attrPlus` b, Curses.Pair p))
139124

140125
------------------------------------------------------------------------
141-
--
142126
-- | Getting from nice abstract colours to ncurses-settable values
143127

144128
-- 20% of allocss occur here! But there's only 3 or 4 colours :/
145129
-- Every call to uiAttr
146-
--
147130
uiAttr :: Style -> IO (Curses.Attr, Curses.Pair)
148131
uiAttr sty = do
149132
m <- readIORef pairMap
150133
pure $ lookupPair m sty
151-
{-# INLINE uiAttr #-}
152134

153135
-- | Given a curses color pair, find the Curses.Pair (i.e. the pair
154136
-- curses thinks these colors map to) from the state
155137
lookupPair :: PairMap -> Style -> (Curses.Attr, Curses.Pair)
156138
lookupPair m s =
157139
fromMaybe (Curses.attr0, Curses.Pair 0) (M.lookup s m)
158-
{-# INLINE lookupPair #-}
159140

160141
-- | Keep a map of nice style defs to underlying curses pairs, created at init time
161142
type PairMap = M.Map Style (Curses.Attr, Curses.Pair)
@@ -166,22 +147,17 @@ pairMap = unsafePerformIO $ newIORef M.empty
166147
{-# NOINLINE pairMap #-}
167148

168149
------------------------------------------------------------------------
169-
--
170150
-- Basic (ncurses) colours.
171-
--
151+
172152
defaultColor :: Curses.Color
173153
defaultColor = fromJust $ Curses.color "default"
174154

175-
--
176155
-- Combine attribute with another attribute
177-
--
178156
setBoldA, setReverseA :: Curses.Attr -> Curses.Attr
179157
setBoldA = flip Curses.setBold True
180158
setReverseA = flip Curses.setReverse True
181159

182-
--
183160
-- | Some attribute constants
184-
--
185161
boldA, nullA, reverseA :: Curses.Attr
186162
nullA = Curses.attr0
187163
boldA = setBoldA nullA
@@ -194,7 +170,6 @@ newtype CColor = CColor (Curses.Attr, Curses.Color)
194170
-- | Map an abstract 'Style' to its ncurses foreground/background pair.
195171
style2curses :: Style -> (CColor, CColor)
196172
style2curses (Style fg bg) = (fgCursCol fg, bgCursCol bg)
197-
{-# INLINE style2curses #-}
198173

199174
-- | The ncurses color for each ANSI hue.
200175
hueColor :: Hue -> Curses.Color
@@ -226,7 +201,6 @@ plainSeg :: ByteString -> Segment
226201
plainSeg = Seg defaultSty
227202

228203
------------------------------------------------------------------------
229-
--
230204
-- Support for runtime configuration
231205
-- We choose a simple strategy, read/showable record types, with strings
232206
-- to represent colors
@@ -260,7 +234,6 @@ buildStyle bs = UIStyle {
260234
, blockcursor = f bs.hmp3_blockcursor
261235
, progress = f bs.hmp3_progress
262236
}
263-
264237
where
265238
f (x,y) = Style (g x) (g y)
266239
g x = fromMaybe Default $ stringToColor x

UI.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ refreshClock = runDraw $ redrawJustClock <> Draw Curses.refresh
140140

141141
------------------------------------------------------------------------
142142

143-
data DrawData = DD { drawWidth :: Int, drawState :: HState }
143+
data DrawData = DD { drawWidth :: !Int, drawState :: !HState }
144144

145145
------------------------------------------------------------------------
146146

@@ -339,7 +339,6 @@ slice :: Int -> Int -> Array Int e -> [e]
339339
slice i j arr =
340340
let (a, b) = bounds arr
341341
in [unsafeAt arr n | n <- [max a i .. min b j]]
342-
{-# INLINE slice #-}
343342

344343
------------------------------------------------------------------------
345344

hmp3-ng.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ common opts
3939
ghc-options:
4040
-Wall
4141
-Wprepositive-qualified-module
42-
-funbox-strict-fields
4342

4443
library
4544
import: opts

test/TextSpec.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module TextSpec (tests) where
33
import Test.Tasty
44
import Test.Tasty.HUnit
55

6-
import Base ((<&>))
76
import Text
87

98
-- These tests depend on wcwidth's behavior under a UTF-8 locale and on a
@@ -91,5 +90,5 @@ tests = testGroup "Text"
9190

9291

9392
m :: Maybe Bool -> String -> String -> String -> TestTree
94-
m b tag pat str = testCase tag $ (matches (u pat) <&> ($ u str)) @?= b
93+
m b tag pat str = testCase tag $ ($ u str) <$> matches (u pat) @?= b
9594

0 commit comments

Comments
 (0)