EBernhardson (WMF) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/87021


Change subject: Cache a couple UUID values
......................................................................

Cache a couple UUID values

Cache's the hex and timestamp representations of the UUID to prevent
re-calculating on each method call.

Change-Id: I1a4695c86579841cf9a1d8549209aab15db4b110
---
M includes/Model/UUID.php
1 file changed, 22 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Flow 
refs/changes/21/87021/1

diff --git a/includes/Model/UUID.php b/includes/Model/UUID.php
index 04f7ffe..9ed7a36 100644
--- a/includes/Model/UUID.php
+++ b/includes/Model/UUID.php
@@ -5,7 +5,11 @@
 use Flow\Data\ObjectManager;
 
 class UUID {
+       // provided binary UUID
        protected $binaryValue;
+       // alternate representations
+       protected $hexValue;
+       protected $timestamp;
 
        function __construct( $binaryValue ) {
                if ( strlen( $binaryValue ) !== 16 ) {
@@ -46,7 +50,10 @@
        }
 
        public function getHex() {
-               return str_pad( bin2hex( $this->binaryValue ), 32, '0', 
STR_PAD_LEFT );
+               if ( $this->hexValue === null ) {
+                       return str_pad( bin2hex( $this->binaryValue ), 32, '0', 
STR_PAD_LEFT );
+               }
+               return $this->hexValue;
        }
 
        public function getBinary() {
@@ -58,19 +65,22 @@
        }
 
        public function getTimestampObj() {
-               // First 6 bytes === 48 bits
-               $hex = $this->getHex();
-               $timePortion = substr( $hex, 0, 12 );
-               $bits_48 = wfBaseConvert( $timePortion, 16, 2, 48 );
-               $bits_46 = substr( $bits_48, 0, 46 );
-               $msTimestamp = wfBaseConvert( $bits_46, 2, 10 );
+               if ( $this->timestamp === null ) {
+                       // First 6 bytes === 48 bits
+                       $hex = $this->getHex();
+                       $timePortion = substr( $hex, 0, 12 );
+                       $bits_48 = wfBaseConvert( $timePortion, 16, 2, 48 );
+                       $bits_46 = substr( $bits_48, 0, 46 );
+                       $msTimestamp = wfBaseConvert( $bits_46, 2, 10 );
 
-               try {
-                       return new \MWTimestamp( intval( $msTimestamp / 1000 ) 
);
-               } catch ( \TimestampException $e ) {
-                       wfDebugLog( __CLASS__, __FUNCTION__ . ": bogus time 
value: UUID=$hex; VALUE=$msTimestamp" );
-                       return false;
+                       try {
+                               $this->timestamp = new \MWTimestamp( intval( 
$msTimestamp / 1000 ) );
+                       } catch ( \TimestampException $e ) {
+                               wfDebugLog( __CLASS__, __FUNCTION__ . ": bogus 
time value: UUID=$hex; VALUE=$msTimestamp" );
+                               return false;
+                       }
                }
+               return clone $this->timestamp;
        }
 
        public function getTimestamp() {

-- 
To view, visit https://gerrit.wikimedia.org/r/87021
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1a4695c86579841cf9a1d8549209aab15db4b110
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Flow
Gerrit-Branch: master
Gerrit-Owner: EBernhardson (WMF) <ebernhard...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to