Browse Source

Minor optimization in the HashRing class when adding/removing nodes from the ring

Daniele Alessandri 15 years ago
parent
commit
7dde9ff93c
1 changed files with 4 additions and 2 deletions
  1. 4 2
      lib/Predis.php

+ 4 - 2
lib/Predis.php

@@ -679,8 +679,9 @@ class HashRing {
     }
     }
 
 
     public function add($node) {
     public function add($node) {
+        $nodeHash = (string) $node;
         for ($i = 0; $i < self::NUMBER_OF_REPLICAS; $i++) {
         for ($i = 0; $i < self::NUMBER_OF_REPLICAS; $i++) {
-            $key = crc32((string)$node . ':' . $i);
+            $key = crc32($nodeHash . ':' . $i);
             $this->_ring[$key] = $node;
             $this->_ring[$key] = $node;
         }
         }
         ksort($this->_ring, SORT_NUMERIC);
         ksort($this->_ring, SORT_NUMERIC);
@@ -688,8 +689,9 @@ class HashRing {
     }
     }
 
 
     public function remove($node) {
     public function remove($node) {
+        $nodeHash = (string) $node;
         for ($i = 0; $i < self::NUMBER_OF_REPLICAS; $i++) {
         for ($i = 0; $i < self::NUMBER_OF_REPLICAS; $i++) {
-            $key = crc32((string)$node . '_' . $i);
+            $key = crc32($nodeHash . '_' . $i);
             unset($this->_ring[$key]);
             unset($this->_ring[$key]);
             $this->_ringKeys = array_filter($this->_ringKeys, function($rk) use($key) {
             $this->_ringKeys = array_filter($this->_ringKeys, function($rk) use($key) {
                 return $rk !== $key;
                 return $rk !== $key;