Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make faster #123

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ rel/riak_dt
data/*
.qc/*
.eqc-info
current_counterexample.eqc
current_counterexample.eqc
*.o
75 changes: 75 additions & 0 deletions c_src/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Based on c_src.mk from erlang.mk by Loic Hoguin <[email protected]>

.PHONY: all
CURDIR := $(shell pwd)
BASEDIR := $(abspath $(CURDIR)/..)

PROJECT ?= $(notdir $(BASEDIR))
PROJECT := $(strip $(PROJECT))

ERTS_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s/erts-~s/include/\", [code:root_dir(), erlang:system_info(version)]).")
ERL_INTERFACE_INCLUDE_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, include)]).")
ERL_INTERFACE_LIB_DIR ?= $(shell erl -noshell -s init stop -eval "io:format(\"~s\", [code:lib_dir(erl_interface, lib)]).")

C_SRC_DIR = $(CURDIR)

# System type and C compiler/flags.

UNAME_SYS := $(shell uname -s)
ifeq ($(UNAME_SYS), Darwin)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -arch x86_64 -finline-functions -Wall
CXXFLAGS ?= -O3 -arch x86_64 -finline-functions -Wall
LDFLAGS ?= -arch x86_64 -flat_namespace -undefined suppress
else ifeq ($(UNAME_SYS), FreeBSD)
CC ?= cc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall
CXXFLAGS ?= -O3 -finline-functions -Wall
else ifeq ($(UNAME_SYS), Linux)
CC ?= gcc
CFLAGS ?= -O3 -std=c99 -finline-functions -Wall
CXXFLAGS ?= -O3 -finline-functions -Wall
endif

CFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)
CXXFLAGS += -fPIC -I $(ERTS_INCLUDE_DIR) -I $(ERL_INTERFACE_INCLUDE_DIR)

LDLIBS += -L $(ERL_INTERFACE_LIB_DIR) -lerl_interface -lei
LDFLAGS += -shared

# Verbosity.

c_verbose_0 = @echo " C " $(?F);
c_verbose = $(c_verbose_$(V))

cpp_verbose_0 = @echo " CPP " $(?F);
cpp_verbose = $(cpp_verbose_$(V))

link_verbose_0 = @echo " LD " $(@F);
link_verbose = $(link_verbose_$(V))

SOURCES := $(shell find $(C_SRC_DIR) -type f \( -name "*.c" -o -name "*.C" -o -name "*.cc" -o -name "*.cpp" \))
OBJECTS = $(addsuffix .o, $(basename $(SOURCES)))

COMPILE_C = $(c_verbose) $(CC) $(CFLAGS) $(CPPFLAGS) -c
COMPILE_CPP = $(cpp_verbose) $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c

RIAK_DT_VCLOCK_SO = $(CURDIR)/../priv/riak_dt_vclock.so
$(RIAK_DT_VCLOCK_SO): riak_dt_vclock.o
@mkdir -p $(BASEDIR)/priv/
$(link_verbose) $(CC) riak_dt_vclock.o $(LDFLAGS) $(LDLIBS) -o $(RIAK_DT_VCLOCK_SO)

%.o: %.c
$(COMPILE_C) $(OUTPUT_OPTION) $<

%.o: %.cc
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

%.o: %.C
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

%.o: %.cpp
$(COMPILE_CPP) $(OUTPUT_OPTION) $<

clean:
@rm -f $(OBJECTS) $(CURDIR)/../priv/riak_dt_vclock.so
286 changes: 286 additions & 0 deletions c_src/riak_dt_vclock.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
#include "erl_nif.h"
#include <stdbool.h>


static ERL_NIF_TERM atom_true;
static ERL_NIF_TERM atom_false;
static ERL_NIF_TERM badarg;



static int load(ErlNifEnv* env, void** priv_data, ERL_NIF_TERM load_info) {
atom_true = enif_make_atom(env, "true");
atom_false = enif_make_atom(env, "false");
badarg = enif_make_badarg(env);
return 0;
}

ERL_NIF_TERM is_sorted_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM head1, head2, tail, rest, list = argv[0];
const ERL_NIF_TERM *tuple1, *tuple2;
int arity1, arity2;

if(enif_get_list_cell(env, list, &head1, &tail)) {
if(!enif_get_tuple(env, head1, &arity1, &tuple1))
return badarg;
if(arity1 == 0)
return badarg;

while(enif_get_list_cell(env, tail, &head2, &rest)) {
if(!enif_get_tuple(env, head2, &arity2, &tuple2))
return badarg;
if (arity2 == 0)
return badarg;
if (enif_compare(tuple1[0], tuple2[0]) >= 0)
return atom_false;

tuple1 = tuple2;
arity1 = arity2;
head1 = head2;
tail = rest;
}
return atom_true;
} else if (enif_is_empty_list(env, list))
return atom_true;
else
return badarg;
}

static inline ErlNifSInt64 max(ErlNifSInt64 i1, ErlNifSInt64 i2) {
if (i1 > i2)
return i1;
else
return i2;
}

ERL_NIF_TERM merge2_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM headlhs, taillhs, listlhs = argv[0];
ERL_NIF_TERM headrhs, tailrhs, listrhs = argv[1];
ERL_NIF_TERM rretlist, retlist = enif_make_list(env, 0);
ERL_NIF_TERM newtuple;

const ERL_NIF_TERM *tuplelhs, *tuplerhs;
int aritylhs, arityrhs, cmp;
ErlNifSInt64 vallhs, valrhs;

/* We will never receive an empty list either on the left or right side */

while (enif_get_list_cell(env, listlhs, &headlhs, &taillhs) &&
enif_get_list_cell(env, listrhs, &headrhs, &tailrhs)) {

if(!enif_get_tuple(env, headlhs, &aritylhs, &tuplelhs))
return badarg;
if (aritylhs != 2)
return badarg;

if(!enif_get_tuple(env, headrhs, &arityrhs, &tuplerhs))
return badarg;
if (arityrhs != 2)
return badarg;

cmp = enif_compare(tuplelhs[0], tuplerhs[0]);
if (cmp == 0) {
if (!enif_get_int64(env, tuplelhs[1], &vallhs))
return badarg;
if (!enif_get_int64(env, tuplerhs[1], &valrhs))
return badarg;

newtuple = enif_make_tuple2(env, tuplelhs[0], enif_make_int64(env, max(vallhs, valrhs)));
retlist = enif_make_list_cell(env, newtuple, retlist);
/* Pop both lists */
listlhs = taillhs;
listrhs = tailrhs;
} else if (cmp < 0) { /* lhs < rhs */
/* Pop something off LHS to have it try to catch up with RHS */
retlist = enif_make_list_cell(env, headlhs, retlist);
listlhs = taillhs;
} else if (cmp > 0) { /* lhs > rhs */
/* Pop something off RHS to have it try to catch up with LHS */
retlist = enif_make_list_cell(env, headrhs, retlist);
listrhs = tailrhs;
}
}
while (enif_get_list_cell(env, listlhs, &headlhs, &taillhs)) {
retlist = enif_make_list_cell(env, headlhs, retlist);
listlhs = taillhs;
}

while (enif_get_list_cell(env, listrhs, &headrhs, &tailrhs)) {
retlist = enif_make_list_cell(env, headrhs, retlist);
listrhs = tailrhs;
}

/* TODO: Maybe have some heuristic here to determine whether to do a reverse here, or in Erlang? */
enif_make_reverse_list(env, retlist, &rretlist);
return rretlist;
}

/* Return true if Lhs is a direct descendant of Rhs, else false -- remember, a vclock is its own descendant! */

ERL_NIF_TERM descends2_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM headlhs, taillhs, listlhs = argv[0];
ERL_NIF_TERM headrhs, tailrhs, listrhs = argv[1];

const ERL_NIF_TERM *tuplelhs, *tuplerhs;
int aritylhs, arityrhs, cmp;
ErlNifSInt64 vallhs, valrhs;

while (enif_get_list_cell(env, listlhs, &headlhs, &taillhs) &&
enif_get_list_cell(env, listrhs, &headrhs, &tailrhs)) {

if(!enif_get_tuple(env, headlhs, &aritylhs, &tuplelhs))
return badarg;
if (aritylhs != 2)
return badarg;

if(!enif_get_tuple(env, headrhs, &arityrhs, &tuplerhs))
return badarg;
if (arityrhs != 2)
return badarg;
cmp = enif_compare(tuplelhs[0], tuplerhs[0]);
if (cmp == 0) {
if (!enif_get_int64(env, tuplelhs[1], &vallhs))
return badarg;
if (!enif_get_int64(env, tuplerhs[1], &valrhs))
return badarg;
if (vallhs < valrhs)
return atom_false;
listlhs = taillhs;
listrhs = tailrhs;
} else if (cmp < 0) {
/* We have an extra actor on the lhs, drop it */
listlhs = taillhs;
} else
return atom_false;
}
/* Make sure that RHS is empty */
if(enif_get_list_cell(env, listrhs, &headrhs, &tailrhs))
return atom_false;

return atom_true;
}

ERL_NIF_TERM drop_dots_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM headlhs, taillhs, listlhs = argv[0];
ERL_NIF_TERM headrhs, tailrhs, listrhs = argv[1];
ERL_NIF_TERM rretlist, retlist = enif_make_list(env, 0);
ERL_NIF_TERM newtuple;

const ERL_NIF_TERM *tuplelhs, *tuplerhs;
int aritylhs, arityrhs, cmp;
ErlNifSInt64 vallhs, valrhs;

/* We will never receive an empty list either on the left or right side */

while (enif_get_list_cell(env, listlhs, &headlhs, &taillhs) &&
enif_get_list_cell(env, listrhs, &headrhs, &tailrhs)) {

if(!enif_get_tuple(env, headlhs, &aritylhs, &tuplelhs))
return badarg;
if (aritylhs != 2)
return badarg;

if(!enif_get_tuple(env, headrhs, &arityrhs, &tuplerhs))
return badarg;
if (arityrhs != 2)
return badarg;

cmp = enif_compare(tuplelhs[0], tuplerhs[0]);
if (cmp == 0) {
if (!enif_get_int64(env, tuplelhs[1], &vallhs))
return badarg;
if (!enif_get_int64(env, tuplerhs[1], &valrhs))
return badarg;
if (vallhs > valrhs) {
newtuple = enif_make_tuple2(env, tuplelhs[0], enif_make_int64(env, vallhs));
retlist = enif_make_list_cell(env, newtuple, retlist);
}
/* Pop both lists */
listlhs = taillhs;
listrhs = tailrhs;
} else if (cmp < 0) { /* lhs < rhs */
/* Pop something off LHS to have it try to catch up with RHS */
retlist = enif_make_list_cell(env, headlhs, retlist);
listlhs = taillhs;
} else if (cmp > 0) { /* lhs > rhs */
/* Pop something off RHS to have it try to catch up with LHS */
listrhs = tailrhs;
}
}
while (enif_get_list_cell(env, listlhs, &headlhs, &taillhs)) {
retlist = enif_make_list_cell(env, headlhs, retlist);
listlhs = taillhs;
}

/* TODO: Maybe have some heuristic here to determine whether to do a reverse here, or in Erlang? */
enif_make_reverse_list(env, retlist, &rretlist);
return rretlist;
}

ERL_NIF_TERM dominates2_nif(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) {
ERL_NIF_TERM headlhs, taillhs, listlhs = argv[0];
ERL_NIF_TERM headrhs, tailrhs, listrhs = argv[1];
bool has_dominated = false;

const ERL_NIF_TERM *tuplelhs, *tuplerhs;
int aritylhs, arityrhs, cmp;
ErlNifSInt64 vallhs, valrhs;

while (enif_get_list_cell(env, listlhs, &headlhs, &taillhs) &&
enif_get_list_cell(env, listrhs, &headrhs, &tailrhs)) {

if(!enif_get_tuple(env, headlhs, &aritylhs, &tuplelhs))
return badarg;
if (aritylhs != 2)
return badarg;

if(!enif_get_tuple(env, headrhs, &arityrhs, &tuplerhs))
return badarg;
if (arityrhs != 2)
return badarg;

cmp = enif_compare(tuplelhs[0], tuplerhs[0]);
if (!enif_get_int64(env, tuplelhs[1], &vallhs))
return badarg;
if (!enif_get_int64(env, tuplerhs[1], &valrhs))
return badarg;

/* Extra actor on left hand side, ignore it */
if (cmp < 0) {
listlhs = taillhs;
} else if (cmp > 0) {
return atom_false;
} else {
if (vallhs < valrhs)
return atom_false;
else if(vallhs > valrhs)
has_dominated = true;
listlhs = taillhs;
listrhs = tailrhs;
}
}

/* Both sides are empty */
if(enif_is_empty_list(env, listlhs) && enif_is_empty_list(env, listrhs)) {
if(has_dominated)
return atom_true;
else
return atom_false;
/* LHS is not empty, but RHS is empty */
} else if(!enif_is_empty_list(env, listlhs) && enif_is_empty_list(env, listrhs)) {
return atom_true;
/* LHS is empty, but RHS is not empty */
} else {
return atom_false;
}
}

static ErlNifFunc nif_funcs[] = {
{"descends2", 2, descends2_nif},
{"is_sorted", 1, is_sorted_nif},
{"merge2", 2, merge2_nif},
{"drop_dots", 2, drop_dots_nif},
{"dominates2", 2, dominates2_nif}
};

ERL_NIF_INIT(riak_dt_vclock, nif_funcs, load, NULL, NULL, NULL)
2 changes: 2 additions & 0 deletions include/r18.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-type dict() :: dict:dict().
-type set() :: sets:set().
19 changes: 19 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,22 @@
{erl_opts, [debug_info, warnings_as_errors]}.
{eunit_opts, [verbose]}.
{xref_checks, [undefined_function_calls]}.


{profiles, [
{bench, [
{erl_opts, [
debug_info,
warnings_as_errors,
{platform_define, "^[0-9]+", namespaced_types},
{d, 'BENCH', true}
]}
]}
]}.

{pre_hooks,
[{"(linux|darwin|solaris)", compile, "make -C c_src"},
{"(freebsd)", compile, "gmake -C c_src"}]}.
{post_hooks,
[{"(linux|darwin|solaris)", clean, "make -C c_src clean"},
{"(freebsd)", clean, "gmake -C c_src clean"}]}.
Loading