Skip to content

Commit d973a8d

Browse files
committed
lib: replace array iterator with for loop in match_string function
array iterators require quite a lot of allocations for some reason, so just use a plain old for loop, which does exactly the same and requires zero allocations.
1 parent 2816667 commit d973a8d

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

lib/plist_match.c

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -100,19 +100,14 @@ xbps_match_any_virtualpkg_in_rundeps(xbps_array_t rundeps,
100100
static bool
101101
match_string_in_array(xbps_array_t array, const char *str, int mode)
102102
{
103-
xbps_object_iterator_t iter;
104-
xbps_object_t obj;
105-
const char *pkgdep;
106103
char pkgname[XBPS_NAME_SIZE];
107104
bool found = false;
108105

109106
assert(xbps_object_type(array) == XBPS_TYPE_ARRAY);
110107
assert(str != NULL);
111108

112-
iter = xbps_array_iterator(array);
113-
assert(iter);
114-
115-
while ((obj = xbps_object_iterator_next(iter))) {
109+
for (unsigned int i = 0; i < xbps_array_count(array); i++) {
110+
xbps_object_t obj = xbps_array_get(array, i);
116111
if (mode == 0) {
117112
/* match by string */
118113
if (xbps_string_equals_cstring(obj, str)) {
@@ -121,7 +116,7 @@ match_string_in_array(xbps_array_t array, const char *str, int mode)
121116
}
122117
} else if (mode == 1) {
123118
/* match by pkgname against pkgver */
124-
pkgdep = xbps_string_cstring_nocopy(obj);
119+
const char *pkgdep = xbps_string_cstring_nocopy(obj);
125120
if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, pkgdep))
126121
break;
127122
if (strcmp(pkgname, str) == 0) {
@@ -130,7 +125,7 @@ match_string_in_array(xbps_array_t array, const char *str, int mode)
130125
}
131126
} else if (mode == 2) {
132127
/* match by pkgver against pkgname */
133-
pkgdep = xbps_string_cstring_nocopy(obj);
128+
const char *pkgdep = xbps_string_cstring_nocopy(obj);
134129
if (!xbps_pkg_name(pkgname, XBPS_NAME_SIZE, str))
135130
break;
136131
if (strcmp(pkgname, pkgdep) == 0) {
@@ -139,21 +134,20 @@ match_string_in_array(xbps_array_t array, const char *str, int mode)
139134
}
140135
} else if (mode == 3) {
141136
/* match pkgpattern against pkgdep */
142-
pkgdep = xbps_string_cstring_nocopy(obj);
137+
const char *pkgdep = xbps_string_cstring_nocopy(obj);
143138
if (xbps_pkgpattern_match(pkgdep, str)) {
144139
found = true;
145140
break;
146141
}
147142
} else if (mode == 4) {
148143
/* match pkgdep against pkgpattern */
149-
pkgdep = xbps_string_cstring_nocopy(obj);
144+
const char *pkgdep = xbps_string_cstring_nocopy(obj);
150145
if (xbps_pkgpattern_match(str, pkgdep)) {
151146
found = true;
152147
break;
153148
}
154149
}
155150
}
156-
xbps_object_iterator_release(iter);
157151

158152
return found;
159153
}

0 commit comments

Comments
 (0)