Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 339916 - [evaluator] Improve Tuple's hashCode performance by caching
Summary: [evaluator] Improve Tuple's hashCode performance by caching
Status: CLOSED FIXED
Alias: None
Product: OCL
Classification: Modeling
Component: Core (show other bugs)
Version: 3.1.0   Edit
Hardware: All All
: P3 enhancement (vote)
Target Milestone: ---   Edit
Assignee: OCL Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-03-14 12:02 EDT by Axel Uhl CLA
Modified: 2011-03-14 17:15 EDT (History)
1 user (show)

See Also:


Attachments
Caches hashCode of TupleInstance (1.45 KB, patch)
2011-03-14 12:25 EDT, Axel Uhl CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
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.