Browse Source

Add `AcediaObject`/`AcediaActor` hash code caching

Now all objects only have to calculate their hash code once and it's
value will be stored for later `GetHashCode()` calls, since has code is
not suppoed to change during their lifetime anyway.
pull/8/head
Anton Tarasenko 4 years ago
parent
commit
2f1d599431
  1. 2
      sources/Data/Collections/Tests/MockItem.uc
  2. 2
      sources/Text/MutableText.uc
  3. 2
      sources/Text/Text.uc
  4. 42
      sources/Types/AcediaActor.uc
  5. 40
      sources/Types/AcediaObject.uc
  6. BIN
      sources/Types/Boxes/Native/BoolArrayBox.uc
  7. BIN
      sources/Types/Boxes/Native/BoolBox.uc
  8. BIN
      sources/Types/Boxes/Native/ByteArrayBox.uc
  9. BIN
      sources/Types/Boxes/Native/ByteBox.uc
  10. BIN
      sources/Types/Boxes/Native/FloatArrayBox.uc
  11. BIN
      sources/Types/Boxes/Native/FloatBox.uc
  12. BIN
      sources/Types/Boxes/Native/IntArrayBox.uc
  13. BIN
      sources/Types/Boxes/Native/IntBox.uc
  14. BIN
      sources/Types/Boxes/Native/StringArrayBox.uc
  15. BIN
      sources/Types/Boxes/Native/StringBox.uc

2
sources/Data/Collections/Tests/MockItem.uc

@ -37,7 +37,7 @@ public function bool IsEqual(Object other)
return true; return true;
} }
public function int GetHashCode() protected function int CalculateHashCode()
{ {
return 0; return 0;
} }

2
sources/Text/MutableText.uc

@ -358,7 +358,7 @@ private final function FormattedBlock CreateFormattedBlock(bool isOpening)
* *
* @return Hash code for the caller `MutableText`. * @return Hash code for the caller `MutableText`.
*/ */
public function int GetHashCode() protected function int CalculateHashCode()
{ {
return super(AcediaObject).GetHashCode(); return super(AcediaObject).GetHashCode();
} }

2
sources/Text/Text.uc

@ -418,7 +418,7 @@ protected final function ConvertCase(bool toLower)
* *
* @return Hash, that depends on textual contents only. * @return Hash, that depends on textual contents only.
*/ */
public function int GetHashCode() protected function int CalculateHashCode()
{ {
local int i; local int i;
local int hash; local int hash;

42
sources/Types/AcediaActor.uc

@ -52,9 +52,10 @@ var private bool _isAllocated;
// called for this object. // called for this object.
var private bool _staticConstructorWasCalled; var private bool _staticConstructorWasCalled;
// We want to have a common `GetHashCode()` method for all Acedia's objects. // We only want to compute hash code once and reuse generated value later,
// Just randomizing one at the time of allocation seems like a great idea. // since it cannot changed without reallocation.
var private int _randomizedHashCode; var private int _cachedHashCode;
var private bool _hashCodeWasCached;
// This object will provide hashed `string` to `Text` map, necessary for // This object will provide hashed `string` to `Text` map, necessary for
// efficient and convenient conversion methods. // efficient and convenient conversion methods.
@ -111,11 +112,11 @@ public function _constructor()
if (_isAllocated) return; if (_isAllocated) return;
_isAllocated = true; _isAllocated = true;
_lifeVersion += 1; _lifeVersion += 1;
_randomizedHashCode = Rand(MaxInt);
if (_ == none) { if (_ == none) {
default._ = class'Global'.static.GetInstance(); default._ = class'Global'.static.GetInstance();
_ = default._; _ = default._;
} }
_hashCodeWasCached = false;
Constructor(); Constructor();
} }
@ -261,19 +262,42 @@ public function bool IsEqual(Object other)
} }
/** /**
* Returns hash of an object. * Calculated hash of an object. Overload this method if you want to change
* how object's hash is computed.
*
* `GetHashCode()` method uses it to calculate had code once and then cache it.
* *
* If you overload `IsEqual()` method to allow two different objects to * If you overload `IsEqual()` method to allow two different objects to
* be equal, you must implement `GetHashCode()` to return the same hash * be equal, you must implement `CalculateHashCode()` to return the same hash
* for them. * for them.
* *
* By default it is just a random value, generated at the time of allocation. * By default it is just a random value, generated at the time of allocation.
* *
* @return Hash code for the caller actor. * @return Hash code for the caller object.
*/
protected function int CalculateHashCode()
{
return Rand(MaxInt);
}
/**
* Returns hash of an object.
*
* Calculated hash only once, later using internally cached value.
*
* By default it is just a random value.
* See `CalculateHashCode()` if you wish to change how hash code is computed.
*
* @return Hash code for the caller object.
*/ */
public function int GetHashCode() public final function int GetHashCode()
{ {
return _randomizedHashCode; if (_hashCodeWasCached) {
return _cachedHashCode;
}
_hashCodeWasCached = true;
_cachedHashCode = CalculateHashCode();
return _cachedHashCode;
} }
/** /**

40
sources/Types/AcediaObject.uc

@ -53,9 +53,10 @@ var private bool _isAllocated;
// called for this object. // called for this object.
var private bool _staticConstructorWasCalled; var private bool _staticConstructorWasCalled;
// We want to have a common `GetHashCode()` method for all Acedia's objects. // We only want to compute hash code once and reuse generated value later,
// Just randomizing one at the time of allocation seems like a great idea. // since it cannot changed without reallocation.
var private int _randomizedHashCode; var private int _cachedHashCode;
var private bool _hashCodeWasCached;
// This object will provide hashed `string` to `Text` map, necessary for // This object will provide hashed `string` to `Text` map, necessary for
// efficient and convenient conversion methods. // efficient and convenient conversion methods.
@ -112,7 +113,6 @@ public final function _constructor()
if (_isAllocated) return; if (_isAllocated) return;
_isAllocated = true; _isAllocated = true;
_lifeVersion += 1; _lifeVersion += 1;
_randomizedHashCode = Rand(MaxInt);
if (_ == none) { if (_ == none) {
default._ = class'Global'.static.GetInstance(); default._ = class'Global'.static.GetInstance();
_ = default._; _ = default._;
@ -121,6 +121,7 @@ public final function _constructor()
StaticConstructor(); StaticConstructor();
default._staticConstructorWasCalled = true; default._staticConstructorWasCalled = true;
} }
_hashCodeWasCached = false;
Constructor(); Constructor();
} }
@ -266,19 +267,42 @@ public function bool IsEqual(Object other)
} }
/** /**
* Returns hash of an object. * Calculated hash of an object. Overload this method if you want to change
* how object's hash is computed.
*
* `GetHashCode()` method uses it to calculate had code once and then cache it.
* *
* If you overload `IsEqual()` method to allow two different objects to * If you overload `IsEqual()` method to allow two different objects to
* be equal, you must implement `GetHashCode()` to return the same hash * be equal, you must implement `CalculateHashCode()` to return the same hash
* for them. * for them.
* *
* By default it is just a random value, generated at the time of allocation. * By default it is just a random value, generated at the time of allocation.
* *
* @return Hash code for the caller object. * @return Hash code for the caller object.
*/ */
public function int GetHashCode() protected function int CalculateHashCode()
{ {
return _randomizedHashCode; return Rand(MaxInt);
}
/**
* Returns hash of an object.
*
* Calculated hash only once, later using internally cached value.
*
* By default it is just a random value.
* See `CalculateHashCode()` if you wish to change how hash code is computed.
*
* @return Hash code for the caller object.
*/
public final function int GetHashCode()
{
if (_hashCodeWasCached) {
return _cachedHashCode;
}
_hashCodeWasCached = true;
_cachedHashCode = CalculateHashCode();
return _cachedHashCode;
} }
/** /**

BIN
sources/Types/Boxes/Native/BoolArrayBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/BoolBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/ByteArrayBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/ByteBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/FloatArrayBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/FloatBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/IntArrayBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/IntBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/StringArrayBox.uc

Binary file not shown.

BIN
sources/Types/Boxes/Native/StringBox.uc

Binary file not shown.
Loading…
Cancel
Save