Skip to content

Commit

Permalink
perf: ⚡️ use xorshift32 for random node picking
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Jan 13, 2024
1 parent e112134 commit e89429e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
},
"dependencies": {
"json-joy": "^11.25.0",
"thingies": "^1.15.0"
"thingies": "^1.16.0"
},
"devDependencies": {
"@types/benchmark": "^2.1.5",
Expand Down
15 changes: 10 additions & 5 deletions src/cluster/RedisClusterRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {AvlMap} from 'json-joy/es2020/util/trees/avl/AvlMap';
import {RedisClusterSlotRange} from './RedisClusterSlotRange';
import {RedisClusterNode} from './RedisClusterNode';
import {NodeHealth, NodeRole} from './constants';
import {xorShift32} from 'thingies/es2020/xorshift';
import {printTree} from 'json-joy/es2020/util/print/printTree';
import type {Printable} from 'json-joy/es2020/util/print/types';
import type {RedisCluster} from './RedisCluster';
Expand Down Expand Up @@ -106,20 +107,24 @@ export class RedisClusterRouter implements Printable {

public getRandomReplicaNodeForSlot(slot: number): RedisClusterNode | undefined {
const replicas = this.getReplicaNodesForSlot(slot);
if (!replicas.length) return undefined;
return replicas[Math.floor(Math.random() * replicas.length)];
const length = replicas.length;
if (!length) return undefined;
const index = xorShift32() % length;
return replicas[index];
}

public getRandomNodeForSlot(slot: number): RedisClusterNode | undefined {
const nodes = this.getNodesForSlot(slot);
if (!nodes.length) return undefined;
return nodes[Math.floor(Math.random() * nodes.length)];
const length = nodes.length;
if (!length) return undefined;
const index = xorShift32() % length;
return nodes[index];
}

public getRandomNode(): RedisClusterNode | undefined {
const size = this.byId.size;
if (!size) return undefined;
const index = Math.floor(Math.random() * size);
const index = xorShift32() % size;
let i = 0;
for (const client of this.byId.values()) if (i++ === index) return client;
return;
Expand Down
7 changes: 6 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2521,11 +2521,16 @@ test-exclude@^6.0.0:
glob "^7.1.4"
minimatch "^3.0.4"

thingies@^1.14.1, thingies@^1.15.0:
thingies@^1.14.1:
version "1.15.0"
resolved "https://registry.yarnpkg.com/thingies/-/thingies-1.15.0.tgz#bd186213bed5105b11eda0ce749fa475c5d4d6e3"
integrity sha512-ZSJlvEpD8QllYim0VSGlbAoob/iPrTWNlV/m8ltizMvMmzzU2gVJvHfH9ijLstyciWF70ZiQXqz+BCXWJq+ZQw==

thingies@^1.16.0:
version "1.16.0"
resolved "https://registry.yarnpkg.com/thingies/-/thingies-1.16.0.tgz#968cde87fbf0fdd69a1a3a8e9678324f634e5053"
integrity sha512-J23AVs11hSQxuJxvfQyMIaS9z1QpDxOCvMkL3ZxZl8/jmkgmnNGWrlyNxVz6Jbh0U6DuGmHqq6f7zUROfg/ncg==

tmpl@1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc"
Expand Down

0 comments on commit e89429e

Please sign in to comment.