Path: blob/master/src/java.desktop/share/classes/sun/java2d/marlin/ArrayCacheConst.java
41159 views
/*1* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.marlin;2627import java.util.Arrays;28import static sun.java2d.marlin.MarlinUtils.logInfo;2930public final class ArrayCacheConst implements MarlinConst {3132static final int BUCKETS = 8;33static final int MIN_ARRAY_SIZE = 4096;34// maximum array size35static final int MAX_ARRAY_SIZE;36// threshold below to grow arrays by 437static final int THRESHOLD_SMALL_ARRAY_SIZE = 4 * 1024 * 1024;38// threshold to grow arrays only by (3/2) instead of 239static final int THRESHOLD_ARRAY_SIZE;40// threshold to grow arrays only by (5/4) instead of (3/2)41static final long THRESHOLD_HUGE_ARRAY_SIZE;42static final int[] ARRAY_SIZES = new int[BUCKETS];4344static {45// initialize buckets for int/float arrays46int arraySize = MIN_ARRAY_SIZE;4748int inc_lg = 2; // x44950for (int i = 0; i < BUCKETS; i++, arraySize <<= inc_lg) {51ARRAY_SIZES[i] = arraySize;5253if (DO_TRACE) {54logInfo("arraySize[" + i + "]: " + arraySize);55}5657if (arraySize >= THRESHOLD_SMALL_ARRAY_SIZE) {58inc_lg = 1; // x259}60}61MAX_ARRAY_SIZE = arraySize >> inc_lg;6263if (MAX_ARRAY_SIZE <= 0) {64throw new IllegalStateException("Invalid max array size !");65}6667THRESHOLD_ARRAY_SIZE = 16 * 1024 * 1024; // >16M68THRESHOLD_HUGE_ARRAY_SIZE = 48L * 1024 * 1024; // >48M6970if (DO_STATS || DO_MONITORS) {71logInfo("ArrayCache.BUCKETS = " + BUCKETS);72logInfo("ArrayCache.MIN_ARRAY_SIZE = " + MIN_ARRAY_SIZE);73logInfo("ArrayCache.MAX_ARRAY_SIZE = " + MAX_ARRAY_SIZE);74logInfo("ArrayCache.ARRAY_SIZES = "75+ Arrays.toString(ARRAY_SIZES));76logInfo("ArrayCache.THRESHOLD_ARRAY_SIZE = "77+ THRESHOLD_ARRAY_SIZE);78logInfo("ArrayCache.THRESHOLD_HUGE_ARRAY_SIZE = "79+ THRESHOLD_HUGE_ARRAY_SIZE);80}81}8283private ArrayCacheConst() {84// Utility class85}8687// small methods used a lot (to be inlined / optimized by hotspot)8889static int getBucket(final int length) {90for (int i = 0; i < ARRAY_SIZES.length; i++) {91if (length <= ARRAY_SIZES[i]) {92return i;93}94}95return -1;96}9798/**99* Return the new array size (~ x2)100* @param curSize current used size101* @param needSize needed size102* @return new array size103*/104public static int getNewSize(final int curSize, final int needSize) {105// check if needSize is negative or integer overflow:106if (needSize < 0) {107// hard overflow failure - we can't even accommodate108// new items without overflowing109throw new ArrayIndexOutOfBoundsException(110"array exceeds maximum capacity !");111}112assert curSize >= 0;113final int initial = curSize;114int size;115if (initial > THRESHOLD_ARRAY_SIZE) {116size = initial + (initial >> 1); // x(3/2)117} else {118size = (initial << 1); // x2119}120// ensure the new size is >= needed size:121if (size < needSize) {122// align to 4096 (may overflow):123size = ((needSize >> 12) + 1) << 12;124}125// check integer overflow:126if (size < 0) {127// resize to maximum capacity:128size = Integer.MAX_VALUE;129}130return size;131}132133/**134* Return the new array size (~ x2)135* @param curSize current used size136* @param needSize needed size137* @return new array size138*/139public static long getNewLargeSize(final long curSize, final long needSize) {140// check if needSize is negative or integer overflow:141if ((needSize >> 31L) != 0L) {142// hard overflow failure - we can't even accommodate143// new items without overflowing144throw new ArrayIndexOutOfBoundsException(145"array exceeds maximum capacity !");146}147assert curSize >= 0L;148long size;149if (curSize > THRESHOLD_HUGE_ARRAY_SIZE) {150size = curSize + (curSize >> 2L); // x(5/4)151} else if (curSize > THRESHOLD_ARRAY_SIZE) {152size = curSize + (curSize >> 1L); // x(3/2)153} else if (curSize > THRESHOLD_SMALL_ARRAY_SIZE) {154size = (curSize << 1L); // x2155} else {156size = (curSize << 2L); // x4157}158// ensure the new size is >= needed size:159if (size < needSize) {160// align to 4096:161size = ((needSize >> 12L) + 1L) << 12L;162}163// check integer overflow:164if (size > Integer.MAX_VALUE) {165// resize to maximum capacity:166size = Integer.MAX_VALUE;167}168return size;169}170171static final class CacheStats {172final String name;173final BucketStats[] bucketStats;174int resize = 0;175int oversize = 0;176long totalInitial = 0L;177178CacheStats(final String name) {179this.name = name;180181bucketStats = new BucketStats[BUCKETS];182for (int i = 0; i < BUCKETS; i++) {183bucketStats[i] = new BucketStats();184}185}186187void reset() {188resize = 0;189oversize = 0;190191for (int i = 0; i < BUCKETS; i++) {192bucketStats[i].reset();193}194}195196long dumpStats() {197long totalCacheBytes = 0L;198199if (DO_STATS) {200for (int i = 0; i < BUCKETS; i++) {201final BucketStats s = bucketStats[i];202203if (s.maxSize != 0) {204totalCacheBytes += getByteFactor()205* (s.maxSize * ARRAY_SIZES[i]);206}207}208209if (totalInitial != 0L || totalCacheBytes != 0L210|| resize != 0 || oversize != 0)211{212logInfo(name + ": resize: " + resize213+ " - oversize: " + oversize214+ " - initial: " + getTotalInitialBytes()215+ " bytes (" + totalInitial + " elements)"216+ " - cache: " + totalCacheBytes + " bytes"217);218}219220if (totalCacheBytes != 0L) {221logInfo(name + ": usage stats:");222223for (int i = 0; i < BUCKETS; i++) {224final BucketStats s = bucketStats[i];225226if (s.getOp != 0) {227logInfo(" Bucket[" + ARRAY_SIZES[i] + "]: "228+ "get: " + s.getOp229+ " - put: " + s.returnOp230+ " - create: " + s.createOp231+ " :: max size: " + s.maxSize232);233}234}235}236}237return totalCacheBytes;238}239240private int getByteFactor() {241int factor = 1;242if (name.contains("Int") || name.contains("Float")) {243factor = 4;244} else if (name.contains("Double")) {245factor = 8;246}247return factor;248}249250long getTotalInitialBytes() {251return getByteFactor() * totalInitial;252}253}254255static final class BucketStats {256int getOp = 0;257int createOp = 0;258int returnOp = 0;259int maxSize = 0;260261void reset() {262getOp = 0;263createOp = 0;264returnOp = 0;265maxSize = 0;266}267268void updateMaxSize(final int size) {269if (size > maxSize) {270maxSize = size;271}272}273}274}275276277