mirror of
https://git.robbyzambito.me/zits
synced 2026-02-04 03:34:48 +00:00
Optimize PUB and HPUB parsing
This will try to take better advantage of the buffered reading. Instead of pushing one byte at a time to the array list for each section, find the end index for each section, then alloc the arraylist and copy the data into it all at once.
This commit is contained in:
@@ -404,12 +404,14 @@ fn parseSub(alloc: Allocator, in: *Reader) !Message {
|
|||||||
continue :sw .in_second;
|
continue :sw .in_second;
|
||||||
},
|
},
|
||||||
.in_second => {
|
.in_second => {
|
||||||
const byte = try in.peekByte();
|
for (1..in.buffer.len) |i| {
|
||||||
if (!isWhitespace(byte)) {
|
try in.fill(i + 1);
|
||||||
try second.append(alloc, byte);
|
if (isWhitespace(in.buffered()[i])) {
|
||||||
in.toss(1);
|
@memcpy(try second.addManyAsSlice(alloc, i), in.buffered()[0..i]);
|
||||||
continue :sw .in_second;
|
in.toss(i);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
} else return error.EndOfStream;
|
||||||
continue :sw .after_second;
|
continue :sw .after_second;
|
||||||
},
|
},
|
||||||
.after_second => {
|
.after_second => {
|
||||||
@@ -424,13 +426,15 @@ fn parseSub(alloc: Allocator, in: *Reader) !Message {
|
|||||||
continue :sw .in_third;
|
continue :sw .in_third;
|
||||||
},
|
},
|
||||||
.in_third => {
|
.in_third => {
|
||||||
const byte = try in.peekByte();
|
for (1..in.buffer.len) |i| {
|
||||||
if (byte == '\r') {
|
try in.fill(i + 1);
|
||||||
continue :sw .in_end;
|
if (isWhitespace(in.buffered()[i])) {
|
||||||
}
|
@memcpy(try third.?.addManyAsSlice(alloc, i), in.buffered()[0..i]);
|
||||||
try third.?.append(alloc, byte);
|
in.toss(i);
|
||||||
in.toss(1);
|
break;
|
||||||
continue :sw .in_third;
|
}
|
||||||
|
} else return error.EndOfStream;
|
||||||
|
continue :sw .in_end;
|
||||||
},
|
},
|
||||||
.in_end => {
|
.in_end => {
|
||||||
try expectStreamBytes(in, "\r\n");
|
try expectStreamBytes(in, "\r\n");
|
||||||
@@ -674,12 +678,14 @@ fn parsePub(alloc: Allocator, in: *Reader) !Message {
|
|||||||
continue :sw .in_second;
|
continue :sw .in_second;
|
||||||
},
|
},
|
||||||
.in_second => {
|
.in_second => {
|
||||||
const byte = try in.peekByte();
|
for (1..in.buffer.len) |i| {
|
||||||
if (!isWhitespace(byte)) {
|
try in.fill(i + 1);
|
||||||
try second.append(alloc, byte);
|
if (isWhitespace(in.buffered()[i])) {
|
||||||
in.toss(1);
|
@memcpy(try second.addManyAsSlice(alloc, i), in.buffered()[0..i]);
|
||||||
continue :sw .in_second;
|
in.toss(i);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
} else return error.EndOfStream;
|
||||||
continue :sw .after_second;
|
continue :sw .after_second;
|
||||||
},
|
},
|
||||||
.after_second => {
|
.after_second => {
|
||||||
@@ -694,15 +700,15 @@ fn parsePub(alloc: Allocator, in: *Reader) !Message {
|
|||||||
continue :sw .in_third;
|
continue :sw .in_third;
|
||||||
},
|
},
|
||||||
.in_third => {
|
.in_third => {
|
||||||
const byte = try in.peekByte();
|
for (1..in.buffer.len) |i| {
|
||||||
if (byte == '\r') {
|
try in.fill(i + 1);
|
||||||
continue :sw .in_end;
|
if (isWhitespace(in.buffered()[i])) {
|
||||||
} else if (isDigit(byte)) {
|
@memcpy(try third.?.addManyAsSlice(alloc, i), in.buffered()[0..i]);
|
||||||
try third.?.append(alloc, byte);
|
in.toss(i);
|
||||||
in.toss(1);
|
break;
|
||||||
continue :sw .in_third;
|
}
|
||||||
}
|
} else return error.EndOfStream;
|
||||||
return error.InvalidStream;
|
continue :sw .in_end;
|
||||||
},
|
},
|
||||||
.in_end => {
|
.in_end => {
|
||||||
try expectStreamBytes(in, "\r\n");
|
try expectStreamBytes(in, "\r\n");
|
||||||
@@ -847,12 +853,14 @@ fn parseHPub(alloc: Allocator, in: *Reader) !Message {
|
|||||||
continue :sw .in_second;
|
continue :sw .in_second;
|
||||||
},
|
},
|
||||||
.in_second => {
|
.in_second => {
|
||||||
const byte = try in.peekByte();
|
for (1..in.buffer.len) |i| {
|
||||||
if (!isWhitespace(byte)) {
|
try in.fill(i + 1);
|
||||||
try second.append(alloc, byte);
|
if (isWhitespace(in.buffered()[i])) {
|
||||||
in.toss(1);
|
@memcpy(try second.addManyAsSlice(alloc, i), in.buffered()[0..i]);
|
||||||
continue :sw .in_second;
|
in.toss(i);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
} else return error.EndOfStream;
|
||||||
continue :sw .after_second;
|
continue :sw .after_second;
|
||||||
},
|
},
|
||||||
.after_second => {
|
.after_second => {
|
||||||
@@ -867,12 +875,14 @@ fn parseHPub(alloc: Allocator, in: *Reader) !Message {
|
|||||||
continue :sw .in_third;
|
continue :sw .in_third;
|
||||||
},
|
},
|
||||||
.in_third => {
|
.in_third => {
|
||||||
const byte = try in.peekByte();
|
for (1..in.buffer.len) |i| {
|
||||||
if (!isWhitespace(byte)) {
|
try in.fill(i + 1);
|
||||||
try third.append(alloc, byte);
|
if (isWhitespace(in.buffered()[i])) {
|
||||||
in.toss(1);
|
@memcpy(try third.addManyAsSlice(alloc, i), in.buffered()[0..i]);
|
||||||
continue :sw .in_third;
|
in.toss(i);
|
||||||
}
|
break;
|
||||||
|
}
|
||||||
|
} else return error.EndOfStream;
|
||||||
continue :sw .after_third;
|
continue :sw .after_third;
|
||||||
},
|
},
|
||||||
.after_third => {
|
.after_third => {
|
||||||
@@ -887,15 +897,15 @@ fn parseHPub(alloc: Allocator, in: *Reader) !Message {
|
|||||||
continue :sw .in_fourth;
|
continue :sw .in_fourth;
|
||||||
},
|
},
|
||||||
.in_fourth => {
|
.in_fourth => {
|
||||||
const byte = try in.peekByte();
|
for (1..in.buffer.len) |i| {
|
||||||
if (byte == '\r') {
|
try in.fill(i + 1);
|
||||||
continue :sw .in_end;
|
if (isWhitespace(in.buffered()[i])) {
|
||||||
} else if (isDigit(byte)) {
|
@memcpy(try fourth.?.addManyAsSlice(alloc, i), in.buffered()[0..i]);
|
||||||
try fourth.?.append(alloc, byte);
|
in.toss(i);
|
||||||
in.toss(1);
|
break;
|
||||||
continue :sw .in_fourth;
|
}
|
||||||
}
|
} else return error.EndOfStream;
|
||||||
return error.InvalidStream;
|
continue :sw .in_end;
|
||||||
},
|
},
|
||||||
.in_end => {
|
.in_end => {
|
||||||
try expectStreamBytes(in, "\r\n");
|
try expectStreamBytes(in, "\r\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user