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

WIP: Filterx perf improvements2 #401

Closed
wants to merge 3 commits into from
Closed
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 lib/filterx/filterx-expr.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ struct _FilterXExpr
static inline FilterXObject *
filterx_expr_eval(FilterXExpr *self)
{
#if 0
stats_counter_inc(self->eval_count);

#endif
return self->eval(self);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/filterx/filterx-object.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ filterx_object_free_method(FilterXObject *self)
void
filterx_object_init_instance(FilterXObject *self, FilterXType *type)
{
g_atomic_counter_set(&self->ref_cnt, 1);
self->ref_cnt = 1;
self->type = type;
self->readonly = !type->is_mutable;
}
Expand All @@ -118,16 +118,16 @@ filterx_object_freeze(FilterXObject *self)
{
if (filterx_object_is_frozen(self))
return FALSE;
g_assert(g_atomic_counter_get(&self->ref_cnt) == 1);
g_atomic_counter_set(&self->ref_cnt, FILTERX_OBJECT_MAGIC_BIAS);
g_assert(self->ref_cnt == 1);
self->ref_cnt = FILTERX_OBJECT_MAGIC_BIAS;
return TRUE;
}

void
filterx_object_unfreeze_and_free(FilterXObject *self)
{
g_assert(filterx_object_is_frozen(self));
g_atomic_counter_set(&self->ref_cnt, 1);
self->ref_cnt = 1;
filterx_object_unref(self);
}

Expand Down
12 changes: 6 additions & 6 deletions lib/filterx/filterx-object.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ FILTERX_DECLARE_TYPE(object);

struct _FilterXObject
{
GAtomicCounter ref_cnt;
GAtomicCounter fx_ref_cnt;
guint32 ref_cnt;
guint32 fx_ref_cnt;

/* NOTE:
*
Expand Down Expand Up @@ -110,7 +110,7 @@ void filterx_object_free_method(FilterXObject *self);
static inline gboolean
filterx_object_is_frozen(FilterXObject *self)
{
return g_atomic_counter_get(&self->ref_cnt) == FILTERX_OBJECT_MAGIC_BIAS;
return self->ref_cnt == FILTERX_OBJECT_MAGIC_BIAS;
}

static inline FilterXObject *
Expand All @@ -122,7 +122,7 @@ filterx_object_ref(FilterXObject *self)
if (filterx_object_is_frozen(self))
return self;

g_atomic_counter_inc(&self->ref_cnt);
self->ref_cnt++;

return self;
}
Expand All @@ -136,8 +136,8 @@ filterx_object_unref(FilterXObject *self)
if (filterx_object_is_frozen(self))
return;

g_assert(g_atomic_counter_get(&self->ref_cnt) > 0);
if (g_atomic_counter_dec_and_test(&self->ref_cnt))
g_assert(self->ref_cnt > 0);
if (--(self->ref_cnt) == 0)
{
self->type->free_fn(self);
g_free(self);
Expand Down
10 changes: 5 additions & 5 deletions lib/filterx/filterx-ref.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ _filterx_ref_clone(FilterXObject *s)
static void
_filterx_ref_cow(FilterXRef *self)
{
if (g_atomic_counter_get(&self->value->fx_ref_cnt) <= 1)
if (self->value->fx_ref_cnt <= 1)
return;

FilterXObject *cloned = filterx_object_clone(self->value);

g_atomic_counter_dec_and_test(&self->value->fx_ref_cnt);
self->value->fx_ref_cnt--;
filterx_object_unref(self->value);

self->value = cloned;
g_atomic_counter_inc(&self->value->fx_ref_cnt);
self->value->fx_ref_cnt++;
}

FilterXObject *
Expand Down Expand Up @@ -107,7 +107,7 @@ _filterx_ref_free(FilterXObject *s)
{
FilterXRef *self = (FilterXRef *) s;

g_atomic_counter_dec_and_test(&self->value->fx_ref_cnt);
self->value->fx_ref_cnt--;
filterx_object_unref(self->value);

filterx_object_free_method(s);
Expand Down Expand Up @@ -257,7 +257,7 @@ filterx_ref_new(FilterXObject *value)
self->super.readonly = FALSE;

self->value = value;
g_atomic_counter_inc(&self->value->fx_ref_cnt);
self->value->fx_ref_cnt++;

return &self->super;
}
Expand Down
50 changes: 27 additions & 23 deletions lib/filterx/object-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@
#include "scanner/list-scanner/list-scanner.h"
#include "str-repr/encode.h"

static gboolean
#define json_object_get_type(jso) (*((int *) jso))

static inline gboolean
_is_json_cacheable(struct json_object *jso)
{
if (json_object_get_type(jso) == json_type_double && JSON_C_MAJOR_VERSION == 0 && JSON_C_MINOR_VERSION < 14)
Expand All @@ -53,6 +55,30 @@ _is_json_cacheable(struct json_object *jso)
return TRUE;
}

static inline FilterXObject *
filterx_json_get_cached_object(struct json_object *jso)
{
if (!_is_json_cacheable(jso))
return NULL;

if (json_object_get_type(jso) == json_type_double)
{
/*
* This is a workaround to ditch builtin serializer for double objects
* that are set when parsing from a string representation.
* json_object_double_new_ds() will set userdata to the string
* representation of the number, as extracted from the JSON source.
*
* Changing the value of the double to the same value, ditches this,
* but only if necessary.
*/
json_object_set_double(jso, json_object_get_double(jso));
}

return (FilterXObject *) json_object_get_userdata(jso);
}


static int
_deep_copy_filterx_object_ref(json_object *src, json_object *parent, const char *key, size_t index, json_object **dst)
{
Expand Down Expand Up @@ -154,28 +180,6 @@ filterx_json_associate_cached_object(struct json_object *jso, FilterXObject *fil
json_object_set_userdata(jso, filterx_obj, NULL);
}

FilterXObject *
filterx_json_get_cached_object(struct json_object *jso)
{
if (!_is_json_cacheable(jso))
return NULL;

if (json_object_get_type(jso) == json_type_double)
{
/*
* This is a workaround to ditch builtin serializer for double objects
* that are set when parsing from a string representation.
* json_object_double_new_ds() will set userdata to the string
* representation of the number, as extracted from the JSON source.
*
* Changing the value of the double to the same value, ditches this,
* but only if necessary.
*/
json_object_set_double(jso, json_object_get_double(jso));
}

return (FilterXObject *) json_object_get_userdata(jso);
}

FilterXObject *
filterx_json_new_from_repr(const gchar *repr, gssize repr_len)
Expand Down
1 change: 0 additions & 1 deletion lib/filterx/object-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const gchar *filterx_json_array_to_json_literal(FilterXObject *s);
FilterXObject *filterx_json_convert_json_to_object(FilterXObject *root_obj, FilterXWeakRef *root_container,
struct json_object *jso);
void filterx_json_associate_cached_object(struct json_object *jso, FilterXObject *filterx_object);
FilterXObject *filterx_json_get_cached_object(struct json_object *jso);

struct json_object *filterx_json_object_get_value(FilterXObject *s);
struct json_object *filterx_json_array_get_value(FilterXObject *s);
Expand Down
8 changes: 4 additions & 4 deletions lib/stats/stats-counter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ stats_counter_read_only(StatsCounterItem *counter)
static inline void
stats_counter_add(StatsCounterItem *counter, gssize add)
{
if (counter)
if (G_UNLIKELY(counter))
{
g_assert(!stats_counter_read_only(counter));
atomic_gssize_add(&counter->value, add);
Expand All @@ -61,7 +61,7 @@ stats_counter_add(StatsCounterItem *counter, gssize add)
static inline void
stats_counter_sub(StatsCounterItem *counter, gssize sub)
{
if (counter)
if (G_UNLIKELY(counter))
{
g_assert(!stats_counter_read_only(counter));
atomic_gssize_sub(&counter->value, sub);
Expand All @@ -71,7 +71,7 @@ stats_counter_sub(StatsCounterItem *counter, gssize sub)
static inline void
stats_counter_inc(StatsCounterItem *counter)
{
if (counter)
if (G_UNLIKELY(counter))
{
g_assert(!stats_counter_read_only(counter));
atomic_gssize_inc(&counter->value);
Expand All @@ -81,7 +81,7 @@ stats_counter_inc(StatsCounterItem *counter)
static inline void
stats_counter_dec(StatsCounterItem *counter)
{
if (counter)
if (G_UNLIKELY(counter))
{
g_assert(!stats_counter_read_only(counter));
atomic_gssize_dec(&counter->value);
Expand Down
Loading