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

Improve compose traverse perf #380

Merged
merged 5 commits into from
Sep 1, 2024
Merged
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
33 changes: 26 additions & 7 deletions bench/compose-traversal.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,40 @@

#include "config.h"

#include <string.h>
#include <time.h>

#include "xkbcommon/xkbcommon-compose.h"

#include "../test/compose-iter.h"
#include "../test/test.h"
#include "bench.h"

#define BENCHMARK_ITERATIONS 1000

static void
compose_fn(struct xkb_compose_table_entry *entry, void *data)
{
assert (entry);
}

/* Benchmark compose traversal using:
* • the internal recursive function `xkb_compose_table_for_each` if `foreach` is
* is passed as argument to the program;
* • else the iterator API (`xkb_compose_table_iterator_new`, …).
*/
int
main(void)
main(int argc, char *argv[])
{
struct xkb_context *ctx;
char *path;
FILE *file;
struct xkb_compose_table *table;
struct xkb_compose_table_iterator *iter;
struct xkb_compose_table_entry *entry;
struct bench bench;
char *elapsed;

bool use_foreach_impl = (argc > 1 && strcmp(argv[1], "foreach") == 0);

ctx = test_get_context(CONTEXT_NO_FLAG);
assert(ctx);

Expand All @@ -68,11 +81,17 @@ main(void)

bench_start(&bench);
for (int i = 0; i < BENCHMARK_ITERATIONS; i++) {
iter = xkb_compose_table_iterator_new(table);
while ((entry = xkb_compose_table_iterator_next(iter))) {
assert (entry);
if (use_foreach_impl) {
xkb_compose_table_for_each(table, compose_fn, NULL);
} else {
struct xkb_compose_table_iterator *iter;
struct xkb_compose_table_entry *entry;
iter = xkb_compose_table_iterator_new(table);
while ((entry = xkb_compose_table_iterator_next(iter))) {
assert (entry);
}
xkb_compose_table_iterator_free(iter);
}
xkb_compose_table_iterator_free(iter);
}
bench_stop(&bench);

Expand Down
31 changes: 29 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ if cc.has_header_symbol('stdio.h', 'asprintf', prefix: system_ext_define)
elif cc.has_header_symbol('stdio.h', 'vasprintf', prefix: system_ext_define)
configh_data.set('HAVE_VASPRINTF', 1)
endif
if cc.has_header_symbol('stdio.h', 'open_memstream', prefix: system_ext_define)
configh_data.set('HAVE_OPEN_MEMSTREAM', 1)
endif
if cc.has_header_symbol('stdlib.h', 'secure_getenv', prefix: system_ext_define)
configh_data.set('HAVE_SECURE_GETENV', 1)
elif cc.has_header_symbol('stdlib.h', '__secure_getenv', prefix: system_ext_define)
Expand Down Expand Up @@ -503,6 +506,9 @@ if build_tools
# Tool: compose
executable('xkbcli-compile-compose',
'tools/compile-compose.c',
'src/compose/dump.c',
'src/compose/dump.h',
'src/compose/escape.h',
dependencies: tools_dep,
install: true,
install_dir: dir_libexec)
Expand Down Expand Up @@ -765,7 +771,18 @@ test(
)
test(
'compose',
executable('test-compose', 'test/compose.c', dependencies: test_dep),
executable(
'test-compose',
'test/compose.c',
'test/shuffle-lines.c',
'test/shuffle-lines.h',
'test/compose-iter.c',
'test/compose-iter.h',
'src/compose/dump.c',
'src/compose/dump.h',
'src/compose/escape.h',
dependencies: test_dep
),
env: test_env,
)
test(
Expand Down Expand Up @@ -853,6 +870,8 @@ if valgrind.found()
'--track-origins=yes',
'--gen-suppressions=all',
'--error-exitcode=99'],
# This is used in some tests, to avoid excessive run time.
env: {'RUNNING_VALGRIND': '1'},
timeout_multiplier : 10)
else
message('valgrind not found, disabling valgrind test setup')
Expand Down Expand Up @@ -898,7 +917,15 @@ benchmark(
)
benchmark(
'compose-traversal',
executable('bench-compose-traversal', 'bench/compose-traversal.c', dependencies: test_dep),
executable(
'bench-compose-traversal',
'bench/compose-traversal.c',
'bench/bench.h',
'test/compose-iter.c',
'test/compose-iter.h',
'test/test.h',
dependencies: test_dep
),
env: bench_env,
)
benchmark(
Expand Down
92 changes: 92 additions & 0 deletions src/compose/dump.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
* Copyright © 2021 Ran Benita <[email protected]>
* Copyright © 2023 Pierre Le Marre <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

#include "config.h"

#include "src/darray.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>

#include "xkbcommon/xkbcommon-compose.h"
#include "parser.h"
#include "escape.h"
#include "dump.h"
#include "src/keysym.h"
#include "src/utils.h"

bool
print_compose_table_entry(FILE *file, struct xkb_compose_table_entry *entry)
{
size_t nsyms;
const xkb_keysym_t *syms = xkb_compose_table_entry_sequence(entry, &nsyms);
char buf[XKB_KEYSYM_NAME_MAX_SIZE];
for (size_t i = 0; i < nsyms; i++) {
xkb_keysym_get_name(syms[i], buf, sizeof(buf));
fprintf(file, "<%s>", buf);
if (i + 1 < nsyms) {
fprintf(file, " ");
}
}
fprintf(file, " : ");
const char *utf8 = xkb_compose_table_entry_utf8(entry);
if (*utf8 != '\0') {
char *escaped = escape_utf8_string_literal(utf8);
if (!escaped) {
fprintf(stderr, "ERROR: Cannot escape the string: allocation error\n");
return false;
} else {
fprintf(file, " \"%s\"", escaped);
free(escaped);
}
}
const xkb_keysym_t keysym = xkb_compose_table_entry_keysym(entry);
if (keysym != XKB_KEY_NoSymbol) {
xkb_keysym_get_name(keysym, buf, sizeof(buf));
fprintf(file, " %s", buf);
}
fprintf(file, "\n");
return true;
}

bool
xkb_compose_table_dump(FILE *file, struct xkb_compose_table *table)
{
struct xkb_compose_table_entry *entry;
struct xkb_compose_table_iterator *iter = xkb_compose_table_iterator_new(table);

if (!iter)
return false;

bool ok = true;
while ((entry = xkb_compose_table_iterator_next(iter))) {
if (!print_compose_table_entry(file, entry)) {
ok = false;
break;
}
}

xkb_compose_table_iterator_free(iter);
return ok;
}
91 changes: 5 additions & 86 deletions src/compose/dump.h
Original file line number Diff line number Diff line change
@@ -1,95 +1,14 @@
/*
* Copyright © 2023 Pierre Le Marre <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/


#ifndef COMPOSE_DUMP_H
#define COMPOSE_DUMP_H

#include "config.h"

#include <stdlib.h>

#include "src/utils.h"
#include "table.h"

/* Ad-hoc escaping for UTF-8 string
*
* Note that it only escapes the strict minimum to get a valid Compose file.
* It also escapes hexadecimal digits after an hexadecimal escape. This is not
* strictly needed by the current implementation: "\x0abcg" parses as "␊bcg",
* but better be cautious than sorry and produce "\x0a\x62\x63g" instead.
* In the latter string there is no ambiguity and no need to know the maximum
* number of digits supported by the escape sequence.
*/
static inline char*
escape_utf8_string_literal(const char *from)
{
const size_t length = strlen(from);
/* Longest escape is converting ASCII character to "\xNN" */
char* to = calloc(4 * length + 1, sizeof(*to));
if (!to)
return NULL;
bool
print_compose_table_entry(FILE *file, struct xkb_compose_table_entry *entry);

size_t t = 0;
bool previous_is_hex_escape = false;
uint8_t nbytes = 0;
for (size_t f = 0; f < length;) {
if ((unsigned char) from[f] < 0x80) {
/* ASCII */
if (from[f] <= 0x10 || from[f] == 0x7f ||
(is_xdigit(from[f]) && previous_is_hex_escape))
{
/* Control character or
hexadecimal digit following an hexadecimal escape */
snprintf_safe(&to[t], 5, "\\x%02x", from[f]);
t += 4;
previous_is_hex_escape = true;
} else if (from[f] == '"' || from[f] == '\\') {
/* Quote and backslash */
snprintf_safe(&to[t], 3, "\\%c", from[f]);
t += 2;
previous_is_hex_escape = false;
} else {
/* Other characters */
to[t++] = from[f];
previous_is_hex_escape = false;
}
f++;
continue;
}
/* Test next byte for the next Unicode codepoint’s bytes count */
else if ((unsigned char) from[f] < 0xe0)
nbytes = 2;
else if ((unsigned char) from[f] < 0xf0)
nbytes = 3;
else
nbytes = 4;
memcpy(&to[t], &from[f], nbytes);
t += nbytes;
f += nbytes;
previous_is_hex_escape = false;
}
to[t++] = '\0';
return realloc(to, t);
}
bool
xkb_compose_table_dump(FILE *file, struct xkb_compose_table *table);

#endif
Loading
Loading