Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | /* * Copyright (C) 2014 Apple Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "tree.h" #include <limits> #include <stdlib.h> #include <strings.h> #include "mbmalloc.h" namespace { struct Node { void* operator new(size_t size) { return mbmalloc(size); } void operator delete(void* p, size_t size) { mbfree(p, size); } Node(Node* left, Node* right, size_t payloadSize, size_t id) : m_refCount(1) , m_left(left) , m_right(right) , m_payload(static_cast<char*>(mbmalloc(payloadSize))) , m_payloadSize(payloadSize) , m_id(id) { if (m_left) m_left->ref(); if (m_right) m_right->ref(); bzero(m_payload, payloadSize); } ~Node() { if (m_left) m_left->deref(); if (m_right) m_right->deref(); mbfree(m_payload, m_payloadSize); } void ref() { ++m_refCount; } void deref() { if (m_refCount == 1) delete this; else --m_refCount; } size_t id() { return m_id; } Node* left() { return m_left; } Node* right() { return m_right; } void setLeft(Node* left) { left->ref(); if (m_left) m_left->deref(); m_left = left; } void setRight(Node* right) { right->ref(); if (m_right) m_right->deref(); m_right = right; } unsigned m_refCount; Node* m_left; Node* m_right; char* m_payload; size_t m_payloadSize; size_t m_id; }; void verify(Node* node, Node* left, Node* right) { if (left && left->id() >= node->id()) abort(); if (right && right->id() <= node->id()) abort(); } Node* createTree(size_t depth, size_t& counter) { if (!depth) return 0; Node* left = createTree(depth - 1, counter); size_t id = counter++; Node* right = createTree(depth - 1, counter); Node* result = new Node(left, right, ((depth * 8) & (64 - 1)) | 1, id); verify(result, left, right); if (left) left->deref(); if (right) right->deref(); return result; } Node* createTree(size_t depth) { size_t counter = 0; return createTree(depth, counter); } void churnTree(Node* node, size_t stride, size_t& counter) { if (!node) return; churnTree(node->left(), stride, counter); if (node->left() && !(counter % stride)) { Node* left = new Node(node->left()->left(), node->left()->right(), (counter & (64 - 1)) | 1, node->left()->id()); Node* right = new Node(node->right()->left(), node->right()->right(), (counter & (64 - 1)) | 1, node->right()->id()); node->setLeft(left); node->setRight(right); left->deref(); right->deref(); } ++counter; churnTree(node->right(), stride, counter); verify(node, node->left(), node->right()); } void churnTree(Node* tree, size_t stride) { size_t counter; churnTree(tree, stride, counter); } } // namespace void benchmark_tree_allocate(CommandLine& commandLine) { size_t times = 24; size_t depth = 16; if (commandLine.isParallel()) { times *= 4; depth = 13; } for (size_t time = 0; time < times; ++time) { Node* tree = createTree(depth); tree->deref(); } } void benchmark_tree_traverse(CommandLine& commandLine) { size_t times = 256; size_t depth = 15; if (commandLine.isParallel()) { times = 512; depth = 13; } Node* tree = createTree(depth); for (size_t time = 0; time < times; ++time) churnTree(tree, std::numeric_limits<size_t>::max()); // Reuse this to iterate and validate. tree->deref(); } void benchmark_tree_churn(CommandLine& commandLine) { size_t times = 130; size_t depth = 15; if (commandLine.isParallel()) { times *= 4; depth = 12; } Node* tree = createTree(depth); for (size_t time = 0; time < times; ++time) churnTree(tree, 8); tree->deref(); } |