TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Range-Checks and Recklessness

22 pointsby fexlabout 11 years ago

5 comments

bunderbunderabout 11 years ago
Modern CPU design adds an interesting twist to the bounds checking question that (imo) renders it moot. At least on Intel CPUs, the array bounds check gets compiled down to a compare-and-jump instruction pair, which then ends up getting run as a single microinstruction on the CPU. Unless you&#x27;re in an <i>extremely</i> tight loop, the cost of that instruction just isn&#x27;t significant. The only time it costs much at all is in the event of a branch misprediction, in which case the very next thing that&#x27;s going to happen is an exception so it&#x27;s still insignificant given the context. And if you are in an extremely tight loop, it&#x27;s probably structured in a way that makes it easy for compiler optimize away the bounds check.
评论 #7455663 未加载
Roboprogabout 11 years ago
It&#x27;s funny the author uses an example of an array indexed by 199x values, as the 90s were in some way &quot;the lost decade&quot; of C&#x2F;++ development, and an utter lack of concern for this sort of checking.<p>The realities of internet exposure put an end to this recklessness for casual^H^H^H^H^H &quot;enterprise&quot; software development.<p>I love C A Hoare&#x27;s comments, reference in this link (<a href="http://en.wikipedia.org/wiki/Bounds_checking" rel="nofollow">http:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bounds_checking</a>) about &quot;some languages&quot; (cough cough - C)<p>Interesting how the Go language now includes array bounds checking. While 2 of the main designers are ex Bell labs, 1 of them is from U of Zurich. (I&#x27;m assuming he would have had some Pascal&#x2F;Modula exposure there)
评论 #7454253 未加载
deathanatosabout 11 years ago
Range checking is great; the CPU cycles consumed are well worth the bugs they catch.<p>Thing is, they&#x27;re a list thing, and there&#x27;s more than just &quot;is this index valid?&quot; that can go wrong. One of the things I appreciate about C++ is that iterator invalidation is very precisely specified. Sadly, that&#x27;s where it stops: it&#x27;s just specified, but the onus is still on you to catch the errors. It&#x27;d be great to have that same thing: immediate errors when you use an invalidated iterator. (in vectors, it might just skip an element (some deletes) or see the same element twice (some inserts); Python warns about this during some cases with dicts:<p><pre><code> RuntimeError: dictionary changed size during iteration </code></pre> which is nice, but I think you can still slip by that.) I don&#x27;t believe Java or Python specify what happens to iterators when the collection changes, which to me, is a bit sad.<p>Thing is, to implement this, the collections would probably need to know about all outstanding iterators, so as to figure out where they are and whether they should be invalidated at all. Most operators then become O(number of active iterators + whatever the normal cost of this op is); I&#x27;d argue this might still be close to O(1 + ...) since the number of active iterators on a collection is probably usually 0 or 1. But there&#x27;s a memory cost, a CPU cost, and if you have something like:<p><pre><code> if(weird condition) { use_that_iterator_i_just_accidentally_invalidated() } </code></pre> Then your bug only gets caught if `weird_condition` is true. Is it worth it?
评论 #7454997 未加载
TheLoneWolflingabout 11 years ago
Simple answer:<p>Leave in all range checks. If the compiler can determine that the range check will never be hit, or can partially optimize it (hoisting it out of a loop, etc) great. Otherwise? It gets checked at runtime, which is typically not that expensive an operation anyways.
al2o3crabout 11 years ago
I wonder if the &quot;bound&quot; instruction on x86 has the same issue that the VAX(?) opcode did - that it&#x27;s faster to write out the two checks than to call the instruction...