Skip to content

Commit a2ffe16

Browse files
biojppmingydotnet
authored andcommitted
rapidyaml: Add run-time timing toggle
1 parent 1bfcc1f commit a2ffe16

7 files changed

Lines changed: 80 additions & 16 deletions

File tree

rapidyaml/native/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,8 @@ add_custom_target(${libname}-test-run
6464
COMMAND $<TARGET_FILE:${libname}-test>
6565
COMMENT "running C++ tests"
6666
)
67+
add_custom_target(${libname}-test-run-timing
68+
DEPENDS ${libname}-test
69+
COMMAND $<TARGET_FILE:${libname}-test> --timing
70+
COMMENT "running C++ tests, with timing"
71+
)

rapidyaml/native/Makefile

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,21 @@ cfg-static: rapidyaml
6363
$(CMK_ENV) $(CMAKE) -S . -B $(BDIR)-static $(CMK_FLAGS) $(CMK_FLAGS_EXTRA) -D BUILD_SHARED_LIBS=OFF
6464
test-static: build-static
6565
$(CMK_ENV) $(CMAKE) --build $(BDIR)-static --verbose --target rapidyaml-test-run
66+
test-static-timing: build-static
67+
$(CMK_ENV) $(CMAKE) --build $(BDIR)-static --verbose --target rapidyaml-test-run-timing
6668
$(RAPIDYAML_LIB): cfg-static $(RAPIDYAML_DEPS)
67-
$(CMK_ENV) $(CMAKE) --build $(BDIR)-static --target rapidyaml --parallel --verbose
69+
$(CMK_ENV) $(CMAKE) --build $(BDIR)-static --verbose --parallel --target rapidyaml
6870
cp -fv $(BDIR)-static/*.a $@
6971

7072
build-shared: $(RAPIDYAML_SO)
7173
cfg-shared: rapidyaml
7274
$(CMK_ENV) $(CMAKE) -S . -B $(BDIR)-shared $(CMK_FLAGS) $(CMK_FLAGS_EXTRA) -D BUILD_SHARED_LIBS=ON
7375
test-shared: build-shared
74-
$(CMK_ENV) $(CMAKE) --build $(BDIR)-shared --target rapidyaml-test-run --verbose
76+
$(CMK_ENV) $(CMAKE) --build $(BDIR)-shared --verbose --target rapidyaml-test-run
77+
test-shared-timing: build-shared
78+
$(CMK_ENV) $(CMAKE) --build $(BDIR)-shared --verbose --target rapidyaml-test-run-timing
7579
$(RAPIDYAML_SO): cfg-shared $(RAPIDYAML_DEPS)
76-
$(CMK_ENV) $(CMAKE) --build $(BDIR)-shared --target rapidyaml --parallel --verbose
80+
$(CMK_ENV) $(CMAKE) --build $(BDIR)-shared --verbose --parallel --target rapidyaml
7781
cp -fv $(BDIR)-shared/*.so $@
7882
ln -fs $@ librapidyaml.so
7983

rapidyaml/native/org_rapidyaml_Rapidyaml.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@
55
extern "C" {
66
#endif
77

8+
89
static C4_NO_INLINE void throw_runtime_exception(JNIEnv * env, const char* msg);
910
static C4_NO_INLINE void throw_parse_error(JNIEnv *env, size_t offset, size_t line, size_t column, const char *msg);
1011

1112

13+
JNIEXPORT void JNICALL
14+
Java_org_rapidyaml_Rapidyaml_ysparse_1timing_1set(JNIEnv *, jobject, jboolean yes)
15+
{
16+
ysparse_timing_set(yes);
17+
}
18+
1219
JNIEXPORT jlong JNICALL
1320
Java_org_rapidyaml_Rapidyaml_ys2evt_1init(JNIEnv *env, jobject)
1421
{
1522
Ryml2Evt *obj = ys2evt_init();
1623
return (jlong)obj;
1724
}
1825

26+
1927
JNIEXPORT void JNICALL
2028
Java_org_rapidyaml_Rapidyaml_ys2evt_1destroy(JNIEnv *, jobject, jlong obj)
2129
{
@@ -140,6 +148,16 @@ Java_org_rapidyaml_Rapidyaml_ys2evt_1parse_1buf(JNIEnv *env, jobject,
140148
//-----------------------------------------------------------------------------
141149
//-----------------------------------------------------------------------------
142150

151+
static bool s_timing_enabled = false;
152+
RYML_EXPORT bool ysparse_timing_get()
153+
{
154+
return s_timing_enabled;
155+
}
156+
RYML_EXPORT void ysparse_timing_set(bool yes)
157+
{
158+
s_timing_enabled = yes;
159+
}
160+
143161
static C4_NO_INLINE void throw_java_exception(JNIEnv * env, const char* type, const char* msg)
144162
{
145163
jclass clazz = env->FindClass(type);

rapidyaml/native/org_rapidyaml_Rapidyaml.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rapidyaml/native/ysparse_common.hpp

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#define YSPARSE_COMMON_HPP_
44

55
#include <stdexcept>
6+
#include <c4/yml/export.hpp>
67

78
namespace ryml {
89
using namespace c4;
@@ -18,6 +19,19 @@ struct YsParseError : public std::exception
1819
const char* what() const noexcept override { return msg.c_str(); }
1920
};
2021

22+
23+
//-----------------------------------------------------------------------------
24+
// timing
25+
26+
#ifdef __cplusplus
27+
extern "C" {
28+
#endif
29+
RYML_EXPORT bool ysparse_timing_get();
30+
RYML_EXPORT void ysparse_timing_set(bool yes);
31+
#ifdef __cplusplus
32+
}
33+
#endif
34+
2135
#ifndef YSPARSE_TIMED
2236
#define TIMED_SECTION(...)
2337
#error
@@ -28,22 +42,28 @@ struct YsParseError : public std::exception
2842
struct timed_section
2943
{
3044
using myclock = std::chrono::steady_clock;
31-
ryml::csubstr name;
45+
const char* name;
3246
size_type len;
3347
myclock::time_point start;
34-
timed_section(ryml::csubstr n, size_type len_=0)
35-
: name(n)
36-
, len(len_)
37-
, start(myclock::now())
48+
C4_NO_INLINE timed_section(const char* n, size_type len_=0)
3849
{
50+
if(ysparse_timing_get())
51+
{
52+
name = n;
53+
len = len_;
54+
start = myclock::now();
55+
}
3956
}
40-
~timed_section()
57+
C4_NO_INLINE ~timed_section()
4158
{
42-
const std::chrono::duration<float, std::milli> t = myclock::now() - start;
43-
fprintf(stderr, "%.6fms: %.*s", t.count(), (int)name.len, name.str);
44-
if(len)
45-
fprintf(stderr, " %.3fMB/s", (float)len / t.count() * 1.e-3);
46-
fprintf(stderr, "\n");
59+
if(ysparse_timing_get())
60+
{
61+
const std::chrono::duration<float, std::milli> t = myclock::now() - start;
62+
fprintf(stderr, "%.6fms: %s", t.count(), name);
63+
if(len)
64+
fprintf(stderr, " %.3fMB/s", (float)len / t.count() * 1.e-3);
65+
fprintf(stderr, "\n");
66+
}
4767
}
4868
};
4969
#endif // YSPARSE_TIMED

rapidyaml/native/ysparse_test.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,14 @@ defn run(prompt session=nil):
570570
};
571571
} // namespace
572572

573-
int main()
573+
int main(int argc, const char *argv[])
574574
{
575+
for(int i = 1; i < argc; ++i)
576+
{
577+
csubstr arg = ryml::to_csubstr(argv[i]);
578+
if(arg == "--timing" || arg == "-t")
579+
ysparse_timing_set(true);
580+
}
575581
Ys2EvtScoped ys2evt;
576582
TestResult total = {};
577583
size_t failed_cases = {};

rapidyaml/src/main/java/org/rapidyaml/Rapidyaml.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ public class Rapidyaml
1313
{
1414
public static String RAPIDYAML_VERSION = "0.8.0";
1515

16+
private native void ysparse_timing_set(boolean yes);
17+
// TODO: rename these to ysparse_init() etc
1618
private native long ys2evt_init();
1719
private native void ys2evt_destroy(long ryml2evt);
1820
private native int ys2evt_parse(long ryml2evt, String filename,
@@ -64,7 +66,7 @@ public int parseYsToEvtBuf(ByteBuffer src, IntBuffer evt) throws Exception
6466

6567
public int parseYsToEvt(String filename, byte[] src, int[] evts) throws Exception
6668
{
67-
long t = timingStart("ys2evtBuf");
69+
long t = timingStart("ys2evt");
6870
int required_size = ys2evt_parse(this.ryml2evt, filename, src, src.length, evts, evts.length);
6971
timingStop("ys2evt", t, src.length);
7072
return required_size;
@@ -107,6 +109,7 @@ public static IntBuffer mkIntBuffer(int numInts)
107109
public void timingEnabled(boolean yes)
108110
{
109111
showTiming = yes;
112+
ysparse_timing_set(yes);
110113
}
111114

112115
private long timingStart(String name)

0 commit comments

Comments
 (0)