为Windows编写超薄的c++程序(如uTorrent)

时间:2021-09-02 17:17:53

I've always admired the original uTorrent program. It looked great, was less than 64kb, was extremely fast and had all the features I needed. Unfortunately the program is closed source (and becoming more bloated by the day) so I come to * for inspiration.

我一直很欣赏最初的uTorrent格式。它看起来很棒,不到64kb,速度非常快,拥有我需要的所有功能。不幸的是,这个程序是封闭的(而且越来越臃肿),所以我来到*寻求灵感。

What methods do you recommend in writing fast, memory efficient and elegant programs on Windows?

你有什么方法可以在Windows上快速、高效、优雅地编写程序吗?

While C# (and the whole .NET concept) are cool ideas I am more interested in 'purist' answers and the challenge of writing efficient, fast software for the Windows platform, much like the original uTorrent client. I don't mind allocating my own memory, doing my own garbage collection and creating my own data structures.

虽然c#(和整个。net概念)是很酷的想法,但我更感兴趣的是“纯粹”的答案,以及为Windows平台编写高效、快速的软件的挑战,就像最初的uTorrent客户端一样。我不介意分配我自己的内存,做我自己的垃圾收集和创建我自己的数据结构。

Recommendations on books, articles, libraries, IDEs (even efficient ways of getting more caffeine into my system) welcome.

关于书籍、文章、图书馆、ide(甚至是让更多咖啡因进入我的身体的有效方法)的推荐值得欢迎。

7 个解决方案

#1


17  

The Windows Template Library is geared towards what you want to do. It's a light-weight, template-based C++ wrapper for the Win32 API. With it, you don't have to go through the pain of direct Win32 coding, but it doesn't add a lot of overhead like MFC.

Windows模板库针对的是您想要做的事情。它是Win32 API的一个轻量级的、基于模板的c++包装器。使用它,您不必经历直接Win32编码的痛苦,但是它不像MFC那样增加很多开销。

#2


10  

uTorrent is written in C++ and uses the old fashioned Win32 API. Google Chrome is also written this way, so why not download the source code and learn from their code?

uTorrent是用c++编写的,使用的是老式的Win32 API。谷歌Chrome也是这样写的,所以为什么不下载源代码并从他们的代码中学习呢?

#3


8  

If you want to optimize for the smallest possible memory footprint and you don't mind jumping through a bunch of hoops that the .NET CLR was invented to take care of for you, then writing a direct Win32API app and hooking to GDI+ is the way to go. Petzold was the definitive reference.

如果您想要优化尽可能小的内存占用,并且您不介意跳过。net CLR为您设计的一系列限制,那么编写一个直接的Win32API应用程序并连接到GDI+就是一个不错的选择。佩佐德是最权威的参考。

Really, though, it's sort of a fool's errand, since the .NET runtime is going to be loaded into the OS's memory whether your app uses it or not, so you may as well link to it.

实际上,这是一种愚蠢的差事,因为。net运行时将被加载到操作系统的内存中,不管你的应用程序是否使用它,所以你也可以链接到它。

#4


7  

The Demo Scene is a group of people who spend their free time trying to make impressive and very small executables, which usually render something in 3d to music. Often the entire demo (code, music, 3d data) compiles into a single executable that is compressed to 64k or an impressively small size for the content.

演示场景是一群人,他们利用空闲时间尝试制作令人印象深刻的、非常小的可执行文件,这些文件通常以3d形式呈现给音乐。通常整个演示(代码、音乐、3d数据)编译成单个可执行文件,压缩到64k,或者内容非常小。

You might draw some inspiration from the demos and learning about how they are made will inform your obsession to create small executables.

您可能会从演示中获得一些灵感,了解它们是如何制作的,这将有助于您创建小型可执行文件。

Often, the key is to leverage as many 3rd party DLLs as possible that are installed with windows. Also, low level, custom coding of everything else is required.

通常,关键是尽可能多地利用安装在windows上的第三方dll。此外,还需要对所有其他内容进行低级别自定义编码。

#5


5  

General: For smaller executables, #define WIN32_LEAN_AND_MEAN and VC_EXTRALEAN (assuming VS). Don't compile with debug symbols (you probably knew this). Use fewer libraries, and be user to only link the parts of libraries you need (VC's linker is pretty good about this, but don't touch optlink if you can help it).

一般:对于较小的可执行文件,#定义WIN32_LEAN_AND_MEAN和VC_EXTRALEAN(假设VS)。不要使用调试符号进行编译(您可能知道这一点)。使用更少的库,并且用户只需要链接你需要的库的部分(VC的链接器很好,但是如果你能帮助它,不要触摸optlink)。

Strip the relocation headers : Go to http://www.paehl.de/cms/oldtools and search for "ReduceEXE" (direct download link: http://www.paehl.de/reduce.zip ).

删除搬迁标题:访问http://www.paehl.de/cms/oldtools并搜索“ReduceEXE”(直接下载链接:http://www.paehl.de/reduce.zip)。

Run an executable packer: http://upx.sourceforge.net/ ... It uses up more memory at runtime and starts a bit slower, but the file is MUCH smaller.

运行可执行封隔器:http://upx.sourceforge.net/…它在运行时使用了更多的内存,并且开始得稍微慢一些,但是文件要小得多。

If you care about file size more than speed, VC has an option to "optimize for size", which turns off some things like loop unrolling and function inling.

如果你关心文件的大小超过速度,那么VC有一个“优化大小”的选项,它会关闭一些诸如循环展开和函数inling之类的东西。

If you want to go hardcore (and don't care about all the software engineering advantages), try using fewer classes, preferring POS types without virtual functions instead. Wikipedia suggests that 6-13% of a program's execution time is spent doing virtual calls. Further, the vtables themselves take up (a LITTLE) memory, and size_t worth of memory at the beginning of every class instance (that has a virtual function) is allocated for the vtable pointer. IOW, "pure C" can end up being slightly faster (though if you find yourself emulating classes with function pointers, go back to C++).

如果您想要走核心路线(并且不关心所有的软件工程优势),那么尝试使用更少的类,更喜欢没有虚拟功能的POS类型。*建议,程序执行时间的6-13%用于虚拟调用。此外,vtables本身占用(少量)内存,并且在每个类实例(具有虚拟函数)的开头为vtable指针分配size_t内存。IOW,“纯C”可能会稍微快一点(不过如果你发现自己在用函数指针模拟类,回到c++)。

#6


5  

The old "LIBCTiny" trick still works. With modern VC++ releases, you might need to turn of a few features.

旧的“LIBCTiny”技巧仍然有效。使用现代的vc++版本,您可能需要转换一些特性。

Another good trick to know is the lstr* collection of functions in Kernel32. That's already in memory, so those functions might be a leaner choice.

另一个需要了解的技巧是Kernel32中的lstr*函数集合。这已经在内存中了,所以这些函数可能是更精简的选择。

#7


1  

Notepad++ is also a very fast, highly optimized and very useful os program that could spark your inspiration. It's philosophy is similar to that of uTorrent. It uses the good old Win32 api which should still be the fastest you can go on Windows.

记事本++也是一个非常快速、高度优化和非常有用的操作系统程序,可以激发你的灵感。它的哲学与uTorrent相似。它使用的是老式的Win32 api,它应该仍然是你可以在Windows上运行的最快的。

If you want to go really artistic, the demo scene is the perfect place to go. Although their code is not always open.

如果你想要真正的艺术化,演示场景是一个完美的地方。虽然他们的代码并不总是打开的。

#1


17  

The Windows Template Library is geared towards what you want to do. It's a light-weight, template-based C++ wrapper for the Win32 API. With it, you don't have to go through the pain of direct Win32 coding, but it doesn't add a lot of overhead like MFC.

Windows模板库针对的是您想要做的事情。它是Win32 API的一个轻量级的、基于模板的c++包装器。使用它,您不必经历直接Win32编码的痛苦,但是它不像MFC那样增加很多开销。

#2


10  

uTorrent is written in C++ and uses the old fashioned Win32 API. Google Chrome is also written this way, so why not download the source code and learn from their code?

uTorrent是用c++编写的,使用的是老式的Win32 API。谷歌Chrome也是这样写的,所以为什么不下载源代码并从他们的代码中学习呢?

#3


8  

If you want to optimize for the smallest possible memory footprint and you don't mind jumping through a bunch of hoops that the .NET CLR was invented to take care of for you, then writing a direct Win32API app and hooking to GDI+ is the way to go. Petzold was the definitive reference.

如果您想要优化尽可能小的内存占用,并且您不介意跳过。net CLR为您设计的一系列限制,那么编写一个直接的Win32API应用程序并连接到GDI+就是一个不错的选择。佩佐德是最权威的参考。

Really, though, it's sort of a fool's errand, since the .NET runtime is going to be loaded into the OS's memory whether your app uses it or not, so you may as well link to it.

实际上,这是一种愚蠢的差事,因为。net运行时将被加载到操作系统的内存中,不管你的应用程序是否使用它,所以你也可以链接到它。

#4


7  

The Demo Scene is a group of people who spend their free time trying to make impressive and very small executables, which usually render something in 3d to music. Often the entire demo (code, music, 3d data) compiles into a single executable that is compressed to 64k or an impressively small size for the content.

演示场景是一群人,他们利用空闲时间尝试制作令人印象深刻的、非常小的可执行文件,这些文件通常以3d形式呈现给音乐。通常整个演示(代码、音乐、3d数据)编译成单个可执行文件,压缩到64k,或者内容非常小。

You might draw some inspiration from the demos and learning about how they are made will inform your obsession to create small executables.

您可能会从演示中获得一些灵感,了解它们是如何制作的,这将有助于您创建小型可执行文件。

Often, the key is to leverage as many 3rd party DLLs as possible that are installed with windows. Also, low level, custom coding of everything else is required.

通常,关键是尽可能多地利用安装在windows上的第三方dll。此外,还需要对所有其他内容进行低级别自定义编码。

#5


5  

General: For smaller executables, #define WIN32_LEAN_AND_MEAN and VC_EXTRALEAN (assuming VS). Don't compile with debug symbols (you probably knew this). Use fewer libraries, and be user to only link the parts of libraries you need (VC's linker is pretty good about this, but don't touch optlink if you can help it).

一般:对于较小的可执行文件,#定义WIN32_LEAN_AND_MEAN和VC_EXTRALEAN(假设VS)。不要使用调试符号进行编译(您可能知道这一点)。使用更少的库,并且用户只需要链接你需要的库的部分(VC的链接器很好,但是如果你能帮助它,不要触摸optlink)。

Strip the relocation headers : Go to http://www.paehl.de/cms/oldtools and search for "ReduceEXE" (direct download link: http://www.paehl.de/reduce.zip ).

删除搬迁标题:访问http://www.paehl.de/cms/oldtools并搜索“ReduceEXE”(直接下载链接:http://www.paehl.de/reduce.zip)。

Run an executable packer: http://upx.sourceforge.net/ ... It uses up more memory at runtime and starts a bit slower, but the file is MUCH smaller.

运行可执行封隔器:http://upx.sourceforge.net/…它在运行时使用了更多的内存,并且开始得稍微慢一些,但是文件要小得多。

If you care about file size more than speed, VC has an option to "optimize for size", which turns off some things like loop unrolling and function inling.

如果你关心文件的大小超过速度,那么VC有一个“优化大小”的选项,它会关闭一些诸如循环展开和函数inling之类的东西。

If you want to go hardcore (and don't care about all the software engineering advantages), try using fewer classes, preferring POS types without virtual functions instead. Wikipedia suggests that 6-13% of a program's execution time is spent doing virtual calls. Further, the vtables themselves take up (a LITTLE) memory, and size_t worth of memory at the beginning of every class instance (that has a virtual function) is allocated for the vtable pointer. IOW, "pure C" can end up being slightly faster (though if you find yourself emulating classes with function pointers, go back to C++).

如果您想要走核心路线(并且不关心所有的软件工程优势),那么尝试使用更少的类,更喜欢没有虚拟功能的POS类型。*建议,程序执行时间的6-13%用于虚拟调用。此外,vtables本身占用(少量)内存,并且在每个类实例(具有虚拟函数)的开头为vtable指针分配size_t内存。IOW,“纯C”可能会稍微快一点(不过如果你发现自己在用函数指针模拟类,回到c++)。

#6


5  

The old "LIBCTiny" trick still works. With modern VC++ releases, you might need to turn of a few features.

旧的“LIBCTiny”技巧仍然有效。使用现代的vc++版本,您可能需要转换一些特性。

Another good trick to know is the lstr* collection of functions in Kernel32. That's already in memory, so those functions might be a leaner choice.

另一个需要了解的技巧是Kernel32中的lstr*函数集合。这已经在内存中了,所以这些函数可能是更精简的选择。

#7


1  

Notepad++ is also a very fast, highly optimized and very useful os program that could spark your inspiration. It's philosophy is similar to that of uTorrent. It uses the good old Win32 api which should still be the fastest you can go on Windows.

记事本++也是一个非常快速、高度优化和非常有用的操作系统程序,可以激发你的灵感。它的哲学与uTorrent相似。它使用的是老式的Win32 api,它应该仍然是你可以在Windows上运行的最快的。

If you want to go really artistic, the demo scene is the perfect place to go. Although their code is not always open.

如果你想要真正的艺术化,演示场景是一个完美的地方。虽然他们的代码并不总是打开的。