Skip to content

Commit

Permalink
adapt Elm compiler for efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
Viir committed Jan 7, 2025
1 parent 4eda207 commit aef5d93
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 285 deletions.
36 changes: 19 additions & 17 deletions implement/example-apps/elm-editor/src/FNV1a.elm
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module FNV1a exposing (hash, hashWithSeed, initialSeed)
-}

import Bitwise as Bit
import Bitwise


{-| The initial seed represents the starting point of a hash.
Expand Down Expand Up @@ -40,13 +40,14 @@ This allows you to hash two, or more, strings in sequence without concatenating
-}
hashWithSeed : String -> Int -> Int
hashWithSeed str seed =
Bit.shiftRightZfBy 0 (String.foldl utf32ToUtf8 seed str)
Bitwise.shiftRightZfBy 0 (String.foldl utf32ToUtf8 seed str)


utf32ToUtf8 : Char -> Int -> Int
utf32ToUtf8 char acc =
{- Implementation copied from: https://github.com/zwilias/elm-utf-tools/tree/2.0.1 -}
let
byte : Int
byte =
Char.toCode char
in
Expand All @@ -55,33 +56,34 @@ utf32ToUtf8 char acc =

else if byte < 0x0800 then
acc
|> hasher (Bit.or 0xC0 <| Bit.shiftRightZfBy 6 byte)
|> hasher (Bit.or 0x80 <| Bit.and 0x3F byte)
|> hasher (Bitwise.or 0xC0 <| Bitwise.shiftRightZfBy 6 byte)
|> hasher (Bitwise.or 0x80 <| Bitwise.and 0x3F byte)

else if byte < 0x00010000 then
acc
|> hasher (Bit.or 0xE0 <| Bit.shiftRightZfBy 12 byte)
|> hasher (Bit.or 0x80 <| Bit.and 0x3F <| Bit.shiftRightZfBy 6 byte)
|> hasher (Bit.or 0x80 <| Bit.and 0x3F byte)
|> hasher (Bitwise.or 0xE0 <| Bitwise.shiftRightZfBy 12 byte)
|> hasher (Bitwise.or 0x80 <| Bitwise.and 0x3F <| Bitwise.shiftRightZfBy 6 byte)
|> hasher (Bitwise.or 0x80 <| Bitwise.and 0x3F byte)

else
acc
|> hasher (Bit.or 0xF0 <| Bit.shiftRightZfBy 18 byte)
|> hasher (Bit.or 0x80 <| Bit.and 0x3F <| Bit.shiftRightZfBy 12 byte)
|> hasher (Bit.or 0x80 <| Bit.and 0x3F <| Bit.shiftRightZfBy 6 byte)
|> hasher (Bit.or 0x80 <| Bit.and 0x3F byte)
|> hasher (Bitwise.or 0xF0 <| Bitwise.shiftRightZfBy 18 byte)
|> hasher (Bitwise.or 0x80 <| Bitwise.and 0x3F <| Bitwise.shiftRightZfBy 12 byte)
|> hasher (Bitwise.or 0x80 <| Bitwise.and 0x3F <| Bitwise.shiftRightZfBy 6 byte)
|> hasher (Bitwise.or 0x80 <| Bitwise.and 0x3F byte)


hasher : Int -> Int -> Int
hasher byte hashValue =
{- Implementation ported from: https://gist.github.com/vaiorabbit/5657561 -}
let
mixed : Int
mixed =
Bit.xor byte hashValue
Bitwise.xor byte hashValue
in
mixed
+ Bit.shiftLeftBy 1 mixed
+ Bit.shiftLeftBy 4 mixed
+ Bit.shiftLeftBy 7 mixed
+ Bit.shiftLeftBy 8 mixed
+ Bit.shiftLeftBy 24 mixed
+ Bitwise.shiftLeftBy 1 mixed
+ Bitwise.shiftLeftBy 4 mixed
+ Bitwise.shiftLeftBy 7 mixed
+ Bitwise.shiftLeftBy 8 mixed
+ Bitwise.shiftLeftBy 24 mixed
52 changes: 27 additions & 25 deletions implement/pine/Elm/elm-compiler/src/CompileElmApp.elm
Original file line number Diff line number Diff line change
Expand Up @@ -4027,20 +4027,24 @@ findFileTreeNodeWithPathMatchingRepresentationInFunctionName sourceDirs sourceFi
nodesWithRepresentations =
sourceDirectoryPaths
|> List.concatMap
(\elmJsonDirectoryPath ->
FileTree.getNodeAtPathFromFileTree elmJsonDirectoryPath fileTree
|> Maybe.withDefault (FileTree.TreeNode [])
|> FileTree.listNodesWithPath
|> List.map
(\( nodePathRelative, node ) ->
( filePathRepresentationInFunctionName nodePathRelative
, ( { relativePath = nodePathRelative
, absolutePath = elmJsonDirectoryPath ++ nodePathRelative
}
, node
)
)
)
(\sourceDirPath ->
case FileTree.getNodeAtPathFromFileTree sourceDirPath fileTree of
Nothing ->
[]

Just sourceDirNode ->
sourceDirNode
|> FileTree.listNodesWithPath
|> List.map
(\( nodePathRelative, node ) ->
( filePathRepresentationInFunctionName nodePathRelative
, ( { relativePath = nodePathRelative
, absolutePath = List.concat [ sourceDirPath, nodePathRelative ]
}
, node
)
)
)
)

nodesGroupedByRepresentation : Dict.Dict String (List ( DeclarationFileMatch, FileTree.FileTreeNode Bytes.Bytes ))
Expand Down Expand Up @@ -4782,18 +4786,16 @@ enumerateLeavesFromRecordTree node =


filePathRepresentationInFunctionName : List String -> String
filePathRepresentationInFunctionName =
String.join "/"
>> String.toList
>> List.map
(\char ->
if Char.isAlphaNum char then
char
filePathRepresentationInFunctionName pathItems =
String.map
(\char ->
if Char.isAlphaNum char then
char

else
'_'
)
>> String.fromList
else
'_'
)
(String.join "_" pathItems)


addModulesFromTextToAppFiles : SourceDirectories -> List String -> AppFiles -> AppFiles
Expand Down
Loading

0 comments on commit aef5d93

Please sign in to comment.