TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

JVM Anatomy Quark #26: Identity Hash Code

101 点作者 fniephaus将近 4 年前

5 条评论

MauranKilom将近 4 年前
I don&#x27;t see how<p>&gt; &quot;Not suprisingly, [...] global counters [...] are poorly distributed as well.&quot;<p>is true.<p>I mean, yes, they are not uniformly distributed, but that was never the requirement. As the article itself states, the desired property is that &quot;the values for distinct objects are more or less distinct&quot;. With a global counter, you get <i>maximally</i> distinct hash codes. <i>More</i> distinct than any of the other approaches (and not less than any user-implemented function), at least until 2^31 object allocations.<p>Yes, after 2^31 objects you will get repeated values, but that is trivially the case for <i>any</i> pattern of assigning 31 bit hash codes to distinct objects (and any of the pseudo-random approaches will get birthday-paradoxed <i>much</i> sooner, and <i>much</i> harder). The only case where this could matter is in an adversarial scenario where someone is trying to DoS your hash map with crafted inputs. But according to the article itself, it would take 4+ minutes (120 ns * 2^31) of only allocating objects for each global counter wrapping. If an adversary can reliably achieve that already, what&#x27;s the point in slowing down a hash map by an epsilon every four minutes?
评论 #27899870 未加载
评论 #27899875 未加载
barosl将近 4 年前
I think it was a mistake to put hashCode (and to an extent, equals) to all objects by default. It makes Objects bloated, and because of that, those who will never be put to a hash table also need to provide their own hashCode implementations, causing problems like the one mentioned in the article. There should have been a separate, dedicated &quot;Hashable&quot; interface. Strangely, C# shares the same caveat. Is there any benefit to making objects hashable by default?
评论 #27900253 未加载
评论 #27902495 未加载
评论 #27899946 未加载
评论 #27899894 未加载
评论 #27899620 未加载
评论 #27899617 未加载
评论 #27904249 未加载
评论 #27899574 未加载
评论 #27906781 未加载
评论 #27900245 未加载
评论 #27899674 未加载
jollybean将近 4 年前
They use int instead of long which means if you start to group objects together in any magnitude you will get collisions fairly quickly.<p>Any large HashTable in Java starts to yield the problem of duplicate keys, it&#x27;s just a weird situation, like you can 99.999% trust something ... but can&#x27;t ever fully trust it so that over time, you&#x27;re guaranteed to have something wrong.<p>This problem exhibits itself again under the hood in the JNI API when you have to identify objects in another domain i.e. C++.<p>It&#x27;s not a &#x27;Quirk&#x27; it&#x27;s basically a big mistake.<p>The ability to uniquely identify objects is so important in so many ways.<p>Sometimes I wish every few versions of Java they would skip the reverse compatibility and make some needed changes.
评论 #27902658 未加载
评论 #27905280 未加载
matheusmoreira将近 4 年前
Love to read about these implementation details!
评论 #27898284 未加载
teknopaul将近 4 年前
&quot;It is a frequent source of bugs to change the object in such a way that its hashCode changes after it was used&quot;<p>(citation needed)<p>I have never seen that. 20 years in the game.<p>I can&#x27;t remember ever having seen anyone use a mutable object for a hashmap key.
评论 #27898575 未加载
评论 #27898461 未加载
评论 #27898709 未加载
评论 #27902547 未加载
评论 #27899642 未加载
评论 #27899212 未加载
评论 #27900421 未加载
评论 #27902360 未加载
评论 #27898471 未加载