Skip to content

Commit eb5c74c

Browse files
committed
Use std.array.appender if possible
1 parent b67b70d commit eb5c74c

File tree

2 files changed

+24
-16
lines changed

2 files changed

+24
-16
lines changed

source/binary/mod.d

+21-14
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import binary.section;
77
import binary.types;
88

99
import std.algorithm : each;
10+
import std.array : appender, array;
1011
import std.bitmanip : read;
1112
import std.exception : enforce;
1213
import std.range : front, popFront, popFrontExactly;
@@ -118,13 +119,13 @@ Limits decodeLimits(ref const(ubyte)[] input)
118119
///
119120
const(Instruction)[] decodeExpr(ref const(ubyte)[] input)
120121
{
121-
typeof(return) insns = [];
122+
auto insns = appender!(const(Instruction)[]);
122123
while (true)
123124
{
124125
if (OpCode.End == input.front)
125126
{
126127
input.popFront();
127-
return insns;
128+
return insns.array;
128129
}
129130
insns ~= input.decodeInstruction();
130131
}
@@ -135,7 +136,8 @@ const(Instruction)[] decodeExpr(ref const(ubyte)[] input)
135136
const(Data)[] decodeDataSection(ref const(ubyte)[] input)
136137
{
137138
const count = input.leb128!uint();
138-
const(Data)[] data;
139+
auto data = appender!(const(Data)[]);
140+
data.reserve(count);
139141
foreach (_; 0..count)
140142
{
141143
const memoryIndex = input.leb128!uint();
@@ -149,7 +151,7 @@ const(Data)[] decodeDataSection(ref const(ubyte)[] input)
149151
bytes: bytes
150152
);
151153
}
152-
return data;
154+
return data.array;
153155
}
154156

155157
///
@@ -161,8 +163,9 @@ ValueType decodeValueSection(ref const(ubyte)[] input)
161163
///
162164
const(FuncType)[] decodeTypeSection(ref const(ubyte)[] input)
163165
{
164-
const(FuncType)[] funcTypes;
166+
auto funcTypes = appender!(const(FuncType)[]);
165167
const count = input.leb128!uint();
168+
funcTypes.reserve(count);
166169
foreach (_; 0..count)
167170
{
168171
input.read!ubyte();
@@ -174,33 +177,35 @@ const(FuncType)[] decodeTypeSection(ref const(ubyte)[] input)
174177
input.popFrontExactly(size);
175178
funcTypes ~= FuncType(params: params, results: results);
176179
}
177-
return funcTypes;
180+
return funcTypes.array;
178181
}
179182

180183
///
181184
const(uint)[] decodeFunctionSection(ref const(ubyte)[] input)
182185
{
183-
const(uint)[] funcIdxList;
186+
auto funcIdxList = appender!(const(uint)[]);
184187
const count = input.leb128!uint();
188+
funcIdxList.reserve(count);
185189
foreach (_; 0..count)
186190
{
187191
const idx = input.leb128!uint();
188192
funcIdxList ~= idx;
189193
}
190-
return funcIdxList;
194+
return funcIdxList.array;
191195
}
192196

193197
///
194198
Function[] decodeCodeSection(ref const(ubyte)[] input)
195199
{
196-
Function[] functions;
200+
auto functions = appender!(Function[]);
197201
const count = input.leb128!uint();
202+
functions.reserve(count);
198203
foreach (_; 0..count)
199204
{
200205
auto size = input.leb128!uint(); // func body size
201206
functions ~= input.decodeFunctionBody(size);
202207
}
203-
return functions;
208+
return functions.array;
204209
}
205210

206211
///
@@ -302,8 +307,9 @@ Block decodeBlockSection(ref const(ubyte)[] input, ref uint remaining)
302307
///
303308
const(Export)[] decodeExportSection(ref const(ubyte)[] input)
304309
{
310+
auto exports = appender!(const(Export)[]);
305311
const count = input.leb128!uint();
306-
const(Export)[] exports;
312+
exports.reserve(count);
307313
foreach (_; 0..count)
308314
{
309315
const name = input.decodeName();
@@ -313,14 +319,15 @@ const(Export)[] decodeExportSection(ref const(ubyte)[] input)
313319
ExportDesc desc = Func(idx);
314320
exports ~= Export(name: name, desc: desc);
315321
}
316-
return exports;
322+
return exports.array;
317323
}
318324

319325
///
320326
const(Import)[] decodeImportSection(ref const(ubyte)[] input)
321327
{
328+
auto imports = appender!(const(Import)[]);
322329
const count = input.leb128!uint();
323-
const(Import)[] imports;
330+
imports.reserve(count);
324331
foreach (_; 0..count)
325332
{
326333
const moduleName = input.decodeName();
@@ -335,7 +342,7 @@ const(Import)[] decodeImportSection(ref const(ubyte)[] input)
335342
desc: desc
336343
);
337344
}
338-
return imports;
345+
return imports.array;
339346
}
340347

341348
string decodeName(ref const(ubyte)[] input)

source/execution/store.d

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import binary.instruction;
44
import binary.mod;
55
import binary.types;
66

7+
import std.array : appender, array;
78
import std.bitmanip : write;
89
import std.exception : enforce;
910
import std.range : zip;
@@ -101,7 +102,7 @@ struct Store
101102
foreach (body, idx; mod.codeSection.zip(funcTypeIdxs))
102103
{
103104
const funcType = mod.typeSection[idx];
104-
const(ValueType)[] locals;
105+
auto locals = appender!(const(ValueType)[]);
105106
foreach (local; body.locals)
106107
{
107108
foreach (_; 0..local.typeCount)
@@ -112,7 +113,7 @@ struct Store
112113
funcs ~= FuncInst(InternalFuncInst(
113114
funcType: funcType,
114115
code: Func(
115-
locals: locals,
116+
locals: locals.array,
116117
body: body.code
117118
)
118119
));

0 commit comments

Comments
 (0)