Skip to content

Commit

Permalink
Merge pull request #17 from lamdera/optimize-legible
Browse files Browse the repository at this point in the history
Add --optimize-legible compilation mode
  • Loading branch information
supermario authored Apr 9, 2024
2 parents 523487d + ac50b48 commit 9a37020
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 4 deletions.
7 changes: 7 additions & 0 deletions compiler/src/Generate/Mode.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import qualified Elm.Compiler.Type.Extract as Extract
import qualified Generate.JavaScript.Name as JsName


import Lamdera ((&))
import qualified Lamdera

-- MODE

Expand Down Expand Up @@ -55,6 +57,11 @@ addToBuckets field frequency buckets =
addToShortNames :: [Name.Name] -> ShortFieldNames -> ShortFieldNames
addToShortNames fields shortNames =
List.foldl' addField shortNames fields
& Lamdera.alternativeImplementationWhen (Lamdera.isLongNamesEnabled_)
(List.foldl' (\shortNames field ->
Map.insert field (JsName.fromLocal field) shortNames
) shortNames fields
)


addField :: ShortFieldNames -> Name.Name -> ShortFieldNames
Expand Down
22 changes: 22 additions & 0 deletions extra/Lamdera.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ module Lamdera
, disableWire
, isWireEnabled
, isWireEnabled_
, useLongNames_
, enableLongNames
, isLongNamesEnabled
, isLongNamesEnabled_
, isTest
, isLiveMode
, setLiveMode
Expand Down Expand Up @@ -431,6 +435,24 @@ isWireEnabled_ :: Bool
isWireEnabled_ = unsafePerformIO $ isWireEnabled


{-# NOINLINE useLongNames_ #-}
useLongNames_ :: MVar Bool
useLongNames_ = unsafePerformIO $ newMVar False

enableLongNames :: IO ()
enableLongNames = do
debug $ "🗜️ enableLongNames"
modifyMVar_ useLongNames_ (\_ -> pure True)

{-# NOINLINE isLongNamesEnabled #-}
isLongNamesEnabled :: IO Bool
isLongNamesEnabled = do
readMVar useLongNames_

{-# NOINLINE isLongNamesEnabled_ #-}
isLongNamesEnabled_ :: Bool
isLongNamesEnabled_ = unsafePerformIO $ isLongNamesEnabled


isTest :: IO Bool
isTest = do
Expand Down
3 changes: 3 additions & 0 deletions extra/Lamdera/CLI/Check.hs
Original file line number Diff line number Diff line change
Expand Up @@ -590,6 +590,7 @@ buildProductionJsFiles root inProduction_ versionInfo = do
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}

Make.run ["src" </> "LFR.elm"] $
Expand All @@ -600,6 +601,7 @@ buildProductionJsFiles root inProduction_ versionInfo = do
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}

Lamdera.AppConfig.writeUsage
Expand Down Expand Up @@ -700,6 +702,7 @@ migrationCheck root nextVersion = do
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}

-- @TODO this is because the migrationCheck does weird terminal stuff that mangles the display... how to fix this?
Expand Down
4 changes: 4 additions & 0 deletions extra/Lamdera/Compile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ makeOptimizedWithCleanup cleanup root path = do
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}
wait r
remove tmp
Expand All @@ -67,6 +68,7 @@ make_ root = do
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
Expand Down Expand Up @@ -95,6 +97,7 @@ makeDev root paths = do
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}
wait r
-- The compilation process ends by printing to terminal in a way that overwrites
Expand Down Expand Up @@ -130,6 +133,7 @@ makeHarnessDevJs root = do
, _report = Nothing
, _docs = Nothing
, _noWire = False
, _optimizeLegible = False
}
wait r
remove tmp
Expand Down
1 change: 1 addition & 0 deletions terminal/src/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ make =
|-- flag "report" Make.reportType "You can say --report=json to get error messages as JSON. This is only really useful if you are an editor plugin. Humans should avoid it!"
|-- flag "docs" Make.docsFile "Generate a JSON file of documentation for a package. Eventually it will be possible to preview docs with `reactor` because it is quite hard to deal with these JSON files directly."
|-- onOff "no-wire" "Explicitly disable Lamdera's wire codegen."
|-- onOff "optimize-legible" "Same as --optimize but without identifier shortening, handy for debugging optimised code or for when identifiers are more useful than smaller JS compilations."
in
Terminal.Command "make" Uncommon details example (zeroOrMore elmFile) makeFlags Make.run

Expand Down
10 changes: 6 additions & 4 deletions terminal/src/Make.hs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ data Flags =
, _report :: Maybe ReportType
, _docs :: Maybe FilePath
, _noWire :: Bool -- @LAMDERA
, _optimizeLegible :: Bool -- @LAMDERA
}


Expand All @@ -68,21 +69,22 @@ type Task a = Task.Task Exit.Make a


run :: [FilePath] -> Flags -> IO ()
run paths flags@(Flags _ _ _ report _ noWire) =
run paths flags@(Flags _ _ _ report _ noWire optimizeLegible) =
do style <- getStyle report
maybeRoot <- Stuff.findRoot
Lamdera.onlyWhen noWire Lamdera.disableWire
Lamdera.onlyWhen optimizeLegible Lamdera.enableLongNames
Reporting.attemptWithStyle style Exit.makeToReport $
case maybeRoot of
Just root -> runHelp root paths style flags
Nothing -> return $ Left $ Exit.MakeNoOutline


runHelp :: FilePath -> [FilePath] -> Reporting.Style -> Flags -> IO (Either Exit.Make ())
runHelp root paths style (Flags debug optimize maybeOutput _ maybeDocs _) =
runHelp root paths style (Flags debug optimize maybeOutput _ maybeDocs _ optimizeLegible) =
BW.withScope $ \scope ->
Stuff.withRootLock root $ Task.run $
do desiredMode <- getMode debug optimize
do desiredMode <- getMode debug (optimize || optimizeLegible)
details <- Task.eio Exit.MakeBadDetails (Details.load style scope root)
case paths of
[] ->
Expand Down Expand Up @@ -332,7 +334,7 @@ isDevNull name =

-- Clone of run that uses attemptWithStyle_cleanup
run_cleanup :: IO () -> [FilePath] -> Flags -> IO ()
run_cleanup cleanup paths flags@(Flags _ _ _ report _ _) =
run_cleanup cleanup paths flags@(Flags _ _ _ report _ _ _) =
do style <- getStyle report
maybeRoot <- Stuff.findRoot
Reporting.attemptWithStyle_cleanup cleanup style Exit.makeToReport $
Expand Down

0 comments on commit 9a37020

Please sign in to comment.