1 个不稳定版本
使用旧的 Rust 2015
0.0.1 | 2015年1月2日 |
---|
#4 in #解压缩器
2MB
40K SLoC
包含 (模糊的 autoconf 代码, 1KB) orig/contrib/minizip/configure.ac
zlib
这是将知名 zlib 库不完全移植到 Rust 语言的示例。
移植 zlib 到 Rust 的原因有几个
* To provide an implementation of a useful, common library. zlib is used in many
applications and libraries.
* To provide a real-world example of a performance-sensitive component, as a way
of validating the performance objectives of Rust. Rust aims to provide a safe
and efficient programming language. If a version of zlib can be built that is
safe and efficient, then the community gains a safe, efficient zlib and Rust
gains a new piece of real-world example code.
功能
第一个目标(功能)已基本实现。解压缩器已工作,尽管它有一些限制。解压缩器可以两种方式使用。首先,您可以直接使用它,通过创建一个 Inflater
结构的实例。该 Inflater
结构提供与 zlib 的 inflate()
API 非常相似的 API。主要区别在于 Inflater
使用切片而不是原始指针。此外,已完全删除 z_stream
抽象。使用 Inflater
的应用程序只需调用 Inflater::inflate()
,直到解压缩所有数据。
您还可以使用 InflateReader
结构。这个结构实现了 Reader
特性,因此您可以将 zlib 解压缩器轻松地插入到基于 Reader
的代码管道中。
性能
性能目标尚未实现。原因有几个
* This is brand-new code. I mean the ported code is new; obviously zlib has been
around for a very long time. But this code is new, and I have not yet begun to
do any serious performance optimization. The C-based zlib has been optimized
over many years.
* Rust does not support `goto`. The C zlib relies heavily on `goto` to implement
an efficient resumable state machine. To port this code, I used an `enum` to
simulate the `goto`-based state machine. This causes direct jump instructions
to be replaced with variable stores / loads and an indirect table-based job
(if you're lucky) or an if/else ladder (if you're not lucky). I speculate that
this is the source of the main difference in performance.
* Bounds checking. Bounds checking *can* be done efficiently, mainly by hoisting
bounds checks above loops. That is, a well-written inner loop can provide
enough information to a compiler to allow the compiler to perform a single
bounds check at the start of the loop, rather than checking bounds on every
iteration of a loop. (Microsoft's C# / CLR does a decent job on bounds-check
elimination and hoisting, for example.) Bounds-check elimination and hoisting
in Rust/LLVM is weak to non-existent. It is a known deficiency in Rust, and it
is one that will certainly be addressed in time. LLVM evolved to support the
needs of languages that do not require bounds checks (such as C++); LLVM and
Rust will need to implement existing well-known algorithsm for bounds-check
elimination and hoisting in order to provide competitive performance.
* Goofs on my part during the port. It is entirely possible that I broke
something that affects performance when I ported the code from C to Rust.
* Miscellaneous bad code-gen from Rust. Rust is a new language; it will take
time for Rust to reach the same level of high-quality code generation as
in existing systems programming languages, such as C/C++. I have confidence
that Rust will get there. In fact, the purpose of this experiment with
porting zlib is to provide a useful piece of code for optimizing Rust *itself*.
许可证
zlib 是一个知名、广泛使用的开源算法。我特此将我关于将 zlib 移植到 Rust 的工作贡献给开源社区,并使用宽松的 MIT 许可证。我对 zlib 本身没有任何权利要求!我只是将其移植到 Rust。此外,我放弃对 Rust 移植的任何保修。如果您将其用于任何目的,那么您将自行承担风险。我相信此代码是 C 代码的忠实移植,但同时我仅将其作为副项目进行。
这是原始 zlib README 文件中的版权声明
(C) 1995-2013 Jean-loup Gailly 和 Mark Adler
本软件“原样提供”,不提供任何明示或暗示的保证。在任何情况下,作者均不对因使用本软件而引起的任何损害承担责任。
任何人都可以出于任何目的使用此软件,包括商业应用,并可自由修改和重新分发,但需遵守以下限制:
- 此软件的来源不得被错误地表示;不得声称您编写了原始软件。如果您在产品中使用此软件,产品文档中应有致谢,但不是必需的。
- 修改后的源代码版本必须明显标记为修改版,并且不得将其误认为是原始软件。
- 本声明不得从任何源分发中删除或修改。
Jean-loup Gailly Mark Adler [email protected] [email protected]
(Rust端口的作者)授予每个人使用zlib Rust端口的类似权利,并且我也同样免除因使用此软件而产生的任何损害赔偿责任。
反馈
欢迎反馈!请通过GitHub联系我 https://github.com/sivadeilra。此外,如果您在此代码中发现任何错误/问题,请简单地通过GitHub在 https://github.com/sivadeilra/zlib 上提交一个issue。
如果您反馈说“哇,这太慢了!”——是的,我已经知道这一点了。如果您对性能分析和改进感兴趣,请随时联系我。如果您只是想告诉我这项工作很糟糕,我一开始就不应该费心——那就请自己保留,谢谢。