Skip to content

Commit 15f3e57

Browse files
committed
Move away from vector slowed :( : ~15s runtime
1 parent e37b471 commit 15f3e57

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

stencil.cpp

Lines changed: 14 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ using namespace std;
1010
struct stencil_type {
1111
int offset;
1212
int move;
13-
vector<short> cells;
13+
unsigned int length;
14+
unique_ptr<short[]> cells;
1415
};
1516

1617
static const int HEAPSIZE=30000;
@@ -28,7 +29,7 @@ static int STACK[STACKSIZE] = {};
2829

2930
stencil_type build_stencil() {
3031
stencil_type stencil;
31-
stencil.cells.push_back(0);
32+
vector<short> stencil_buffer(1,0);
3233
int cursor = 0;
3334
int left_offset = 0;
3435

@@ -39,21 +40,21 @@ stencil_type build_stencil() {
3940
if (cursor > 0) {
4041
--cursor;
4142
} else {
42-
stencil.cells.insert(stencil.cells.begin(), 0);
43+
stencil_buffer.insert(stencil_buffer.begin(), 0);
4344
++left_offset;
4445
}
4546
break;
4647
case '>':
4748
++cursor;
48-
if (cursor >= (int) (stencil.cells.size())) {
49-
stencil.cells.push_back(0);
49+
if (cursor >= (int) (stencil_buffer.size())) {
50+
stencil_buffer.push_back(0);
5051
}
5152
break;
5253
case '+':
53-
++stencil.cells[cursor];
54+
++stencil_buffer[cursor];
5455
break;
5556
case '-':
56-
--stencil.cells[cursor];
57+
--stencil_buffer[cursor];
5758
break;
5859
case '[':
5960
case ']':
@@ -64,25 +65,17 @@ stencil_type build_stencil() {
6465
}
6566
}
6667
exit_loop:
68+
ungetc(next_char, stdin);
69+
6770
stencil.move = cursor - left_offset;
6871
stencil.offset = left_offset;
69-
70-
ungetc(next_char, stdin);
72+
stencil.length = stencil_buffer.size();
73+
stencil.cells.reset(new short[stencil.length]());
74+
copy(stencil_buffer.begin(), stencil_buffer.end(), stencil.cells.get());
7175

7276
return stencil;
7377
}
7478

75-
unsigned char parse_run(char character) {
76-
unsigned char number = 1;
77-
char next_char;
78-
while ((next_char = getchar_unlocked()) == character &&
79-
number < 255) {
80-
++number;
81-
}
82-
ungetc(next_char, stdin);
83-
return number;
84-
}
85-
8679
void parse_and_compile() {
8780
int *stack_ptr = STACK;
8881
char input;
@@ -133,28 +126,12 @@ void run() {
133126
tmp_data_ptr = data_ptr - stencil->offset;
134127
data_ptr += stencil->move;
135128

136-
for (unsigned int i= 0; i < stencil->cells.size(); ++i) {
129+
for (unsigned int i= 0; i < stencil->length; ++i) {
137130
*tmp_data_ptr = (unsigned char) *tmp_data_ptr +
138131
stencil->cells[i];
139132
++tmp_data_ptr;
140133
}
141134
break;
142-
case '<':
143-
++code_ptr;
144-
data_ptr -= *code_ptr;
145-
break;
146-
case '>':
147-
++code_ptr;
148-
data_ptr += *code_ptr;
149-
break;
150-
case '+':
151-
++code_ptr;
152-
*data_ptr += *code_ptr;
153-
break;
154-
case '-':
155-
++code_ptr;
156-
*data_ptr -= *code_ptr;
157-
break;
158135
case '[':
159136
if (!*data_ptr) {
160137
code_ptr =

0 commit comments

Comments
 (0)