Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 339916

Summary: [evaluator] Improve Tuple's hashCode performance by caching
Product: [Modeling] OCL Reporter: Axel Uhl <eclipse>
Component: CoreAssignee: OCL Inbox <mdt-ocl-inbox>
Status: CLOSED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: ed
Version: 3.1.0   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Caches hashCode of TupleInstance none

Description Axel Uhl CLA 2011-03-14 12:02:34 EDT
Tuples are immutable values in OCL. Currently, their hashCode is computed each time hashCode() is invoked. By caching the result, performance can be improved.
Comment 1 Axel Uhl CLA 2011-03-14 12:25:39 EDT
Created attachment 191134 [details]
Caches hashCode of TupleInstance

This patch caches the tuple instance's hash code. A small number of computations is saved during the execution of the test cases which still are all green.
Comment 2 Ed Willink CLA 2011-03-14 13:26:27 EDT
-1. You assume the hashCode cannot be 0 (after numeric truncation).

Problem can be fixed by using an Integer rather than an int as the cache.
Comment 3 Axel Uhl CLA 2011-03-14 14:58:30 EDT
(In reply to comment #2)
> -1. You assume the hashCode cannot be 0 (after numeric truncation).
> 
> Problem can be fixed by using an Integer rather than an int as the cache.

That was on purpose, actually, because an int comparison is much faster than an Integer comparison, and the likelihood that a half-way reasonably defined hashCode evaluates to 0 is sufficiently small to justify this.

However, as the savings are miniscule anyway, I'm happy with closing this as WONTFIX. Feel free...
Comment 4 Ed Willink CLA 2011-03-14 15:10:16 EDT
I wasn't thinking of the consequences.

If the hashcode really is 0 (1 in 2^32) if uniformly distributed, in practice much less since numeric overflow probably only cuts in after three/four parts, the 0 hash is recomputed and recomputed. No worse than at present.

So + 1. Just add a comment to indicate that accidental recomputation is intended and safe.
Comment 5 Axel Uhl CLA 2011-03-14 17:15:11 EDT
Committed to CVS HEAD, adding a comment as Ed suggested.