From c99496527da65524519654515db745721c854834 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Mon, 11 Dec 2017 14:24:09 -0800 Subject: [PATCH] Optimize GPN nextLevel heuristic --- lib/common.coffee | 10 ------ lib/gpn-heuristic.js | 82 ++++++++++++++++++++++++++++++++++++++++++++ lib/session.coffee | 53 ++++------------------------ 3 files changed, 89 insertions(+), 56 deletions(-) create mode 100644 lib/gpn-heuristic.js diff --git a/lib/common.coffee b/lib/common.coffee index 87e953f..9362a94 100644 --- a/lib/common.coffee +++ b/lib/common.coffee @@ -209,15 +209,6 @@ addPathMeta = (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 escapeRegExp = (str) -> return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&") @@ -235,5 +226,4 @@ exports.typeOf = typeOf exports.pathOverlap = pathOverlap exports.parsePath = parsePath exports.addPathMeta = addPathMeta -exports.hammingWeight = hammingWeight exports.escapeRegExp = escapeRegExp diff --git a/lib/gpn-heuristic.js b/lib/gpn-heuristic.js new file mode 100644 index 0000000..d27ecb3 --- /dev/null +++ b/lib/gpn-heuristic.js @@ -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 . + */ +"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; diff --git a/lib/session.coffee b/lib/session.coffee index 80818e0..0efd946 100644 --- a/lib/session.coffee +++ b/lib/session.coffee @@ -27,6 +27,7 @@ PathSet = require './path-set' VersionedMap = require './versioned-map' InstanceSet = require './instance-set' defaultProvisions = require './default-provisions' +gpnHeuristic = require './gpn-heuristic' 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) -> if not (syncState = sessionContext.syncState)? return @@ -952,13 +913,13 @@ generateGetRpcRequest = (sessionContext) -> path = paths.pop() 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 - 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 est = 0