diff --git a/platform/platform-impl/src/com/intellij/openapi/editor/impl/RangeMarkerImpl.java b/platform/platform-impl/src/com/intellij/openapi/editor/impl/RangeMarkerImpl.java index 00daf95631a6..0f372599fc5d 100644 --- a/platform/platform-impl/src/com/intellij/openapi/editor/impl/RangeMarkerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/editor/impl/RangeMarkerImpl.java @@ -21,12 +21,11 @@ import com.intellij.openapi.editor.ex.DocumentEx; import com.intellij.openapi.editor.ex.RangeMarkerEx; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.UserDataHolderBase; +import com.intellij.util.DistributedCounter; import com.intellij.util.Processor; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; -import java.util.concurrent.atomic.AtomicLong; - public class RangeMarkerImpl extends UserDataHolderBase implements RangeMarkerEx, MutableInterval { private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.editor.impl.RangeMarkerImpl"); @@ -34,8 +33,7 @@ public class RangeMarkerImpl extends UserDataHolderBase implements RangeMarkerEx protected RangeMarkerTree.RMNode myNode; private final long myId; - //private static long counter; - private static final AtomicLong counter = new AtomicLong(); + private static final DistributedCounter counter = new DistributedCounter(); protected RangeMarkerImpl(@NotNull DocumentEx document, int start, int end, boolean register) { this(document, start, end, register, false, false); @@ -52,7 +50,7 @@ public class RangeMarkerImpl extends UserDataHolderBase implements RangeMarkerEx } myDocument = document; - myId = counter.getAndIncrement(); + myId = counter.next(); if (register) { registerInTree(start, end, greedyToLeft, greedyToRight, 0); } diff --git a/platform/util/src/com/intellij/util/DistributedCounter.java b/platform/util/src/com/intellij/util/DistributedCounter.java new file mode 100644 index 000000000000..b2da0cd094b3 --- /dev/null +++ b/platform/util/src/com/intellij/util/DistributedCounter.java @@ -0,0 +1,50 @@ +/* + * Copyright 2000-2011 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.util; + +import java.util.concurrent.atomic.AtomicLong; + +/** + * Low-contention counter. + * Repeated calls to {@link #next()} return increasing numbers unique across all calling threads. + */ +public class DistributedCounter { + private static final int CHUNK_SIZE = 1000; + private final AtomicLong nextChunkStart = new AtomicLong(); + // must not ne static + private final ThreadLocal localCounter = new ThreadLocal(); + private static class NextPair { + long nextId; + final long limit; + + private NextPair(long nextId, long limit) { + this.nextId = nextId; + this.limit = limit; + } + } + + public long next() { + NextPair nextPair = localCounter.get(); + if (nextPair == null || nextPair.nextId == nextPair.limit) { + long start = nextChunkStart.getAndAdd(CHUNK_SIZE); + nextPair = new NextPair(start, start + CHUNK_SIZE); + localCounter.set(nextPair); + } + long result = nextPair.nextId; + nextPair.nextId++; + return result; + } +}