Stream: beginners

Topic: ✔ --optimize bug


view this post on Zulip Adrian (May 01 2024 at 23:59):

Sorry to be back so soon with another major problem, but turning --optimize on seems to change the result of a roc program

$ roc run dn08.roc
3
$ roc run --optimize dn08.roc
0
app "dn08"
    packages {
        pf: "https://github.com/roc-lang/basic-cli/releases/download/0.10.0/vNe6s9hWzoTZtFmNkvEICPErI9ptji_ySjicO6CkucY.tar.br",
    }
    imports [
        pf.Stdout,
        pf.Task,
    ]
    provides [main] to pf

main =
    arr = [14, 12, 19, 16, 18, 11, 15, 10, 13, 17]
    n = List.len arr
    k = 5
    r = 3
    goal = List.sortAsc arr
    List.range { start: At 0, end: At k }
    |> List.map
        \e -> List.range { start: At 0, end: Before e }
            |> List.map \_ -> allSwaps n r
    |> List.joinMap product
    |> List.map \e -> eval arr e r
    |> List.countIf \e -> e == goal
    |> Num.toStr
    |> Stdout.line

eval = \init, prog, r ->
    List.walk prog init \state, elem ->
        when elem is
            Swap a b ->
                List.range { start: At 0, end: Before r }
                |> List.walk state \arr, offset ->
                    List.swap arr (a + offset) (b + offset)

allSwaps = \n, r ->
    List.range { start: At 0, end: At (n - r) }
    |> List.map \e ->
        List.range { start: After e, end: At (n - r) }
        |> List.map \e2 -> Swap e e2
    |> List.join
    |> List.keepIf \e -> e |> isValidSwap r

isValidSwap = \swap, r ->
    when swap is
        Swap a b ->
            a + r <= b

product : List (List a) -> List (List a)
product = \l ->
    first = l |> List.first |> Result.withDefault [] |> List.map List.single
    l
    |> List.dropFirst 1
    |> List.walk first \state, elem ->
        List.joinMap state \a ->
            List.map elem \b ->
                List.append a b

view this post on Zulip Brendan Hansknecht (May 02 2024 at 00:00):

Probably another variant of https://github.com/roc-lang/roc/issues/6434

view this post on Zulip Brendan Hansknecht (May 02 2024 at 00:01):

Specifically a variant of https://github.com/roc-lang/roc/issues/6139

view this post on Zulip Adrian (May 02 2024 at 00:02):

Seems likely

view this post on Zulip Adrian (May 02 2024 at 00:04):

Sorry, for not noticing it, tho it was quite jarring to notice a 3 suddenly turn to a 0

view this post on Zulip Adrian (May 02 2024 at 00:05):

Might i also use this moment of attention to ask about why diff <(roc run dn08.roc) <(roc run --optimize dn08.roc) doesn't work? Does roc change any files on the file system or why would running two compilers at the same time pose a problem?

view this post on Zulip Brendan Hansknecht (May 02 2024 at 00:08):

Both of those commands will generate the same executable file ./dn08. They also will both download and build from the same platform which could have race conditions.

view this post on Zulip Brendan Hansknecht (May 02 2024 at 00:09):

Definitely should work though.

view this post on Zulip Richard Feldman (May 02 2024 at 00:12):

Brendan Hansknecht said:

They also will both download and build from the same platform which could have race conditions.

I took some steps to make this work (if I remember right, it downloads to a tempdir, then renames the file to its destination and ignores "already exists" errors) but the race condition around the output binary is definitely possible :big_smile:

view this post on Zulip Brendan Hansknecht (May 02 2024 at 00:41):

Oh cool

view this post on Zulip Brendan Hansknecht (May 02 2024 at 00:41):

So just the output binary then (and maybe some surgical linker copied files)

view this post on Zulip Adrian (May 02 2024 at 00:43):

For some reason I thought run ran directly

view this post on Zulip Adrian (May 02 2024 at 00:44):

closing, as the original issue is already known. Thanks for the help

view this post on Zulip Notification Bot (May 02 2024 at 00:44):

Adrian has marked this topic as resolved.

view this post on Zulip Brendan Hansknecht (May 02 2024 at 01:52):

For some reason I thought run ran directly

It should

view this post on Zulip Brendan Hansknecht (May 02 2024 at 01:52):

Just doesn't currently

view this post on Zulip Brendan Hansknecht (May 02 2024 at 01:53):

Or at a minimum, it should build into a cache/tmp/hidden folder such that you don't get these kinds of collisions


Last updated: Jul 26 2025 at 12:14 UTC