Skip to content

Commit 6fe1b69

Browse files
committed
Use cmake when available otherwise default to -O2
1 parent 3247a6f commit 6fe1b69

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ compile_nif: get_deps
99
clean_nif:
1010
@make -C c_src clean
1111

12+
clean_zstd: clean_nif
13+
@make -C _build/deps/zstd clean
14+
@cd _build/deps/zstd && git reset --hard
15+
1216
compile:
1317
${REBAR} compile
1418

bench.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
set -e
3+
4+
RunBench()
5+
{
6+
if [ -z "$1" ]; then
7+
echo "Compiling with CMAKE"
8+
export CFLAGS=
9+
export NO_CMAKE=
10+
else
11+
echo "Compiling with CFLAGS=$1 and NO_CMAKE=1"
12+
export CFLAGS=$1
13+
export NO_CMAKE=1
14+
fi
15+
make clean_zstd >> /dev/null
16+
rebar3 compile >> /dev/null
17+
rebar3 shell --script scripts/bench_compress.erl | grep -oE "AVG Time: .*"
18+
}
19+
20+
RunBench "-g"
21+
RunBench "-O1"
22+
RunBench "-O2"
23+
RunBench "-O2 -march=native -mtune=native"
24+
RunBench
25+
26+
# Benchmark results:
27+
# Operating System: Linux
28+
# CPU Information: AMD Ryzen 9 6900HS with Radeon Graphics
29+
# Erlang 26.2.5.8
30+
#
31+
# Based on these decided to default to CFLAGS=-O2 if no cmake is available. As "-O2" is much faster than the default
32+
# and still keeps the binary compatible with other CPU architectures (e.g. for shipping releases).
33+
#
34+
# ./bench.sh
35+
# Compiling with CFLAGS=-g and NO_CMAKE=1
36+
# AVG Time: 2725070.5
37+
# Compiling with CFLAGS=-O1 and NO_CMAKE=1
38+
# AVG Time: 804889.6
39+
# Compiling with CFLAGS=-O2 and NO_CMAKE=1
40+
# AVG Time: 688417.6
41+
# Compiling with CFLAGS=-O2 -march=native -mtune=native and NO_CMAKE=1
42+
# AVG Time: 652656.5
43+
# Compiling with CMAKE
44+
# AVG Time: 631423.5

build_deps.sh

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ CPUS=`getconf _NPROCESSORS_ONLN 2>/dev/null || sysctl -n hw.ncpu`
1111
ZSTD_DESTINATION=zstd
1212
ZSTD_REPO=https://github.com/facebook/zstd.git
1313
ZSTD_BRANCH=release
14-
ZSTD_TAG=v1.5.5
14+
ZSTD_TAG=v1.5.6
1515
ZSTD_SUCCESS=lib/libzstd.a
1616

1717
fail_check()
@@ -52,14 +52,23 @@ CheckoutLib()
5252

5353
BuildLibrary()
5454
{
55-
case $OS in
56-
Linux)
57-
export CFLAGS="-fPIC"
58-
export CXXFLAGS="-fPIC"
59-
;;
60-
*)
61-
;;
62-
esac
55+
if [ -z "$NO_CMAKE" ] && command -v cmake >/dev/null 2>&1; then
56+
echo "Using cmake build system..."
57+
cmake -S build/cmake
58+
else
59+
echo "cmake not found, using make directly..."
60+
# Only define -O2 if CFLAGS is not already defined
61+
export CFLAGS=${CFLAGS:-"-O2"}
62+
63+
case $OS in
64+
Linux)
65+
export CFLAGS="$CFLAGS -fPIC"
66+
export CXXFLAGS="$CXXFLAGS -fPIC"
67+
;;
68+
*)
69+
;;
70+
esac
71+
fi
6372

6473
fail_check make -j $CPUS
6574
rm -rf lib/*.so

scripts/bench_compress.erl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-module(bench_compress).
2+
-export([main/1]).
3+
4+
generate_data(Size) ->
5+
rand:seed({exsss,[244613567848733509|123751949107613598]}),
6+
binary:copy(rand:bytes(trunc(Size/10)), 10).
7+
8+
main(_) ->
9+
Data = generate_data(50000000),
10+
Times = bench(Data, 10),
11+
io:format("AVG Time: ~p~n", [lists:sum(Times) / length(Times)]),
12+
erlang:halt().
13+
14+
bench(_Data, 0) ->
15+
[];
16+
bench(Data, Count) ->
17+
{Time, _} = timer:tc(ezstd, compress, [Data, 15]),
18+
io:format("Time: ~p~n", [Time]),
19+
[Time | bench(Data, Count - 1)].

0 commit comments

Comments
 (0)