From 240bcc3f0857658e5d96de4aef0c7a6de3d8565d Mon Sep 17 00:00:00 2001 From: Lee Hinman Date: Fri, 24 Apr 2015 16:06:33 -0600 Subject: [PATCH] Don't create a new BigArrays instance for every call of `withCircuitBreaking` Since the circuit breaking service doesn't actually change for BigArrays, we can eagerly create a new instance only once and use that for all further invocations of `withCircuitBreaking`. --- .../java/org/elasticsearch/common/util/BigArrays.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/elasticsearch/common/util/BigArrays.java b/src/main/java/org/elasticsearch/common/util/BigArrays.java index 693b552d8ed..fa202f2d719 100644 --- a/src/main/java/org/elasticsearch/common/util/BigArrays.java +++ b/src/main/java/org/elasticsearch/common/util/BigArrays.java @@ -364,6 +364,7 @@ public class BigArrays { final PageCacheRecycler recycler; final CircuitBreakerService breakerService; final boolean checkBreaker; + private final BigArrays circuitBreakingInstance; @Inject public BigArrays(PageCacheRecycler recycler, @Nullable final CircuitBreakerService breakerService) { @@ -375,6 +376,11 @@ public class BigArrays { this.checkBreaker = checkBreaker; this.recycler = recycler; this.breakerService = breakerService; + if (checkBreaker) { + this.circuitBreakingInstance = this; + } else { + this.circuitBreakingInstance = new BigArrays(recycler, breakerService, true); + } } /** @@ -410,11 +416,11 @@ public class BigArrays { } /** - * Return a new instance of this BigArrays class with circuit breaking + * Return an instance of this BigArrays class with circuit breaking * explicitly enabled, instead of only accounting enabled */ public BigArrays withCircuitBreaking() { - return new BigArrays(this.recycler, this.breakerService, true); + return this.circuitBreakingInstance; } private T resizeInPlace(T array, long newSize) {