Optimize GPN nextLevel heuristic
This commit is contained in:
1 parent
32b2d721e5
commit
c99496527d
3 files changed
+89
-56
No files matched your search
@@ -209,15 +209,6 @@ addPathMeta = (path) ->
|
|||||||
return path
|
return path
|
||||||
|
|
||||||
|
|
||||||
hammingWeight = (flags) ->
|
|
||||||
flags -= ((flags >> 1) & 0x55555555)
|
|
||||||
flags = (((flags >> 2) & 0x33333333) + (flags & 0x33333333))
|
|
||||||
flags = (((flags >> 4) + flags) & 0x0f0f0f0f)
|
|
||||||
flags += (flags >> 8)
|
|
||||||
flags += (flags >> 16)
|
|
||||||
return flags & 0x0000003f
|
|
||||||
|
|
||||||
|
|
||||||
# Source: http://stackoverflow.com/a/6969486
|
# Source: http://stackoverflow.com/a/6969486
|
||||||
escapeRegExp = (str) ->
|
escapeRegExp = (str) ->
|
||||||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
|
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&")
|
||||||
@@ -235,5 +226,4 @@ exports.typeOf = typeOf
|
|||||||
exports.pathOverlap = pathOverlap
|
exports.pathOverlap = pathOverlap
|
||||||
exports.parsePath = parsePath
|
exports.parsePath = parsePath
|
||||||
exports.addPathMeta = addPathMeta
|
exports.addPathMeta = addPathMeta
|
||||||
exports.hammingWeight = hammingWeight
|
|
||||||
exports.escapeRegExp = escapeRegExp
|
exports.escapeRegExp = escapeRegExp
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2013-2017 Zaid Abdulla
|
||||||
|
*
|
||||||
|
* This file is part of GenieACS.
|
||||||
|
*
|
||||||
|
* GenieACS is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* GenieACS is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with GenieACS. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
"use strict";
|
||||||
|
|
||||||
|
const WILDCARD_MULTIPLIER = 2;
|
||||||
|
const UNDISCOVERED_DEPTH = 7;
|
||||||
|
|
||||||
|
// Simple heuristic to estimate GPN count given a set of patterns to be
|
||||||
|
// discovered. Used to decide whether to use nextLevel = false in GPN.
|
||||||
|
// gpnPatterns is [pattern, flags]
|
||||||
|
// pattern is a path (array)
|
||||||
|
// flags is an int where its bits mark the segments in the pattern that
|
||||||
|
// need refreshing. Leading 0s indicate that the pattern up to that
|
||||||
|
// point has been discovered.
|
||||||
|
function estimateGpnCount(gpnPatterns, depth = 0) {
|
||||||
|
const children = {};
|
||||||
|
const wildcardChildren = [];
|
||||||
|
let wildcardDiscovered = false;
|
||||||
|
let gpnCount = 0;
|
||||||
|
|
||||||
|
for (let pattern of gpnPatterns) {
|
||||||
|
const path = pattern[0];
|
||||||
|
const flags = pattern[1] >> depth;
|
||||||
|
|
||||||
|
let k = path[depth];
|
||||||
|
|
||||||
|
if (!k) {
|
||||||
|
if (flags & 1)
|
||||||
|
gpnCount = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (flags & 1) {
|
||||||
|
gpnCount = 1;
|
||||||
|
|
||||||
|
if (depth > UNDISCOVERED_DEPTH)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
else if (k === "*")
|
||||||
|
wildcardDiscovered = true;
|
||||||
|
|
||||||
|
if (k === "*") {
|
||||||
|
wildcardChildren.push(pattern);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
children[k] = children[k] || [];
|
||||||
|
children[k].push(pattern);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let wildcardGpnCount = 0;
|
||||||
|
if (!wildcardDiscovered && wildcardChildren.length)
|
||||||
|
wildcardGpnCount += estimateGpnCount(wildcardChildren, depth + 1) * WILDCARD_MULTIPLIER;
|
||||||
|
|
||||||
|
for (let k of Object.keys(children)) {
|
||||||
|
let c = estimateGpnCount(children[k].concat(wildcardChildren), depth + 1);
|
||||||
|
wildcardGpnCount -= c;
|
||||||
|
gpnCount += c;
|
||||||
|
}
|
||||||
|
|
||||||
|
gpnCount += Math.max(0, wildcardGpnCount);
|
||||||
|
|
||||||
|
return gpnCount;
|
||||||
|
}
|
||||||
|
|
||||||
|
exports.estimateGpnCount = estimateGpnCount;
|
||||||
+7
-46
@@ -27,6 +27,7 @@ PathSet = require './path-set'
|
|||||||
VersionedMap = require './versioned-map'
|
VersionedMap = require './versioned-map'
|
||||||
InstanceSet = require './instance-set'
|
InstanceSet = require './instance-set'
|
||||||
defaultProvisions = require './default-provisions'
|
defaultProvisions = require './default-provisions'
|
||||||
|
gpnHeuristic = require './gpn-heuristic'
|
||||||
|
|
||||||
MAX_ITERATIONS = 64
|
MAX_ITERATIONS = 64
|
||||||
|
|
||||||
@@ -856,46 +857,6 @@ rpcRequest = (sessionContext, _declarations, callback) ->
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
# Simple algorithm to estimate GPN count in a set of patterns used
|
|
||||||
# to decide whether to use nextLevel = false in GPN.
|
|
||||||
estimateGpnCount = (patterns) ->
|
|
||||||
res = []
|
|
||||||
counts = new Map()
|
|
||||||
for pattern in patterns
|
|
||||||
c = []
|
|
||||||
i = -1
|
|
||||||
while pattern[1] >> (++ i)
|
|
||||||
if pattern[1] & (1 << i)
|
|
||||||
f = (pattern[0].wildcard & pattern[1]) | ((pattern[1] >> pattern[0].length) << pattern[0].length)
|
|
||||||
r = [i, f & ((1 << i) - 1), 1, new Set()]
|
|
||||||
res.push(c[i] = r)
|
|
||||||
counts.set(pattern, c)
|
|
||||||
|
|
||||||
for pattern in patterns
|
|
||||||
pats = patterns.filter((pat) -> return pat != pattern)
|
|
||||||
i = -1
|
|
||||||
while pattern[1] >> (++ i)
|
|
||||||
pats = pats.filter((pat) ->
|
|
||||||
return false if not pat[1] << i
|
|
||||||
if not pat[0][i]? or pat[0][i] == '*' or pat[0][i] == pattern[0][i]
|
|
||||||
if pattern[1] & pat[1] & (1 << i)
|
|
||||||
counts.get(pattern)[i][3].add(counts.get(pat)[i])
|
|
||||||
return true
|
|
||||||
return false
|
|
||||||
)
|
|
||||||
|
|
||||||
count = 0
|
|
||||||
for r in res
|
|
||||||
div = 1
|
|
||||||
r[3].forEach((rr) -> ++ div if rr[3].has(r))
|
|
||||||
if r[3].size == div - 1
|
|
||||||
h = common.hammingWeight(r[1])
|
|
||||||
if h < 8
|
|
||||||
count += (r[2] * Math.pow(3, h)) / div
|
|
||||||
|
|
||||||
return Math.round(count)
|
|
||||||
|
|
||||||
|
|
||||||
generateGetRpcRequest = (sessionContext) ->
|
generateGetRpcRequest = (sessionContext) ->
|
||||||
if not (syncState = sessionContext.syncState)?
|
if not (syncState = sessionContext.syncState)?
|
||||||
return
|
return
|
||||||
@@ -952,13 +913,13 @@ generateGetRpcRequest = (sessionContext) ->
|
|||||||
path = paths.pop()
|
path = paths.pop()
|
||||||
|
|
||||||
if path
|
if path
|
||||||
patterns = []
|
|
||||||
for p in sessionContext.deviceData.paths.find(path, true, false, 99)
|
|
||||||
if v = syncState.gpnPatterns.get(p)
|
|
||||||
patterns.push([p, (v >> path.length) << path.length])
|
|
||||||
|
|
||||||
if path.length >= GPN_NEXT_LEVEL
|
if path.length >= GPN_NEXT_LEVEL
|
||||||
est = estimateGpnCount(patterns)
|
patterns = [[path, 0]]
|
||||||
|
for p in sessionContext.deviceData.paths.find(path, true, false, 99)
|
||||||
|
if v = syncState.gpnPatterns.get(p)
|
||||||
|
patterns.push([p, (v >> path.length) << path.length])
|
||||||
|
|
||||||
|
est = gpnHeuristic.estimateGpnCount(patterns)
|
||||||
else
|
else
|
||||||
est = 0
|
est = 0
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user