瞎扯淡 关于 C++,关于 gc 的一些东西

rasefon · 2013年10月28日 · 最后由 chrishine 回复于 2013年11月09日 · 4312 次阅读

昨天没事又翻出了 Coders at Work 这本书看看,看到 Ken Thompson 对 C++ 和 GC 的评论,觉得很有意思,也很赞同,关于 C++ 的原文: Seibel: You were at AT&T with Bjarne Stroustrup. Were you involved at all in the development of C++?

Thompson: I’m gonna get in trouble.

Seibel: That’s fine.

Thompson: I would try out the language as it was being developed and make comments on it. It was part of the work atmosphere there. And you’d write something and then the next day it wouldn’t work because the language changed. It was very unstable for a very long period of time. At some point I said, no, no more. In an interview I said exactly that, that I didn’t use it just because it wouldn’t stay still for two days in a row. When Stroustrup read the interview he came screaming into my room about how I was undermining him and what I said mattered and I said it was a bad language. I never said it was a bad language. On and on and on. Since then I kind of avoid that kind of stuff.

Seibel: Can you say now whether you think it’s a good or bad language? Thompson: It certainly has its good points. But by and large I think it’s a bad language. It does a lot of things half well and it’s just a garbage heap of ideas that are mutually exclusive. Everybody I know, whether it’s personal or corporate, selects a subset and these subsets are different. So it’s not a good language to transport an algorithm—to say, “I wrote it; here, take it.” It’s way too big, way too complex. And it’s obviously built by a committee. Stroustrup campaigned for years and years and years, way beyond any sort of technical contributions he made to the language, to get it adopted and used. And he sort of ran all the standards committees with a whip and a chair. And he said “no” to no one. He put every feature in that language that ever existed. It wasn’t cleanly designed—it was just the union of everything that came along. And I think it suffered drastically from that.

Seibel: Do you think that was just because he likes all ideas or was it a way to get the language adopted, by giving everyone what they wanted?

Thompson: I think it’s more the latter than the former. 关于 GC 的原文: Seibel: On a somewhat related note, what about garbage collection? With Java, GC has finally made it into the mainstream. As Dennis Ritchie once said, C is actively hostile to garbage collection. Is it good that folks are moving toward garbage-collected languages—is it a technology that deserves to finally be in mainstream use?

Thompson: I don’t know. I’m schizophrenic on the subject. If you’re writing an operating system or a C compiler or something that’s used by lots and lots of people, I think garbage collection is a mistake, almost. It’s a cheat for you where you can do it by hand and do it better—much better. What you’re doing is your sloughing your task, your job, making it slower for your users. So I think it’s a mistake in an operating system. It almost just doesn’t fit in an operating system. But if you are writing a hack program to do a job, get an answer and then throw the program away, it’s beautiful. It takes a layer of stuff you don’t want to think about, at a cost you can afford, because computers are so fast, and it’s nothing but a win-win-win position. So I’m really schizophrenic on this subject. Part of the problem is there are different garbage-collection algorithms and they have different properties—massively different properties. So you’re writing some really general-purpose thing like an operating system—if you’re going to write it in a language that garbage-collects underneath, you don’t even have the choice of the algorithm for the operating systems. Suppose that you just can’t stand big real-time gaps and you have a garbage collector that runs up to some threshold and then does mark and sweep. You’re screwed before you start. So if you’re doing some general-purpose task that you don’t know who your real users are, you just can’t do that. Plus, garbage collection fights cache coherency massively. And there’s no garbage-collection algorithm that is right for all machines. There are machines where you can speed it up by a factor of five or more by messing around with the cache. They should be tied to the machine much more than they are. Usually they treat them as separate algorithms that have nothing to do with machines, but the cache coherency is very important for garbage-collection algorithms.

C++ 一直处于被吐槽状态,前阵子淘宝出了本书,从缺陷中学习 C/C++,看了下基本还是 C 的内容.据说曾经有公司不用 STL,因为 STL 有 BUG,他们的选择是重新造适应自己环境的轮子.不知道现在情况是否改观.C++ 的入门书都那么厚了,稍微进阶一点的立马有好几本,再算上实践的成本,学习成本的确太大了,以至于我现在还没入门.

GC 的观点感觉还是 thinking in Java 中说的那种:一些人认为 GC 这么重要的事情怎么能让程序员去做;另一部分人认为 GC 这么重要的事情怎么能让程序去做,各有取舍而已.

#2 楼 @chrishine gc 的观点我喜欢 Ken 的看法,做些不重要的小程序,做完就扔的,用 gc 不错。做系统级的,比如文件系统,编译器等,gc 多半会毁掉整个项目。

#3 楼 @rasefon 什么叫做完就扔?你让我们那个跑了 N 年的大型系统情何以堪。 关键还是,只有在迫不得已的情况下才让程序员来管理内存吧。

他说是写操作系统,编译器这些底层的东西,通过手工控制内存可以慢慢的来精益求精,所以不需要 gc,再说了底层的东西变化是非常缓慢的,对性能要求又比较高,所以 gc 用处不大;但对搞定一个任务,快速交付客户,在计算机已经非常快的情况下,这种性能上的损失又是完全可以忍受的情况下,使用 gc 能简化程序员编程,提高你的工作效率,何乐而不为。后面又说了没有一个通用的 gc 算法来处理各种任务,在一些实时任务中可能无法忍受 gc 带来的高延迟,所以需要酌情考虑。总之又回到了:"用合适的工具干合适的事情"这个主题中来。这里有必要说说 Erlang,在电话网络这种延迟要求苛刻的环境中使用了 20 多年了,说明 GC 并非不能做高性能的东西,可靠性说不定反而更好。 而对于 c++,其中说到每个公司,每个程序员只用到了 c++ 的一部分子集,而这些子集又是不同的,这里说的非常好。 我个人觉得如果 c++11,14 这些能早出来 8-10 年,就好了。现在 c++ 这种情况属于无解,历史遗留问题太多,但一时半会儿又没有什么语言能替代它,就这么凑合着用着吧。

我在想,设计一门语言,可以用 gc,也可以手动分配内存,完全的自主权。

#6 楼 @rasefon 你说的是 Rust 吧,其实 Ada ...

#5 楼 @hooluupog Ada 明显要比 C++ 好

#7 楼 @bhuztez 刚粗略地扫了一下 rust,感觉里面 borrowed pointer 更像是 ms 的 managed c++ 的概念。没有像 c 那样 allocte,free 之类的操作?

#10 楼 @bhuztez 看了一遍,感觉都是由 gc 回收的。

#12 楼 @rasefon ~这种是编译器自动插入 mallocfree

另外关于现有语言的错误处理,我也觉得都不怎么样,无论是根据返回值来判断,还是异常处理,都会让代码趋于混乱。如果有新语言,应该设计一套更模块化的错误处理机制,虽然本质上都是 jmp。

#14 楼 @rasefon Erlang 被你无视了啊...

#13 楼 @bhuztez 是啊,我是想有没有自己控制 free 的 entry?

#16 楼 @rasefon 自己 malloc 和 free 的功能是可以由库来提供的么,比如 Other smart pointers, such as ARC (which allows you to share read-only data between threads), are in the standard library and are not built into the compiler.

,而且你非要手工 malloc/free 的话,编译器就不能检查内存安全了,Rust 花了很大力气在如何在编译期尽可能保证内存安全,同时灵活性不比手工分配差太多上。

#15 楼 @bhuztez Erlang 没怎么接触过,今天去看一下。

#15 楼 @bhuztez Erlang.org 居然被墙了……

世界上只有两种编程语言:一种是整天被人喷的语言,一种是没有人用的语言。

#20 楼 @skandhas 每天都在用 c++,算起来也有 10 年了,从初的喜欢,到中间的狂热追捧,现在却是越来越没感觉,偏向于一点点的不喜欢。

#21 楼 @rasefon 哈哈。我用 C++ 的年头和你差不多,对她的感觉也和你差不多。应该说我们能更可观的看待 C++ 了:她强大是无庸置疑,同样缺点也很明显。

#22 楼 @skandhas C++ 的缺点是 C++ 只有优点

#23 楼 @bhuztez 我来说几个缺点,模板的错误信息通常让人觉得很莫名,会极大得增加编译时间,类内部结构细微的改动导致整个工程可能重新编译链接。很多时候,我也许只是想要调试一下,但是编译却往往花上一两个小时。

@rasefon 我觉得 GC 的观点没什么变动,Ken 说的依然是用不用的问题:因为做的事情不一样,所以有的适合用有的不适合用.(归根到底是目前 GC 太弱了. @hooluupog 看了下 Erlang 的软实时,大概是抢占式调度和占用极小的内存堆的一次性 GC?不知道 Erlang 通用性如何. @bhuztez Rust 语言特性级别的 smart pointer 感觉比 C++11 库级别的强很多啊.

C++ 的缺点不应是 C++ 只有 一大堆 优点么.

需要 登录 后方可回复, 如果你还没有账号请 注册新账号