Skip to content

Commit 77accd5

Browse files
committed
context OPTIMIZE do not print context dictionary
1 parent eac9eeb commit 77accd5

File tree

2 files changed

+16
-56
lines changed

2 files changed

+16
-56
lines changed

src/context.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -702,9 +702,6 @@ LIBYANG_API_DECL int ly_ctx_compiled_size(const struct ly_ctx *ctx);
702702
/**
703703
* @brief Print (serialize) a compiled context (without any parsed modules) into a pre-allocated memory chunk.
704704
*
705-
* Context's dictionary contains strings from parsed modules. You should call ::ly_ctx_free_parsed() to free the parsed modules
706-
* before printing the context, otherwise the printed context may contain many strings that will never be used.
707-
*
708705
* @param[in] ctx Compiled context to print.
709706
* @param[in] mem Memory to print to, must be large enough.
710707
* @param[out] mem_end Optional pointer after the printed context.

src/printer_context.c

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @author Michal Vasko <[email protected]>
44
* @brief Compiled context printer
55
*
6-
* Copyright (c) 2024 CESNET, z.s.p.o.
6+
* Copyright (c) 2024 - 2025 CESNET, z.s.p.o.
77
*
88
* This source code is licensed under BSD 3-Clause License (the "License").
99
* You may not use this file except in compliance with the License.
@@ -86,21 +86,12 @@
8686
#define CTXP_MEM_SIZE(SIZE) ((SIZE) + ((~(SIZE) + 1) & (CTXP_MEM_ALIGN - 1)))
8787

8888
static void
89-
ctxs_dict_ht(const struct ly_ht *ht, int *size)
89+
ctxs_dict_strings(const struct ly_ht *ht, int *size)
9090
{
9191
uint32_t i, j;
9292
struct ly_ht_rec *rec;
9393
struct ly_dict_rec *dict_rec;
9494

95-
/* hash table */
96-
*size += CTXP_MEM_SIZE(sizeof *ht);
97-
98-
/* hlists */
99-
*size += CTXP_MEM_SIZE(ht->size * sizeof *ht->hlists);
100-
101-
/* records (with string pointers) */
102-
*size += CTXP_MEM_SIZE(ht->size * ht->rec_size);
103-
10495
LYHT_ITER_ALL_RECS(ht, i, j, rec) {
10596
dict_rec = (struct ly_dict_rec *)&rec->val;
10697

@@ -567,8 +558,8 @@ ly_ctx_compiled_size_context(const struct ly_ctx *ctx, struct ly_ht *addr_ht, in
567558
/* context */
568559
*size += CTXP_MEM_SIZE(sizeof *ctx);
569560

570-
/* dictionary ht (with all the strings) */
571-
ctxs_dict_ht(ctx->dict.hash_tab, size);
561+
/* all the strings in the dictionary */
562+
ctxs_dict_strings(ctx->dict.hash_tab, size);
572563

573564
/* module set */
574565
*size += CTXP_MEM_SIZE(ctx->modules.count * sizeof ctx->modules.objs);
@@ -718,17 +709,6 @@ ly_ctx_compiled_ext_stmts_storage_size(const struct lysc_ext_substmt *substmts,
718709
static void ctxp_node(const struct lysc_node *orig_node, struct lysc_node *node, struct ly_ht *addr_ht,
719710
struct ly_set *ptr_set, void **mem);
720711

721-
static void
722-
ctxp_mutex(pthread_mutex_t *mutex)
723-
{
724-
pthread_mutexattr_t attr;
725-
726-
pthread_mutexattr_init(&attr);
727-
pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
728-
pthread_mutex_init(mutex, &attr);
729-
pthread_mutexattr_destroy(&attr);
730-
}
731-
732712
static void
733713
ctxp_set(const struct ly_set *orig_set, struct ly_set *set, void **mem)
734714
{
@@ -739,45 +719,29 @@ ctxp_set(const struct ly_set *orig_set, struct ly_set *set, void **mem)
739719
}
740720

741721
static void
742-
ctxp_dict_ht(const struct ly_ht *orig_ht, struct ly_ht *ht, struct ly_ht *addr_ht, void **mem)
722+
ctxp_dict_strings(const struct ly_ht *orig_dict, struct ly_ht *addr_ht, void **mem)
743723
{
744724
uint32_t i, j;
745725
struct ly_ht_rec *rec;
746726
struct ly_dict_rec *dict_rec;
747727
int len;
748-
void *orig_addr;
749-
750-
/* hash table, must not be modified in the context */
751-
*ht = *orig_ht;
752-
ht->val_equal = orig_ht->val_equal;
753-
ht->cb_data = NULL;
754-
755-
/* hlists */
756-
ht->hlists = *mem;
757-
*mem = (char *)*mem + CTXP_MEM_SIZE(ht->size * sizeof *ht->hlists);
758-
759-
memcpy(ht->hlists, orig_ht->hlists, ht->size * sizeof *ht->hlists);
728+
void *orig_str;
729+
char *str;
760730

761-
/* records */
762-
ht->recs = *mem;
763-
*mem = (char *)*mem + CTXP_MEM_SIZE(ht->size * ht->rec_size);
764-
765-
memcpy(ht->recs, orig_ht->recs, ht->size * ht->rec_size);
766-
767-
LYHT_ITER_ALL_RECS(ht, i, j, rec) {
731+
LYHT_ITER_ALL_RECS(orig_dict, i, j, rec) {
768732
dict_rec = (struct ly_dict_rec *)&rec->val;
769733

770734
/* strings */
771735
len = strlen(dict_rec->value) + 1;
772-
orig_addr = dict_rec->value;
736+
orig_str = dict_rec->value;
773737

774-
dict_rec->value = *mem;
738+
str = *mem;
775739
*mem = (char *)*mem + CTXP_MEM_SIZE(len);
776740

777-
memcpy(dict_rec->value, orig_addr, len);
741+
memcpy(str, orig_str, len);
778742

779743
/* shared */
780-
ly_ctx_compiled_addr_ht_add(addr_ht, orig_addr, dict_rec->value);
744+
ly_ctx_compiled_addr_ht_add(addr_ht, orig_str, str);
781745
}
782746
}
783747

@@ -1752,12 +1716,11 @@ ly_ctx_compiled_print_context(const struct ly_ctx *orig_ctx, struct ly_ctx *ctx,
17521716
/* set options so they can be read by other functions */
17531717
ctx->opts = orig_ctx->opts | LY_CTX_INT_IMMUTABLE;
17541718

1755-
/* dictionary */
1756-
ctx->dict.hash_tab = *mem;
1757-
*mem = (char *)*mem + CTXP_MEM_SIZE(sizeof *ctx->dict.hash_tab);
1719+
/* strings from the dictionary */
1720+
ctxp_dict_strings(orig_ctx->dict.hash_tab, addr_ht, mem);
17581721

1759-
ctxp_dict_ht(orig_ctx->dict.hash_tab, ctx->dict.hash_tab, addr_ht, mem);
1760-
ctxp_mutex(&ctx->dict.lock);
1722+
/* dictionary, unused */
1723+
memset(&ctx->dict, 0, sizeof ctx->dict);
17611724

17621725
/* no search paths */
17631726
memset(&ctx->search_paths, 0, sizeof ctx->search_paths);

0 commit comments

Comments
 (0)