Debian Codegraph @

net/sf/saxon/tree/tiny/Statistics.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.tree.tiny;
9
10 /**
11 * Statistics on the size of TinyTree instances, kept so that the system can learn how much space to allocate to new trees
12 */
13 public class Statistics {
14
15 // We maintain statistics, recording how large the trees created under this Java VM
16 // turned out to be. These figures are then used when allocating space for new trees, on the assumption
17 // that there is likely to be some uniformity. The statistics are initialized to an arbitrary value
18 // so that they can be used every time including the first time. The count of how many trees have been
19 // created so far is initialized artificially to 5, to provide some smoothing if the first real tree is
20 // atypically large or small.
21
22 private int treesCreated = 5;
23 private double averageNodes = 4000.0;
24 private double averageAttributes = 100.0;
25 private double averageNamespaces = 20.0;
26 private double averageCharacters = 4000.0;
27
28 public Statistics() {
29 }
30
31 public Statistics(int nodes, int atts, int namespaces, int chars) {
32 this.averageNodes = nodes;
33 this.averageAttributes = atts;
34 this.averageNamespaces = namespaces;
35 this.averageCharacters = chars;
36 }
37
38 public double getAverageNodes() {
39 return averageNodes;
40 }
41
42 public double getAverageAttributes() {
43 return averageAttributes;
44 }
45
46 public double getAverageNamespaces() {
47 return averageNamespaces;
48 }
49
50 public double getAverageCharacters() {
51 return averageCharacters;
52 }
53
54 /**
55 * Update the statistics held in static data. We don't bother to sychronize, on the basis that it doesn't
56 * matter if the stats are wrong.
57 * @param numberOfNodes the number of (non-attribute, non-namespace) nodes
58 * @param numberOfAttributes the number of attribute nodes
59 * @param numberOfNamespaces the number of namespace bindings (deltas on namespace nodes)
60 * @param chars the number of characters in text nodes
61 */
62
63 public synchronized void updateStatistics(int numberOfNodes, int numberOfAttributes, int numberOfNamespaces, int chars) {
64 int n0 = treesCreated;
65 if (n0 < 1000000) { // it should have stabilized by then, and we don't want to overflow
66 int n1 = treesCreated + 1;
67 treesCreated = n1;
68 averageNodes = ((averageNodes * n0) + numberOfNodes) / n1;
69 if (averageNodes < 10.0) {
70 averageNodes = 10.0;
71 }
72 averageAttributes = ((averageAttributes * n0) + numberOfAttributes) / n1;
73 if (averageAttributes < 10.0) {
74 averageAttributes = 10.0;
75 }
76 averageNamespaces = ((averageNamespaces * n0) + numberOfNamespaces) / n1;
77 if (averageNamespaces < 5.0) {
78 averageNamespaces = 5.0;
79 }
80 averageCharacters = ((averageCharacters * n0) + chars) / n1;
81 if (averageCharacters < 100.0) {
82 averageCharacters = 100.0;
83 }
84 }
85
86 }
87
88 public String toString() {
89 return treesCreated + "(" + averageNodes + "," + averageAttributes + "," + averageNamespaces + "," + averageCharacters + ")";
90 }
91
92 }
93
94