Skip to content

Commit

Permalink
optimize json parser alot
Browse files Browse the repository at this point in the history
  • Loading branch information
apache-hb committed Apr 4, 2024
1 parent e5b935c commit eed7ef6
Show file tree
Hide file tree
Showing 57 changed files with 942 additions and 571 deletions.
8 changes: 4 additions & 4 deletions data/docs/contrib.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ In order of importance:
1. When an object is heap allocated its constructor must be named `<type>_new` and need a @ref arena_t as the last parameter.
- `map_new`, `vector_new`, `set_new` etc

2. When an object is stack allocated a `<type>_make` function should be provided when construction requires logic
2. When an object is stack allocated a `<type>_init` function should be provided when construction requires logic

```c
typedef struct text_t
Expand All @@ -147,11 +147,11 @@ typedef struct text_t
size_t length; // must be equal to `strlen(string)`
} text_t;

inline text_t text_make(const char *string)
void text_init(text_t *text, const char *string)
{
size_t length = strlen(string);
text_t text = { .string = string, .length = length };
return text;
text->string = string;
text->length = length;
}
```
Expand Down
3 changes: 1 addition & 2 deletions src/common/base/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ src = [
'src/util.c',
'src/panic.c',
'src/log.c',
'src/bitset.c',
'src/typeinfo.c'
'src/bitset.c'
]

deps = [ core ]
Expand Down
14 changes: 7 additions & 7 deletions src/common/base/src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ bool str_equal(const char *lhs, const char *rhs)
}

USE_DECL
size_t ctu_strlen(const char *text)
size_t ctu_strlen(const char *str)
{
CTASSERT(text != NULL);
CTASSERT(str != NULL);

return strlen(text);
return strlen(str);
}

USE_DECL
Expand Down Expand Up @@ -146,9 +146,9 @@ text_t text_make(char *text, size_t length)
}

USE_DECL
text_t text_from(char *str)
text_t text_from(char *text)
{
return text_make(str, ctu_strlen(str));
return text_make(text, ctu_strlen(text));
}

USE_DECL
Expand All @@ -162,9 +162,9 @@ text_view_t text_view_make(const char *text, size_t length)
}

USE_DECL
text_view_t text_view_from(const char *str)
text_view_t text_view_from(const char *text)
{
return text_view_make(str, ctu_strlen(str));
return text_view_make(text, ctu_strlen(text));
}

bool text_equal(text_view_t lhs, text_view_t rhs)
Expand Down
47 changes: 32 additions & 15 deletions src/common/core/include/core/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@

#include <ctu_config.h>

#ifdef __cpp_lib_unreachable
# include <utility>
#endif

/// @defgroup compiler Compiler specific macros
/// @brief Compiler detection macros and compiler specific functionality
/// @ingroup core
/// @{

#if defined(__has_cpp_attribute)
# define CT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
#else
# define CT_HAS_CPP_ATTRIBUTE(x) 0
#endif

// always detect clang first because it pretends to be gcc and msvc
// note: i hate that clang does this
#if defined(__clang__)
Expand Down Expand Up @@ -67,7 +77,7 @@
# elif CT_CC_MSVC
# define CT_NORETURN __declspec(noreturn) void
# else
# define CT_NORETURN void
# define CT_NORETURN _Noreturn void
# endif
#endif

Expand All @@ -79,27 +89,34 @@
# define CT_PATH_SEPERATORS "/"
#endif

/// @def CT_UNREACHABLE()
/// @brief mark a point in code as unreachable

#if __cpp_lib_unreachable
# define CT_UNREACHABLE() std::unreachable()
#elif defined(CT_CC_MSVC)
# define CT_UNREACHABLE() __assume(0)
#elif defined(CT_CC_GNU) || defined(CT_CC_CLANG)
# define CT_UNREACHABLE() __builtin_unreachable()
#else
# define CT_UNREACHABLE() ((void)0)
#endif

/// @def CT_ASSUME(expr)
/// @brief assume that @a expr is true
/// @warning this is a compiler hint that can be used to optimize code
/// use with caution

#ifdef CT_CC_MSVC
# define CT_UNREACHABLE() __assume(0)
#if CT_HAS_CPP_ATTRIBUTE(assume)
# define CT_ASSUME(expr) [[assume(expr)]]
#elif defined(CT_CC_MSVC)
# define CT_ASSUME(expr) __assume(expr)
#elif CT_CC_GNU || CT_CC_CLANG
# define CT_UNREACHABLE() __builtin_unreachable()
# define CT_ASSUME(expr) \
do \
{ \
if (!(expr)) \
{ \
__builtin_unreachable(); \
} \
} while (0)
#elif defined(CT_CC_CLANG)
# define CT_ASSUME(expr) __builtin_assume(expr)
#elif defined(CT_CC_GNU)
# define CT_ASSUME(expr) __attribute__((assume(expr)))
#else
# define CT_UNREACHABLE() ((void)0)
# define CT_ASSUME(expr) ((void)0)
# define CT_ASSUME(expr) do { if (!(expr)) { CT_UNREACHABLE(); } } while (0)
#endif

// clang-format off
Expand Down
45 changes: 45 additions & 0 deletions src/common/interop/include/interop/actions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// SPDX-License-Identifier: LGPL-3.0-or-later
#pragma once

#include <ctu_interop_api.h>

#include "core/analyze.h"

CT_BEGIN_API

typedef struct where_t where_t;
typedef struct scan_t scan_t;

/// @defgroup flex_bison_macros Helpers for flex and bison driver frontends
/// @ingroup interop
/// @{

/// @brief tracks the current source position
/// @see https://ftp.gnu.org/old-gnu/Manuals/flex-2.5.4/html_node/flex_14.html
///
/// @param where a pointer to the current location
/// @param text current source text
CT_INTEROP_API void flex_action(INOUT_NOTNULL where_t *where, IN_STRING const char *text);

/// @brief retrevies more input for flex
///
/// @param scan the source scanner
/// @param out output buffer to write to
/// @param size total number of characters to write
///
/// @return number of characters written
CT_INTEROP_API int flex_input(INOUT_NOTNULL scan_t *scan, OUT_WRITES(size) char *out, int size);

/// @brief initialize source location tracking
///
/// @param where the source location to initialize
CT_INTEROP_API void flex_init(OUT_NOTNULL where_t *where);

/// @brief update the source location
///
/// @param where the source location to update
/// @param offsets the source location offsets
/// @param steps the number of steps to update by
CT_INTEROP_API void flex_update(INOUT_NOTNULL where_t *where, IN_READS(steps) const where_t *offsets, int steps);

CT_END_API
42 changes: 1 addition & 41 deletions src/common/interop/include/interop/flex.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,9 @@

#include <ctu_interop_api.h>

#include "core/where.h"

#include "base/panic.h"

typedef struct scan_t scan_t;

CT_BEGIN_API

/// @defgroup flex_bison_macros Helpers for flex and bison driver frontends
/// @ingroup interop
/// @{

/// @brief tracks the current source position
/// @see https://ftp.gnu.org/old-gnu/Manuals/flex-2.5.4/html_node/flex_14.html
///
/// @param where a pointer to the current location
/// @param text current source text
CT_INTEROP_API void flex_action(INOUT_NOTNULL where_t *where, IN_STRING const char *text);

/// @brief retrevies more input for flex
///
/// @param scan the source scanner
/// @param out output buffer to write to
/// @param size total number of characters to write
///
/// @return number of characters written
CT_INTEROP_API int flex_input(INOUT_NOTNULL scan_t *scan, OUT_WRITES(size) char *out, int size);

/// @brief initialize source location tracking
///
/// @param where the source location to initialize
CT_INTEROP_API void flex_init(OUT_NOTNULL where_t *where);

/// @brief update the source location
///
/// @param where the source location to update
/// @param offsets the source location offsets
/// @param steps the number of steps to update by
CT_INTEROP_API void flex_update(INOUT_NOTNULL where_t *where, IN_READS(steps) const where_t *offsets, int steps);
#include "interop/actions.h" // IWYU pragma: export

/// track source locations inside flex and bison
#ifndef YY_USER_ACTION
Expand Down Expand Up @@ -73,7 +37,3 @@ CT_INTEROP_API void flex_update(INOUT_NOTNULL where_t *where, IN_READS(steps) co
#ifndef YY_FATAL_ERROR
# define YY_FATAL_ERROR(msg) CT_NEVER("fatal flex error: %s", msg)
#endif

/// @}

CT_END_API
41 changes: 24 additions & 17 deletions src/common/interop/src/flex.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// SPDX-License-Identifier: LGPL-3.0-only

#include "interop/actions.h"

#include "base/panic.h"

#include "core/where.h"
#include "scan/scan.h"

#include "interop/flex.h"

#include <limits.h>

USE_DECL
Expand All @@ -14,21 +15,24 @@ void flex_action(where_t *where, const char *text)
CTASSERT(where != NULL);
CTASSERT(text != NULL);

where->first_line = where->last_line;
where->first_column = where->last_column;
where_t tmp = *where;
tmp.first_line = tmp.last_line;
tmp.first_column = tmp.last_column;

for (int i = 0; text[i]; i++)
{
if (text[i] == '\n')
{
where->last_line += 1;
where->last_column = 0;
tmp.last_line += 1;
tmp.last_column = 0;
}
else
{
where->last_column += 1;
tmp.last_column += 1;
}
}

*where = tmp;
}

USE_DECL
Expand All @@ -46,10 +50,8 @@ void flex_init(where_t *where)
{
CTASSERT(where != NULL);

where->first_line = 0;
where->first_column = 0;
where->last_line = 0;
where->last_column = 0;
where_t zero = { 0 };
*where = zero;
}

USE_DECL
Expand All @@ -63,14 +65,19 @@ void flex_update(where_t *where, const where_t *offsets, int steps)
where_t rhs1 = offsets[1];
where_t rhsn = offsets[steps];

where->first_line = rhs1.first_line;
where->first_column = rhs1.first_column;
where->last_line = rhsn.last_line;
where->last_column = rhsn.last_column;
where_t tmp = {
.first_line = rhs1.first_line,
.first_column = rhs1.first_column,
.last_line = rhsn.last_line,
.last_column = rhsn.last_column,
};

*where = tmp;
}
else
{
where->last_line = offsets[0].last_line;
where->last_column = offsets[0].last_column;
where_t rhs = offsets[0];
where->last_line = rhs.last_line;
where->last_column = rhs.last_column;
}
}
6 changes: 4 additions & 2 deletions src/common/notify/include/notify/notify.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "core/analyze.h"
#include "core/compiler.h"

#include "scan/node.h"

#include "notify/diagnostic.h"

#include <stdarg.h>
Expand Down Expand Up @@ -36,7 +38,7 @@ typedef struct event_t
const diagnostic_t *diagnostic;

/// @brief the primary node that this event is attached to
const node_t *node;
node_t node;

/// @brief the primary message
FIELD_STRING char *message;
Expand All @@ -54,7 +56,7 @@ typedef struct event_t
typedef struct segment_t
{
/// @brief the related node
const node_t *node;
node_t node;

/// @brief the message associated with this segment
FIELD_STRING char *message;
Expand Down
4 changes: 2 additions & 2 deletions src/common/notify/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ libnotify = library('notify', src,
build_by_default : not meson.is_subproject(),
install : not meson.is_subproject(),
c_args : user_args + [ '-DCT_NOTIFY_BUILD=1' ],
dependencies : [ std, arena ],
dependencies : [ std, arena, scan ],
include_directories : inc
)

notify = declare_dependency(
link_with : libnotify,
dependencies : base,
dependencies : [ base, scan ],
include_directories : inc
)

Expand Down
4 changes: 2 additions & 2 deletions src/common/notify/src/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ event_builder_t msg_vnotify(logger_t *logs, const diagnostic_t *diagnostic, cons

event_t event = {
.diagnostic = diagnostic,
.node = node,
.node = *node,
.message = msg,
.segments = NULL,
.notes = NULL,
Expand Down Expand Up @@ -171,7 +171,7 @@ void msg_vappend(event_builder_t builder, const node_t *node, const char *fmt, v
char *msg = str_vformat(builder.arena, fmt, args);

segment_t segment = {
.node = node,
.node = *node,
.message = msg
};

Expand Down
Loading

0 comments on commit eed7ef6

Please sign in to comment.