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
Changes from 1 commit
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
Prev Previous commit
Next Next commit
compose: Add quickcheck test for traversal
Test against the `foreach` reference implementation:
- Suffle compose file lines randomly;
- Compare traversal entry by entry.

The `foreach` Compose traversal implementation is based on Ran’s work:
bluetech@f7f3c3c
wismill committed Sep 1, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit ddc58a1ab8b4df6c0eb7c7eb9c293076967ecdd3
6 changes: 6 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
@@ -771,6 +771,10 @@ test(
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',
@@ -863,6 +867,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')
78 changes: 78 additions & 0 deletions test/compose-iter.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright © 2022 Ran Benita <[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 "src/compose/escape.h"
#include "src/compose/parser.h"
#include "src/keysym.h"
#include "src/utils.h"
#include "test/compose-iter.h"

/* Reference implentation of Compose table traversal */
static void
for_each_helper(struct xkb_compose_table *table,
xkb_compose_table_iter_t iter,
void *data,
xkb_keysym_t *syms,
size_t nsyms,
uint16_t p)
{
if (!p) {
return;
}
const struct compose_node *node = &darray_item(table->nodes, p);
for_each_helper(table, iter, data, syms, nsyms, node->lokid);
syms[nsyms++] = node->keysym;
if (node->is_leaf) {
struct xkb_compose_table_entry entry = {
.sequence = syms,
.sequence_length = nsyms,
.keysym = node->leaf.keysym,
.utf8 = &darray_item(table->utf8, node->leaf.utf8),
};
iter(&entry, data);
} else {
for_each_helper(table, iter, data, syms, nsyms, node->internal.eqkid);
}
nsyms--;
for_each_helper(table, iter, data, syms, nsyms, node->hikid);
}

XKB_EXPORT void
xkb_compose_table_for_each(struct xkb_compose_table *table,
xkb_compose_table_iter_t iter,
void *data)
{
if (darray_size(table->nodes) <= 1) {
return;
}
xkb_keysym_t syms[MAX_LHS_LEN];
for_each_helper(table, iter, data, syms, 0, 1);
}
26 changes: 26 additions & 0 deletions test/compose-iter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef COMPOSE_LEGACY_ITER_H
#define COMPOSE_LEGACY_ITER_H

#include "config.h"
#include "src/compose/table.h"

/**
* The iterator function type used by xkb_compose_table_for_each().
*/
typedef void
(*xkb_compose_table_iter_t)(struct xkb_compose_table_entry *entry,
void *data);

/**
* Run a specified function for every valid entry in the table.
*
* The entries are returned in lexicographic order of the left-hand
* side of entries. This does not correspond to the order in which
* the entries appear in the Compose file.
*/
void
xkb_compose_table_for_each(struct xkb_compose_table *table,
xkb_compose_table_iter_t iter,
void *data);

#endif
93 changes: 90 additions & 3 deletions test/compose.c
Original file line number Diff line number Diff line change
@@ -32,6 +32,9 @@
#include "src/keysym.h"
#include "src/compose/parser.h"
#include "src/compose/escape.h"
#include "src/compose/dump.h"
#include "test/shuffle-lines.h"
#include "test/compose-iter.h"

static const char *
compose_status_string(enum xkb_compose_status status)
@@ -717,8 +720,45 @@ test_eq_entry(struct xkb_compose_table_entry *entry, xkb_keysym_t keysym, const
return ok;
}

static bool
test_eq_entries(struct xkb_compose_table_entry *entry1, struct xkb_compose_table_entry *entry2)
{
if (!entry1 || !entry2)
goto error;
bool ok = true;
if (entry1->keysym != entry2->keysym ||
!streq_null(entry1->utf8, entry2->utf8) ||
entry1->sequence_length != entry2->sequence_length)
ok = false;
for (size_t k = 0; k < entry1->sequence_length; k++) {
if (entry1->sequence[k] != entry2->sequence[k])
ok = false;
}
if (ok)
return true;
error:
#define print_entry(msg, entry) \
fprintf(stderr, msg); \
if (entry) \
print_compose_table_entry(stderr, entry); \
else \
fprintf(stderr, "\n");
print_entry("Expected: ", entry1);
print_entry("Got: ", entry2);
#undef print_entry
return false;
}

static void
test_traverse(struct xkb_context *ctx)
compose_traverse_fn(struct xkb_compose_table_entry *entry_ref, void *data)
{
struct xkb_compose_table_iterator *iter = (struct xkb_compose_table_iterator *)data;
struct xkb_compose_table_entry *entry = xkb_compose_table_iterator_next(iter);
assert(test_eq_entries(entry_ref, entry));
}

static void
test_traverse(struct xkb_context *ctx, size_t quickcheck_loops)
{
struct xkb_compose_table *table;
struct xkb_compose_table_iterator *iter;
@@ -796,6 +836,34 @@ test_traverse(struct xkb_context *ctx)

xkb_compose_table_iterator_free(iter);
xkb_compose_table_unref(table);

/* QuickCheck: shuffle compose file lines and compare against
* reference implementation */
char *input = test_read_file("locale/en_US.UTF-8/Compose");
assert(input);
struct text_line lines[6000];
size_t input_length = strlen(input);
size_t lines_count = split_lines(input, input_length, lines, ARRAY_SIZE(lines));
/* Note: we may add additional new line char */
char *shuffled = calloc(input_length + 1, sizeof(char));
assert(shuffled);
for (size_t k = 0; k < quickcheck_loops; k++) {
size_t shuffled_length = shuffle_lines(lines, lines_count, shuffled);
table = xkb_compose_table_new_from_buffer(ctx, shuffled, shuffled_length, "",
XKB_COMPOSE_FORMAT_TEXT_V1,
XKB_COMPOSE_COMPILE_NO_FLAGS);
assert(table);

iter = xkb_compose_table_iterator_new(table);
assert(iter);
xkb_compose_table_for_each(table, compose_traverse_fn, iter);
assert(xkb_compose_table_iterator_next(iter) == NULL);
xkb_compose_table_iterator_free(iter);

xkb_compose_table_unref(table);
}
free(shuffled);
free(input);
}

static void
@@ -938,6 +1006,15 @@ test_encode_escape_sequences(struct xkb_context *ctx)
# undef MAX_CODE_POINTS_COUNT
}

/* CLI positional arguments:
* 1. Seed for the pseudo-random generator:
* - Leave it unset or set it to “-” to use current time.
* - Use an integer to set it explicitly.
* 2. Number of quickcheck loops:
* - Leave it unset to use the default. It depends if the `RUNNING_VALGRIND`
* environment variable is set.
* - Use an integer to set it explicitly.
*/
int
main(int argc, char *argv[])
{
@@ -950,14 +1027,24 @@ main(int argc, char *argv[])

/* Initialize pseudo-random generator with program arg or current time */
int seed;
if (argc == 2) {
if (argc >= 2 && !streq(argv[1], "-")) {
seed = atoi(argv[1]);
} else {
seed = (int)time(NULL);
}
fprintf(stderr, "Seed for the pseudo-random generator: %d\n", seed);
srand(seed);

/* Determine number of loops for quickchecks */
size_t quickcheck_loops = 100; /* Default */
if (argc > 2) {
/* From command-line */
quickcheck_loops = (size_t)atoi(argv[2]);
} else if (getenv("RUNNING_VALGRIND") != NULL) {
/* Reduce if running Valgrind */
quickcheck_loops = quickcheck_loops / 20;
}

/*
* Ensure no environment variables but “top_srcdir” is set. This ensures
* that user Compose file paths are unset before the tests and set
@@ -985,7 +1072,7 @@ main(int argc, char *argv[])
test_modifier_syntax(ctx);
test_include(ctx);
test_override(ctx);
test_traverse(ctx);
test_traverse(ctx, quickcheck_loops);
test_string_length(ctx);
test_decode_escape_sequences(ctx);
test_encode_escape_sequences(ctx);
89 changes: 89 additions & 0 deletions test/shuffle-lines.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright © 2024 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 <stdlib.h>
#include <string.h>
#include "src/utils.h"
#include "test/shuffle-lines.h"

/* Split string into lines */
size_t
split_lines(const char *input, size_t input_length,
struct text_line *output, size_t output_length)
{
const char *start = input;
char *next;
size_t l;
size_t i = 0;

for (l = 0; i < input_length && l < output_length && *start != '\0'; l++) {
/* Look for newline character */
next = strchr(start, 0x0a);
output[l].start = start;
if (next == NULL) {
/* Not found: add the rest of the string */
output[l++].length = strlen(start);
break;
}
output[l].length = (size_t)(next - start) + 1;
start = next + 1;
i += output[l].length;
}
return l;
}

size_t
shuffle_lines(struct text_line *lines, size_t length, char *output)
{
/* Shuffle lines in-place using Fisher–Yates algorithm.
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle */

assert(length < RAND_MAX);
char *out = output;
if (length > 1) {
/* 1. Set the current i to the last line.
* 2. Take a random line j before the current line i.
* 3. Swap the lines i and j.
* 4. Append line i to the output.
* 5. If i is the first line, stop. Else decrease i and go to 2).
*/
for (size_t i = length - 1; i > 0; i--) {
/* Swap current line with random line before it */
size_t j = (size_t)(rand() % (i+1));
struct text_line tmp = lines[j];
lines[j] = lines[i];
lines[i] = tmp;
/* Append current line */
memcpy(out, lines[i].start, lines[i].length);
out += lines[i].length;
/* Ensure line ends with newline */
if (out[-1] != '\n') {
out[0] = '\n';
out++;
}
}
}
return (size_t)(out - output);
}
13 changes: 13 additions & 0 deletions test/shuffle-lines.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdint.h>

struct text_line {
const char *start;
size_t length;
};

size_t
split_lines(const char *input, size_t input_length,
struct text_line *output, size_t output_length);

size_t
shuffle_lines(struct text_line *lines, size_t length, char *output);