Skip to content

Commit

Permalink
Merge pull request #18 from StampyAI/dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ProducerMatt authored Jun 25, 2024
2 parents 9ac4916 + c703518 commit 4bfb8cd
Show file tree
Hide file tree
Showing 17 changed files with 595 additions and 101 deletions.
1 change: 1 addition & 0 deletions .credo.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
{Credo.Check.Refactor.CyclomaticComplexity, []},
{Credo.Check.Refactor.Nesting, []},
{Credo.Check.Refactor.NegatedConditionsWithElse, []},
{Credo.Check.Readability.ParenthesesOnZeroArityDefs, []}
]
}
}
Expand Down
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
end_of_line = lf
charset = utf-8

[{*.ex,*.exs}]
trim_trailing_whitespace = true
insert_final_newline = true
115 changes: 58 additions & 57 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,77 +4,78 @@
# documentation.

name: Elixir CI

env:
MIX_ENV: test

on:
push:
branches: [ "main" ]
branches: ["main"]
pull_request:
branches: [ "main" ]

branches: ["main"]
permissions:
actions: write
contents: read

jobs:
pre_job:
name: check for duplicate checkers
dupe_check:
name: check for duplicate tasks
# continue-on-error: true # Uncomment once integration is finished
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
should_skip: ${{ steps.skip_check.outputs.should_skip }}
steps:
- id: skip_check
uses: fkirc/skip-duplicate-actions@v5
with:
paths_ignore: '["**.md", "docs/**"]'
cancel_others: true

build:
needs: pre_job
if: needs.pre_job.outputs.should_skip != 'true'
name: Build and test
- id: skip_check
uses: fkirc/skip-duplicate-actions@v5
with:
paths_ignore: '["**.md", "docs/**"]'
cancel_others: true
run_checks:
name: Run checks
runs-on: ubuntu-latest
needs: dupe_check
if: needs.dupe_check.outputs.should_skip != 'true'
steps:
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- uses: DeterminateSystems/flake-checker-action@main
- name: Restore dependencies cache
uses: actions/cache@v4
with:
path: deps
key: ${{ runner.os }}-mix-${{ hashFiles('**/mix.lock') }}
restore-keys: ${{ runner.os }}-mix-
- name: install mix
run: nix develop -c mix local.hex
- name: Run flakes check
run: |
nix flake check
nix develop -c echo OK
- name: Install dependencies
run: nix develop -c mix deps.get
# - name: Run Credo
# run: mix credo --strict
- name: Retrieve PLT Cache
uses: actions/cache@v4
id: plt-cache
with:
path: priv/plts
key: ${{ runner.os }}-${{ matrix.otp }}-${{ matrix.elixir }}-plts-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.*')) }}-${{ hashFiles(format('{0}{1}', github.workspace, '/flake.*')) }}
- name: Create PLTs
if: steps.plt-cache.outputs.cache-hit != 'true'
run: |
mkdir -p priv/plts
nix develop -c mix dialyzer --plt
- name: Run dialyzer
run: nix develop -c mix dialyzer --no-check --halt-exit-status
- name: Check Formatting
run: nix develop -c mix format --check-formatted
- name: Run tests
run: nix develop -c mix test
# - name: Build docs
# run: mix docs
- uses: actions/checkout@v4
- uses: DeterminateSystems/nix-installer-action@main
- uses: DeterminateSystems/magic-nix-cache-action@main
- uses: DeterminateSystems/flake-checker-action@main
- name: Restore dependencies cache
id: deps-cache
uses: actions/cache@v4
with:
path: |
deps
_build
priv/plts
.nix-mix
.nix-hex
key: ${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/flake.*')) }}-${{ hashFiles(format('{0}{1}', github.workspace, '/mix.*')) }}
restore-keys: |
${{ runner.os }}-mix-${{ hashFiles(format('{0}{1}', github.workspace, '/flake.*')) }}
${{ runner.os }}-mix-
- name: Build dependencies
if: steps.deps-cache.outputs.cache-hit != 'true'
run: |
# nix flake check # BUG: https://github.com/cachix/git-hooks.nix/issues/466
mkdir -p priv/plts
nix develop -c mix local.hex --force
nix develop -c mix deps.get
nix develop -c mix dialyzer --plt
nix develop -c mix compile
- name: Run pre-commit checks
run: |
nix develop -c pre-commit run --show-diff-on-failure --hook-stage=pre-merge-commit --color=always --all-files
# # NOTE: the above runs on all files regardless of if they changed. What would be better would be checking only the files in a P.R. that changed.
# # However, my attempt below to do so raises an error saying "unknown revision or path 'main..dev'" and I don't know how to fix it.
# - name: Run pre-commit checks
# env:
# HEAD_REF: ${{ github.head_ref }}
# BASE_REF: ${{ github.base_ref }}
# run: |
# if [[ -n "$HEAD_REF" && -n "$BASE_REF" ]]; then
# git fetch --depth=1 origin "$BASE_REF"
# git fetch --depth=1 origin "$HEAD_REF"
# nix develop -c pre-commit run --show-diff-on-failure --color=always --to-ref "$BASE_REF" --from-ref "$HEAD_REF"
# else
# nix develop -c pre-commit run --show-diff-on-failure --hook-stage=pre-merge-commit --color=always --all-files
# fi
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ erl_crash.dump
/priv

/eflame

/.pre-commit-config.yaml
172 changes: 172 additions & 0 deletions bench/prefix_conflicts.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
alias Stampede, as: S
require Stampede.MsgReceived
require Aja

defmodule T do
require Plugin
require Aja

def check_prefixes_for_conflicts(prefixes) when is_list(prefixes) do
all_but_first = Enum.drop(prefixes, 1)
all_but_last = Enum.drop(prefixes, -1)

Enum.find_value(all_but_first, :no_conflict, fn
prefix_in_danger ->
Enum.find_value(all_but_last, false, fn
^prefix_in_danger ->
# we've caught up to ourselves
false

prefix_that_interrupts ->
{false_or_prefix, mangled} = S.split_prefix(prefix_in_danger, prefix_that_interrupts)

if false_or_prefix do
{:conflict, prefix_in_danger, prefix_that_interrupts, mangled}
else
false
end
end)
end)
end

@spec do_check_prefixes_for_conflicts(nonempty_list(binary())) ::
:no_conflict
| {:conflict, mangled_prefix :: binary(), prefix_responsible :: binary(),
how_it_was_mangled :: binary()}
def do_check_prefixes_for_conflicts([first | rest]) do
do_check_prefixes_for_conflicts(first, rest)
end

def do_check_prefixes_for_conflicts(_final_prefix, []),
do: :no_conflict

def do_check_prefixes_for_conflicts(prefix_that_interrupts, latter_prefixes) do
Enum.find_value(latter_prefixes, fn
prefix_in_danger ->
{false_or_prefix, mangled} = S.split_prefix(prefix_in_danger, prefix_that_interrupts)

if false_or_prefix do
{:conflict, prefix_in_danger, prefix_that_interrupts, mangled}
else
nil
end
end)
|> case do
nil ->
[h | t] = latter_prefixes
do_check_prefixes_for_conflicts(h, t)

otherwise ->
otherwise
end
end

def check_prefixes_for_conflicts_vec(prefixes) do
all_but_first = Aja.Vector.drop(prefixes, 1)
all_but_last = Aja.Vector.drop(prefixes, -1)

Aja.Enum.find_value(all_but_first, :no_conflict, fn
prefix_in_danger ->
Aja.Enum.find_value(all_but_last, false, fn
^prefix_in_danger ->
# we've caught up to ourselves
false

prefix_that_interrupts ->
{false_or_prefix, mangled} = S.split_prefix(prefix_in_danger, prefix_that_interrupts)

if false_or_prefix do
{:conflict, prefix_in_danger, prefix_that_interrupts, mangled}
else
false
end
end)
end)
end

@spec do_check_prefixes_for_conflicts_vec_2(%Aja.Vector{}) ::
:no_conflict
| {:conflict, mangled_prefix :: binary(), prefix_responsible :: binary(),
how_it_was_mangled :: binary()}
def do_check_prefixes_for_conflicts_vec_2(vector) do
first = Aja.Vector.first(vector)
rest = Aja.Vector.drop(vector, 1)
do_check_prefixes_for_conflicts_vec_2(first, rest)
end

def do_check_prefixes_for_conflicts_vec_2(v, prefix_that_interrupts_i) when Aja.vec_size(v),
do: :no_conflict

def do_check_prefixes_for_conflicts_vec_2(prefix_that_interrupts, latter_prefixes) do
Aja.Enum.find_value(latter_prefixes, fn
prefix_in_danger ->
{false_or_prefix, mangled} = S.split_prefix(prefix_in_danger, prefix_that_interrupts)

if false_or_prefix do
{:conflict, prefix_in_danger, prefix_that_interrupts, mangled}
else
nil
end
end)
|> case do
nil ->
h = Aja.Vector.first(latter_prefixes)
t = Aja.Vector.drop(latter_prefixes, 1)
do_check_prefixes_for_conflicts(h, t)

otherwise ->
otherwise
end
end

def check_prefixes_for_conflicts_nitpick([_h | []]), do: :no_conflict

def check_prefixes_for_conflicts_nitpick([prefix_that_interrupts | latter_prefixes]) do
Enum.find_value(latter_prefixes, fn
prefix_in_danger ->
{false_or_prefix, mangled} = S.split_prefix(prefix_in_danger, prefix_that_interrupts)

if false_or_prefix do
{:conflict, prefix_in_danger, prefix_that_interrupts, mangled}
else
nil
end
end)
|> case do
nil ->
check_prefixes_for_conflicts_nitpick(latter_prefixes)

otherwise ->
otherwise
end
end

def make_input(pref, number) do
for n <- 0..(number - 1) do
Enum.at(pref, Integer.mod(n, length(pref))) <> String.duplicate("x", 16)
end
end
end

bl = ["ac", "bc", "cc", "aca", "ad", "bd", "cc"]
blv = bl |> Aja.Vector.new()

suites = %{
"manual" => fn -> T.do_check_prefixes_for_conflicts(bl) end,
"manual nitpick" => fn -> T.check_prefixes_for_conflicts_nitpick(bl) end
# # slow
# "find_value" => fn -> T.check_prefixes_for_conflicts(bl) end,
# # Even slower
# "vectorized find_value" => fn -> T.check_prefixes_for_conflicts_vec(blv) end,
# "vectorized manual" => fn -> T.do_check_prefixes_for_conflicts_vec_2(blv) end,
}

Benchee.run(
suites,
time: 30,
memory_time: 5,
# profile_after: true,
# profile_after: :fprof
measure_function_call_overhead: true,
pre_check: true
)
Loading

0 comments on commit 4bfb8cd

Please sign in to comment.