CS144-小计

CS144 小计

TLDR

对不起汤老师,我应该好好上 OOP 课的(哭

Assign1

实现 Reassembler 用于接受乱序包。

这里使用 ODT 实现。

冷知识:文档里的一些接口定义是错的,要看具体代码里的(看 insert 一头雾水)。

语法

Bytestream 的定义上给出了一个代码:

// Please add any additional state to the ByteStream here, and not to the Writer and Reader interfaces.
...
// Helper functions (provided) to access the ByteStream's Reader and Writer interfaces
Reader& reader();
const Reader& reader() const;
Writer& writer();
const Writer& writer() const;

其中 Reader 和 Writer 是其继承。注意第一行的注释,不要在 Reader 和 Writer 内写状态定义。

将一个基类强行提升到子类是 ub,定义一个 bytestream 时其只分配了这么多的内存,如果 Reader/Writer 内再定义内容,强行提升后访问直接造成越界。

这里的用处在于不同的接口,Reader 和 Writer 内只有函数,用于在不同视角操纵 stream 的内容,所以是可行的。

例如可以这么写:

ByteStream stream(1024);
Writer& w = stream.writer();
w.push("hello");
w.close();

ODT

具体而言,Reassembler 可以使用 map<uint, string> 的格式存储一个下标下往后的区间字符串,其实质就是一个平衡树,用于在对数时间内定位到插入位置。

加入一个字符串时,可以获取和其重叠位置,完全覆盖的直接删除,将迭代器往后挪动就可以。

调优

先前的 Bytestream 使用循环字符串,循环逐位编写,导致只有 1.x GBits/s,事实上 memcpy 或者 replace 等连续拷贝会比这个快很多(即便在编译优化下也是),使用后直接飙到 7-10 Gbits/s。

对于环形拷贝,直接拷贝两遍即可,根据其给出的 modern Cpp 标准,不要做裸指针移动,memcpy 就用不了了,用 replace,其支持对 src 和 dst 都做区间约束。

posted @ 2026-09-30 14:10  蒻蒻虫  阅读(5)  评论(0)    收藏  举报