Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
jesperes committed Jan 24, 2024
1 parent 188520e commit 1820a26
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.rebar3
_build
*.beam
/erl_crash.dump
5 changes: 5 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
#!/bin/bash

INPUT=$1
if [ -z "$INPUT" ]; then
INPUT="measurements.txt"
fi

erlc src/erlang_1brc.erl
/bin/time -f "Elapsed time: %E" \
erl \
Expand Down
32 changes: 23 additions & 9 deletions src/erlang_1brc.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,30 @@ main(Args) ->
do_main(Opts) ->
run(proplists:get_value(file, Opts)).

run([Filename]) ->
run(Filename);
run(Filename) when is_atom(Filename) ->
run(atom_to_list(Filename));
run(Filename) ->
process_flag(trap_exit, true),
{ok, FD} = file:open(Filename, [raw, read, binary]),
NumProcessors = erlang:system_info(logical_processors),
{ProcessorPids, AllPids} = start_processors(NumProcessors),
{ok, Bin} = file:pread(FD, 0, ?BUFSIZE),
read_chunks(FD, 0, byte_size(Bin), Bin, ?BUFSIZE, ProcessorPids, NumProcessors * 3),
Map = wait_for_completion(AllPids, #{}),
Fmt = format_final_map(Map),
io:format("~ts~n", [Fmt]).
try
process_flag(trap_exit, true),
case file:open(Filename, [raw, read, binary]) of
{ok, FD} ->
NumProcessors = erlang:system_info(logical_processors),
{ProcessorPids, AllPids} = start_processors(NumProcessors),
{ok, Bin} = file:pread(FD, 0, ?BUFSIZE),
read_chunks(FD, 0, byte_size(Bin), Bin, ?BUFSIZE, ProcessorPids, NumProcessors * 3),
Map = wait_for_completion(AllPids, #{}),
Fmt = format_final_map(Map),
io:format("~ts~n", [Fmt]);
{error, Reason} ->
io:format("*** Failed to open ~ts: ~p~n", [Filename, Reason]),
erlang:halt(1)
end
catch Class:Error:Stacktrace ->
io:format("*** Caught exception: ~p~n", [{Class, Error, Stacktrace}]),
erlang:halt(1)
end.

start_processors(NumProcs) ->
lists:foldl(
Expand Down

0 comments on commit 1820a26

Please sign in to comment.