Skip to content

Commit 49e7f25

Browse files
committed
Small string optimization for evaluation
1 parent bf78b25 commit 49e7f25

File tree

8 files changed

+435
-113
lines changed

8 files changed

+435
-113
lines changed

include/lsp-plug.in/expr/Parameters.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
44
*
55
* This file is part of lsp-runtime-lib
66
* Created on: 19 февр. 2020 г.
@@ -37,9 +37,6 @@ namespace lsp
3737
*/
3838
class Parameters: public Resolver
3939
{
40-
private:
41-
Parameters & operator = (const Parameters &);
42-
4340
protected:
4441
typedef struct param_t
4542
{
@@ -65,6 +62,9 @@ namespace lsp
6562
status_t drop_value(size_t index, value_type_t type, param_t **out);
6663
status_t drop_value(const char *name, value_type_t type, param_t **out);
6764
status_t drop_value(const LSPString *name, value_type_t type, param_t **out);
65+
status_t add_move(const LSPString *name, value_t *value);
66+
status_t add_move(const char *name, value_t *value);
67+
status_t add_move(value_t *value);
6868

6969
protected:
7070
/**
@@ -76,8 +76,13 @@ namespace lsp
7676

7777
public:
7878
explicit Parameters();
79+
Parameters(const Parameters &) = delete;
80+
Parameters(Parameters &&) = delete;
7981
virtual ~Parameters();
8082

83+
Parameters & operator = (const Parameters &) = delete;
84+
Parameters & operator = (Parameters &&) = delete;
85+
8186
public:
8287
virtual status_t resolve(value_t *value, const char *name, size_t num_indexes = 0, const ssize_t *indexes = NULL);
8388

@@ -275,7 +280,7 @@ namespace lsp
275280
inline bool contains(const LSPString *name) const { return get_index(name) >= 0; }
276281
};
277282

278-
} /* namespace calc */
283+
} /* namespace expr */
279284
} /* namespace lsp */
280285

281286
#endif /* LSP_PLUG_IN_EXPR_PARAMETERS_H_ */

include/lsp-plug.in/expr/Variables.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2023 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2023 Vladimir Sadovnikov <sadko4u@gmail.com>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
44
*
55
* This file is part of lsp-runtime-lib
66
* Created on: 19 сент. 2019 г.
@@ -54,11 +54,14 @@ namespace lsp
5454

5555
protected:
5656
status_t insert_var(const LSPString *name, const value_t *value, size_t idx);
57+
status_t insert_var_move(const LSPString *name, value_t *value, size_t idx);
5758
ssize_t index_of_var(const LSPString *name);
5859

5960
status_t insert_func(const LSPString *name, function_t func, void *context, size_t idx);
6061
ssize_t index_of_func(const LSPString *name);
6162

63+
status_t set_move(const LSPString *name, value_t *value);
64+
6265
public:
6366
explicit Variables();
6467
explicit Variables(Resolver *r);

include/lsp-plug.in/expr/types.h

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,61 @@ namespace lsp
7171
*/
7272
void init_value(value_t *dst);
7373

74+
/**
75+
* Init value as NULL
76+
* @param dst target to set
77+
*/
78+
void init_value_null(value_t *dst);
79+
80+
/**
81+
* Init value as UNDEF
82+
* @param dst target to set
83+
*/
84+
void init_value_undef(value_t *dst);
85+
86+
/**
87+
* Init value as integer
88+
* @param dst target to set
89+
* @param value integer value to set
90+
*/
91+
void init_value_int(value_t *dst, ssize_t value);
92+
93+
/**
94+
* Init value as floating point
95+
* @param dst target to set
96+
* @param value floating-point value to set
97+
*/
98+
void init_value_float(value_t *dst, double value);
99+
100+
/**
101+
* Init value as boolean
102+
* @param dst target to set
103+
* @param value boolean value to set
104+
*/
105+
void init_value_bool(value_t *dst, bool value);
106+
107+
/**
108+
* Init value as string
109+
* @param dst target to set
110+
* @param value string value to set
111+
*/
112+
status_t init_value_string(value_t *dst, const LSPString *value);
113+
114+
/**
115+
* Init value as string
116+
* @param dst target to set
117+
* @param value UTF-8 string to set
118+
*/
119+
status_t init_value_string(value_t *dst, const char *value);
120+
121+
/**
122+
* Init value as string
123+
* @param dst target to set
124+
* @param value native string to set
125+
* @param charset native charset name
126+
*/
127+
status_t init_value_string(value_t *dst, const char *value, const char *charset);
128+
74129
/**
75130
* Initialize value with another value by copying contents
76131
* @param dst destination value
@@ -79,6 +134,14 @@ namespace lsp
79134
*/
80135
status_t init_value(value_t *dst, const value_t *src);
81136

137+
/**
138+
* Initialize value with move. Moves contents of source value to destination value
139+
* and resets source value to undef
140+
* @param dst destination value to perform copy
141+
* @param src source value to take data from
142+
*/
143+
void init_value_move(value_t *dst, value_t *src);
144+
82145
/**
83146
* Copy value. Frees previously used value if it was set
84147
* @param dst destination value to perform copy
@@ -87,6 +150,21 @@ namespace lsp
87150
*/
88151
status_t copy_value(value_t *dst, const value_t *src);
89152

153+
/**
154+
* Move value. Moves contents of source value to destination value
155+
* and resets source value to undef
156+
* @param dst destination value to perform copy
157+
* @param src source value to take data from
158+
*/
159+
void move_value(value_t *dst, value_t *src);
160+
161+
/**
162+
* Swap value. Swaps contents of source value and destination value
163+
* @param dst destination value to perform copy
164+
* @param src source value to take data from
165+
*/
166+
void swap_value(value_t *dst, value_t *src);
167+
90168
/**
91169
* Set value to NULL
92170
* @param dst target to set
@@ -125,7 +203,7 @@ namespace lsp
125203
* @param dst target to set
126204
* @param value string value to set
127205
*/
128-
status_t set_value_string(value_t *dst, LSPString *value);
206+
status_t set_value_string(value_t *dst, const LSPString *value);
129207

130208
/**
131209
* Set value to string
@@ -134,6 +212,15 @@ namespace lsp
134212
*/
135213
status_t set_value_string(value_t *dst, const char *value);
136214

215+
/**
216+
* Set value to string
217+
* @param dst target to set
218+
* @param value native string to set
219+
* @param charset character set to use
220+
*/
221+
status_t set_value_string(value_t *dst, const char *value, const char *charset);
222+
223+
137224
/**
138225
* Destroy value and all internal contents associated with it
139226
* The value remains valid and available for further operations but set to UNDEF

src/main/expr/Expression.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,13 +231,12 @@ namespace lsp
231231

232232
right->type = ET_VALUE;
233233
right->eval = eval_value;
234-
right->value.type = VT_STRING;
235-
right->value.v_str = str->clone();
236234

237-
if (right->value.v_str == NULL)
235+
status_t res = init_value_string(&right->value, str);
236+
if (res != STATUS_OK)
238237
{
239238
parse_destroy(right);
240-
return STATUS_NO_MEM;
239+
return res;
241240
}
242241

243242
// Do we need to immediately return?
@@ -393,9 +392,7 @@ namespace lsp
393392
else
394393
{
395394
root->expr = expr;
396-
init_value(&root->result);
397-
root->result.type = VT_UNDEF;
398-
root->result.v_str = NULL;
395+
init_value_undef(&root->result);
399396
}
400397

401398
return res;

src/main/expr/Parameters.cpp

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
2-
* Copyright (C) 2020 Linux Studio Plugins Project <https://lsp-plug.in/>
3-
* (C) 2020 Vladimir Sadovnikov <sadko4u@gmail.com>
2+
* Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3+
* (C) 2025 Vladimir Sadovnikov <sadko4u@gmail.com>
44
*
55
* This file is part of lsp-runtime-lib
66
* Created on: 19 февр. 2020 г.
@@ -373,6 +373,26 @@ namespace lsp
373373
return res;
374374
}
375375

376+
status_t Parameters::add_move(const LSPString *name, value_t *value)
377+
{
378+
if (name == NULL)
379+
return add(value);
380+
381+
param_t *p = allocate(name);
382+
if (p == NULL)
383+
return STATUS_NO_MEM;
384+
385+
init_value_move(&p->value, value);
386+
if (vParams.add(p))
387+
{
388+
modified();
389+
return STATUS_OK;
390+
}
391+
392+
destroy(p);
393+
return STATUS_NO_MEM;
394+
}
395+
376396
status_t Parameters::add(const char *name, const value_t *value)
377397
{
378398
if (name == NULL)
@@ -384,6 +404,17 @@ namespace lsp
384404
return add(&tmp, value);
385405
}
386406

407+
status_t Parameters::add_move(const char *name, value_t *value)
408+
{
409+
if (name == NULL)
410+
return add(value);
411+
412+
LSPString tmp;
413+
if (!tmp.set_utf8(name))
414+
return STATUS_NO_MEM;
415+
return add_move(&tmp, value);
416+
}
417+
387418
status_t Parameters::add(const value_t *value)
388419
{
389420
param_t *p = allocate();
@@ -405,6 +436,23 @@ namespace lsp
405436
return res;
406437
}
407438

439+
status_t Parameters::add_move(value_t *value)
440+
{
441+
param_t *p = allocate();
442+
if (p == NULL)
443+
return STATUS_NO_MEM;
444+
445+
init_value_move(&p->value, value);
446+
if (vParams.add(p))
447+
{
448+
modified();
449+
return STATUS_OK;
450+
}
451+
452+
destroy(p);
453+
return STATUS_NO_MEM;
454+
}
455+
408456
status_t Parameters::add_int(const char *name, ssize_t value)
409457
{
410458
value_t v;
@@ -434,14 +482,13 @@ namespace lsp
434482
if (value == NULL)
435483
return add_null(name);
436484

437-
LSPString s;
438-
if (!s.set_utf8(value))
439-
return STATUS_NO_MEM;
440-
441485
value_t v;
442-
v.type = VT_STRING;
443-
v.v_str = &s;
444-
return add(name, &v);
486+
status_t res = init_value_string(&v, name, value);
487+
if (res != STATUS_OK)
488+
return res;
489+
lsp_finally { destroy_value(&v); };
490+
491+
return add_move(name, &v);
445492
}
446493

447494
status_t Parameters::add_string(const char *name, const LSPString *value)
@@ -1951,5 +1998,5 @@ namespace lsp
19511998
return STATUS_OK;
19521999
}
19532000

1954-
} /* namespace calc */
2001+
} /* namespace expr */
19552002
} /* namespace lsp */

0 commit comments

Comments
 (0)