summaryrefslogtreecommitdiff
path: root/platform/core-impl/src/com/intellij/openapi/editor/impl/CharArray.java
blob: 1549c778a86f9fae17d0e92c7f7229bd05106a09 (plain)
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
/*
 * Copyright 2000-2014 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.openapi.editor.impl;

import com.intellij.diagnostic.Dumpable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.impl.ApplicationInfoImpl;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.event.DocumentEvent;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.LocalTimeCounter;
import com.intellij.util.text.CharArrayCharSequence;
import com.intellij.util.text.CharArrayUtil;
import com.intellij.util.text.CharSequenceBackedByArray;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.ref.Reference;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author cdr
 */
abstract class CharArray implements CharSequenceBackedByArray, Dumpable {
  private static final boolean CHECK_DOCUMENT_CONSISTENCY = ApplicationManager.getApplication() != null && ApplicationManager.getApplication().isUnitTestMode();
  private static final Logger LOG = Logger.getInstance("#" + CharArray.class.getName());

  @SuppressWarnings("UseOfArchaicSystemPropertyAccessors")
  private static final boolean DISABLE_DEFERRED_PROCESSING = Boolean.getBoolean("idea.document.deny.deferred.changes");

  @SuppressWarnings("UseOfArchaicSystemPropertyAccessors")
  private static final boolean DEBUG_DEFERRED_PROCESSING = LOG.isDebugEnabled() || Boolean.getBoolean("idea.document.debug.bulk.processing");
  /**
   * We can't exclude possibility of situation when <code>'defer changes'</code> state is {@link #setDeferredChangeMode(boolean) entered}
   * but not exited, hence, we want to perform automatic flushing if necessary in order to avoid memory leaks. This constant holds
   * a value that defines that 'automatic flushing' criteria, i.e. every time number of stored deferred changes exceeds this value,
   * they are automatically flushed.
   */
  private static final int MAX_DEFERRED_CHANGES_NUMBER = 10000;

  private final TextChangesStorage myDeferredChangesStorage;

  private volatile int myStart; // start offset in myArray (used as an optimization when call substring())
  private volatile int myCount;

  private volatile CharSequence myOriginalSequence;
  private volatile char[] myArray;
  private volatile Reference<String> myStringRef; // buffers String value - for not to generate it every time
  private volatile int myBufferSize;
  private volatile int myDeferredShift;
  private volatile boolean myDeferredChangeMode;
  private volatile boolean myHasDeferredChanges;
  // this lock is for mutual exclusion during read action access
  // (some fields are changed in read action too)
  private final Lock lock = new ReentrantLock();

  // We had a problems with bulk document text processing, hence, debug facilities were introduced. The fields group below work with them.
  // The main idea is to hold all history of bulk processing iteration in order to be able to retrieve it from client and reproduce the
  // problem.
  
  private final boolean myDebug = isDebug();

  boolean isDebug() {
    return DEBUG_DEFERRED_PROCESSING || CHECK_DOCUMENT_CONSISTENCY && !ApplicationInfoImpl.isInPerformanceTest();
  }

  /**
   * Duplicate instance of the current char array that is used during debug processing as follows - apply every text change
   * from the bulk changes group to this instance immediately in order to be able to check if the current 'deferred change-aware'
   * instance functionally behaves at the same way as 'straightforward' one.
   */
  private CharArray myDebugArray;

  /**
   * Holds deferred changes create during the current bulk processing iteration.
   */
  private List<TextChangeImpl> myDebugDeferredChanges;

  /**
   * Document text on bulk processing start.
   */
  private String myDebugTextOnBatchUpdateStart;

  // bufferSize == 0 means unbounded
  CharArray(final int bufferSize, @NotNull char[] data, int length) {
    myBufferSize = bufferSize;
    myDeferredChangesStorage = new TextChangesStorage();
    myArray = Arrays.copyOf(data, length);
    myCount = length;

    if (myDebug) {
      myDebugArray = new CharArray(bufferSize, data, length) {
        @NotNull
        @Override
        protected DocumentEvent beforeChangedUpdate(int offset,
                                                    CharSequence oldString,
                                                    CharSequence newString,
                                                    boolean wholeTextReplaced) {
          return CharArray.this.beforeChangedUpdate(offset, oldString, newString, wholeTextReplaced);
        }

        @Override
        protected void afterChangedUpdate(@NotNull DocumentEvent event, long newModificationStamp) {
        }

        @Override
        protected void assertWriteAccess() {
        }

        @Override
        protected void assertReadAccess() {
        }

        @Override
        boolean isDebug() {
          return false;
        }
      };
      myDebugDeferredChanges = new ArrayList<TextChangeImpl>();
    }
    assertConsistency();
  }

  public void setBufferSize(int bufferSize) {
    assert bufferSize >= 0 : bufferSize;
    myBufferSize = bufferSize;
    assertConsistency();
  }

  private DocumentEvent startChange(int offset,
                                    @Nullable CharSequence oldString,
                                    @Nullable CharSequence newString,
                                    boolean wholeTextReplaced) {
    assert myStart == 0; // can't change substring
    assertWriteAccess();
    assertConsistency();

    return beforeChangedUpdate(offset, oldString, newString, wholeTextReplaced);
  }

  @NotNull
  protected abstract DocumentEvent beforeChangedUpdate(int offset,
                                                       @Nullable CharSequence oldString,
                                                       @Nullable CharSequence newString,
                                                       boolean wholeTextReplaced);
  protected abstract void afterChangedUpdate(@NotNull DocumentEvent event, long newModificationStamp);

  protected abstract void assertWriteAccess();
  protected abstract void assertReadAccess();

  private void setText(@NotNull CharSequence chars) {
    assertConsistency();
    myOriginalSequence = chars.toString();
    myArray = null;
    myStringRef = null;
    myCount = chars.length();
    assert myStart == 0; // can't change substring
    myDeferredChangesStorage.clear();
    myHasDeferredChanges = false;
    trimToSize();

    if (myDebug) {
      myDebugArray.setText(chars);
      myDebugDeferredChanges.clear();
    }
    assertConsistency();
  }

  private void assertConsistency() {
    if (isDeferredChangeMode()) {
      assert myOriginalSequence == null;
    }
    CharSequence originalSequence = myOriginalSequence;
    int origLen = originalSequence == null ? -1 : originalSequence.length();
    String string = com.intellij.reference.SoftReference.dereference(myStringRef);
    int stringLen = string == null ? -1 : string.length();
    assert origLen == stringLen || origLen==-1 || stringLen==-1;

    int count = myCount + myDeferredShift;
    assert count == origLen || origLen==-1;
    assert count == stringLen || stringLen==-1;

    if (!myDebug) return;
    final CharSequence seqFromCharArray;

    if (myArray != null) {
      assert myCount <= myArray.length;
      seqFromCharArray = new CharArrayCharSequence(myArray, myStart, myCount);
    }
    else {
      seqFromCharArray = null;
    }

    if (seqFromCharArray != null && originalSequence != null) {
      assert StringUtil.equals(seqFromCharArray, originalSequence);
    }
    if (!isDeferredChangeMode() && seqFromCharArray != null && string != null) {
      assert StringUtil.equals(seqFromCharArray, string);
    }
    if (originalSequence != null && string != null) {
      assert string.equals(originalSequence.toString());
    }

    myDebugArray.assertConsistency();

    CharSequence str = com.intellij.reference.SoftReference.dereference(myStringRef);
    if (str == null) {
      if (myHasDeferredChanges) {
        str = doSubString(0, myCount + myDeferredShift).toString();
      }
      else if (myOriginalSequence != null) {
        str = myOriginalSequence.toString();
      }
      else {
        str = seqFromCharArray;
      }
    }
    assert count == str.length();
    if (isDeferredChangeMode()) {
      String expected = myDebugArray.toString();
      checkStrings("toString()", expected, str);
    }
  }

  public void replace(int startOffset,
                      int endOffset,
                      @NotNull CharSequence toDelete,
                      @NotNull CharSequence newString,
                      long newModificationStamp,
                      boolean wholeTextReplaced) {
    final DocumentEvent event = startChange(startOffset, toDelete, newString, wholeTextReplaced);

    startOffset += myStart;
    endOffset += myStart;
    doReplace(startOffset, endOffset, newString);
    afterChangedUpdate(event, newModificationStamp);
    assertConsistency();
  }

  private void doReplace(int startOffset, int endOffset, @NotNull CharSequence newString) {
    prepareForModification();

    if (isDeferredChangeMode()) {
      storeChange(new TextChangeImpl(newString, startOffset, endOffset));
      if (myDebug) {
        myDebugArray.doReplace(startOffset, endOffset, newString);
      }
    }
    else {
      int newLength = newString.length();
      int oldLength = endOffset - startOffset;

      CharArrayUtil.getChars(newString, myArray, startOffset, Math.min(newLength, oldLength));
      myStringRef = null;

      if (newLength > oldLength) {
        doInsert(newString.subSequence(oldLength, newLength), endOffset);
      }
      else if (newLength < oldLength) {
        doRemove(startOffset + newLength, startOffset + oldLength);
      }
    }
  }

  public void remove(int startIndex, int endIndex, @NotNull CharSequence toDelete) {
    DocumentEvent event = startChange(startIndex, toDelete, null, false);
    startIndex += myStart;
    endIndex += myStart;
    doRemove(startIndex, endIndex);
    afterChangedUpdate(event, LocalTimeCounter.currentTime());
    assertConsistency();
  }

  private void doRemove(int startIndex, int endIndex) {
    if (startIndex == endIndex) {
      return;
    }
    prepareForModification();

    if (isDeferredChangeMode()) {
      storeChange(new TextChangeImpl("", startIndex, endIndex));
      if (myDebug) {
        myDebugArray.doRemove(startIndex, endIndex);
      }
    }
    else {
      if (endIndex < myCount) {
        System.arraycopy(myArray, endIndex, myArray, startIndex, myCount - endIndex);
        myStringRef = null;
      }
      myCount -= endIndex - startIndex;
    }
  }

  public void insert(@NotNull CharSequence s, int startIndex) {
    DocumentEvent event = startChange(startIndex, null, s, false);
    startIndex += myStart;
    doInsert(s, startIndex);

    afterChangedUpdate(event, LocalTimeCounter.currentTime());
    trimToSize();
    assertConsistency();
  }

  private void doInsert(@NotNull CharSequence s, final int startIndex) {
    prepareForModification();

    if (isDeferredChangeMode()) {
      storeChange(new TextChangeImpl(s, startIndex));
      if (myDebug) {
        myDebugArray.doInsert(s, startIndex);
      }
    }
    else {
      int insertLength = s.length();
      myArray = resizeArray(myArray, myCount + insertLength);
      if (startIndex < myCount) {
        System.arraycopy(myArray, startIndex, myArray, startIndex + insertLength, myCount - startIndex);
      }

      CharArrayUtil.getChars(s, myArray, startIndex);
      myCount += insertLength;
      myStringRef = null;
    }
  }

  /**
   * Stores given change at collection of deferred changes (merging it with others if necessary) and updates current object
   * state ({@link #length() length} etc).
   *
   * @param change      new change to store
   */
  private void storeChange(@NotNull TextChangeImpl change) {
    if (!change.isWithinBounds(length())) {
      LOG.error(
        "Invalid change attempt detected - given change bounds are not within the current char array. Change: " +
        change.getText().length()+":" + change.getStart()+"-" + change.getEnd(), dumpState());
      return;
    }
    if (myDeferredChangesStorage.size() >= MAX_DEFERRED_CHANGES_NUMBER) {
      flushDeferredChanged();
    }
    myDeferredChangesStorage.store(change);
    myHasDeferredChanges = true;
    myDeferredShift += change.getDiff();

    if (myDebug) {
      myDebugDeferredChanges.add(change);
    }
  }

  private void prepareForModification() {
    if (myOriginalSequence != null) {
      myArray = new char[myOriginalSequence.length()];
      CharArrayUtil.getChars(myOriginalSequence, myArray, 0);
      myCount = myArray.length;
      myOriginalSequence = null;
      myStart = 0;
    }
    myStringRef = null;

    assertConsistency();
  }

  @NotNull
  public CharSequence getCharArray() {
    assertConsistency();
    CharSequence originalSequence = myOriginalSequence;
    return originalSequence == null ? this : originalSequence;
  }

  @NotNull
  public String toString() {
    assertConsistency();
    String str = com.intellij.reference.SoftReference.dereference(myStringRef);
    if (str == null) {
      if (myHasDeferredChanges) {
        str = substring(0, length()).toString();
      }
      else {
        str = myOriginalSequence == null ? new String(myArray, myStart, myCount) : myOriginalSequence.toString();
      }
      myStringRef = new SoftReference<String>(str);
    }
    return str;
  }

  @Override
  public final int length() {
    final int result = myCount + myDeferredShift;
    if (myDebug && isDeferredChangeMode()) {
      int expected = myDebugArray.length();
      if (expected != result) {
        dumpDebugInfo("Incorrect length() processing. Expected: '" + expected + "', actual: '" + result + "'");
      }
    }
    return result;
  }

  @Override
  public final char charAt(int i) {
    if (i < 0 || i >= length()) {
      throw new IndexOutOfBoundsException("Wrong offset: " + i + "; count:" + length());
    }
    i += myStart;
    final char result;
    if (!myHasDeferredChanges) {
      if (myOriginalSequence != null) {
        result = myOriginalSequence.charAt(i);
      }
      else {
        result = myArray[i];
      }
    }
    else {
      result = myDeferredChangesStorage.charAt(myArray, i);
    }

    if (myDebug && isDeferredChangeMode()) {
      char expected = myDebugArray.charAt(i);
      if (expected != result) {
        dumpDebugInfo("Incorrect charAt() processing for index " + i + ". Expected: '" + expected + "', actual: '" + result + "'");
      }
    }
    return result;
  }

  @Override
  @NotNull
  public CharSequence subSequence(final int start, final int end) {
    assertReadAccess();
    assertConsistency();
    if (start == 0 && end == length()) return this;
    if (myOriginalSequence != null) {
      return myOriginalSequence.subSequence(start, end);
    }
    flushDeferredChanged();
    return new CharArrayCharSequence(myArray, start, end);
  }

  @Override
  @NotNull
  public char[] getChars() {
    assertReadAccess();
    assertConsistency();
    char[] array = myArray;
    CharSequence originalSequence = myOriginalSequence;
    if (myHasDeferredChanges || originalSequence != null && array == null) {
      // slow track
      lock.lock();
      try {
        flushDeferredChanged();
        if (myOriginalSequence != null && myArray == null) {
          myArray = array = CharArrayUtil.fromSequence(myOriginalSequence);
          myStringRef = null;
        }
      }
      finally {
        lock.unlock();
      }
      assertConsistency();
    }
    return array;
  }

  @Override
  public void getChars(@NotNull final char[] dst, final int dstOffset) {
    assertReadAccess();
    assertConsistency();
    flushDeferredChanged();
    if (myOriginalSequence == null) {
      System.arraycopy(myArray, myStart, dst, dstOffset, length());
    }
    else {
      CharArrayUtil.getChars(myOriginalSequence, dst, dstOffset);
    }

    if (myDebug && isDeferredChangeMode()) {
      char[] expected = new char[dst.length];
      myDebugArray.getChars(expected, dstOffset);
      for (int i = dstOffset, j = myStart; i < dst.length && j < myArray.length; i++, j++) {
        if (expected[i] != myArray[j]) {
          dumpDebugInfo("getChars(char[], int). Given array of length " + dst.length + ", offset " + dstOffset + ". Found char '" + myArray[j] +
                        "' at index " + i + ", expected to find '" + expected[i] + "'");
          break;
        }
      }
    }
  }

  @NotNull
  public CharSequence substring(final int start, final int end) {
    assertReadAccess();
    final CharSequence result = doSubString(start, end);

    assertConsistency();
    return result;
  }

  private CharSequence doSubString(int start, int end) {
    if (start == end) return "";
    final CharSequence result;
    if (myOriginalSequence == null) {
      result = myDeferredChangesStorage.substring(myArray, start + myStart, end + myStart);
    }
    else {
      result = myOriginalSequence.subSequence(start, end);
    }
    return result;
  }

  @NotNull
  private static char[] resizeArray(@NotNull char[] array, int newSize) {
    if (newSize < array.length) {
      return array;
    }

    int newArraySize = array.length;
    if (newArraySize == 0) {
      newArraySize = 16;
    }
    while (newArraySize <= newSize) {
      newArraySize = newArraySize * 12 / 10 + 1;
    }
    char[] newArray = new char[newArraySize];
    System.arraycopy(array, 0, newArray, 0, array.length);
    return newArray;
  }

  private void trimToSize() {
    if (myBufferSize != 0 && length() > myBufferSize) {
      flushDeferredChanged();

      // make a copy
      int endIndex = myCount - myBufferSize;
      String toDelete = getCharArray().subSequence(0, endIndex).toString();
      remove(0, endIndex, toDelete);
    }
  }

  /**
   * @return    <code>true</code> if this object is in the defer changes mode, see {@link #setDeferredChangeMode(boolean)};
   */
  public boolean isDeferredChangeMode() {
    return myDeferredChangeMode;
  }

  public boolean hasDeferredChanges() {
    return myHasDeferredChanges;
  }
  
  /**
   * There is a possible case that client of this class wants to perform great number of modifications in a short amount of time
   * (e.g. end-user performs formatting of the document backed by the object of the current class). It may result in significant
   * performance degradation is the changes are performed one by one (every time the change is applied tail content is shifted to
   * the left or right). So, we may want to optimize that by avoiding actual array modification until information about
   * all target changes is provided and perform array data moves only after that.
   * <p/>
   * This method allows to define that <code>'defer changes'</code> mode usages, i.e. expected usage pattern is as follows:
   * <pre>
   * <ol>
   *   <li>
   *     Client of this class enters <code>'defer changes'</code> mode (calls this method with <code>'true'</code> argument).
   *     That means that all subsequent changes will not actually modify backed array data and will be stored separately;
   *   </li>
   *   <li>
   *     Number of target changes are applied to the current object via standard API
   *     ({@link #insert(CharSequence, int) insert},
   *     {@link #remove(int, int, CharSequence) remove} and
   *     {@link #replace(int, int, java.lang.CharSequence, java.lang.CharSequence, long, boolean)});
   *   </li>
   *   <li>
   *     Client of this class indicates that <code>'massive change time'</code> is over by calling this method with <code>'false'</code>
   *     argument. That flushes all deferred changes (if any) to the backed data array and makes every subsequent change to
   *     be immediate flushed to the backed array;
   *   </li>
   * </ol>
   * </pre>
   * <p/>
   * <b>Note:</b> we can't exclude possibility that <code>'defer changes'</code> mode is started but inadvertently not ended
   * (due to programming error, unexpected exception etc). Hence, this class is free to automatically end
   * <code>'defer changes'</code> mode when necessary in order to avoid memory leak with infinite deferred changes storing.
   * 
   * @param deferredChangeMode    flag that defines if <code>'defer changes'</code> mode should be used by the current object
   */
  public void setDeferredChangeMode(boolean deferredChangeMode) {
    if (!DISABLE_DEFERRED_PROCESSING) {
      if (deferredChangeMode) {
        if (myDebug) {
          myDebugArray.setText(myDebugTextOnBatchUpdateStart = toString());
          myDebugDeferredChanges.clear();
        }
        prepareForModification();
        myDeferredChangeMode = deferredChangeMode;
      }
      else {
        myDeferredChangeMode = deferredChangeMode;
        flushDeferredChanged();
      }
    }
    assertConsistency();
  }

  private void flushDeferredChanged() {
    List<TextChangeImpl> changes = myDeferredChangesStorage.getChanges();
    if (changes.isEmpty()) {
      return;
    }

    lock.lock();
    try {
      char[] beforeMerge = null;
      if (myDebug) {
        beforeMerge = new char[myArray.length];
        System.arraycopy(myArray, 0, beforeMerge, 0, myArray.length);
      }

      BulkChangesMerger changesMerger = BulkChangesMerger.INSTANCE;
      final boolean inPlace;
      if (myArray.length < length()) {
        myArray = changesMerger.mergeToCharArray(myArray, myCount, changes);
        inPlace = false;
      }
      else {
        changesMerger.mergeInPlace(myArray, myCount, changes);
        inPlace = true;
      }

      myCount += myDeferredShift;
      myDeferredShift = 0;
      myDeferredChangesStorage.clear();
      myHasDeferredChanges = false;
      myDeferredChangeMode = false;
      myStringRef = null;

      if (myDebug) {
        for (int i = 0, max = length(); i < max; i++) {
          if (myArray[i] != myDebugArray.myArray[i]) {
            dumpDebugInfo("flushDeferredChanged(). Index " + i + ", expected: '" + myDebugArray.myArray[i]+"', actual '" +
                          myArray[i]+"'. Text before merge: '" + Arrays.toString(beforeMerge)+"', merge inplace: "+inPlace);
            break;
          }
        }
      }
    }
    finally {
      lock.unlock();
    }
    assertConsistency();
  }

  @Override
  @NonNls
  @NotNull
  public String dumpState() {
    return "deferred changes mode: " + isDeferredChangeMode()+", length: " + length()+" (data array length: " + myCount+
           ", deferred shift: " + myDeferredShift+"); view offsets: [" + myStart+"; "+myCount+"]; deferred changes: "+myDeferredChangesStorage;
  }
  
  private void checkStrings(@NonNls @NotNull String operation, @NotNull String expected, @NotNull CharSequence actual) {
    if (StringUtil.equals(expected, actual)) {
      return;
    }
    for (int i = 0, max = Math.min(expected.length(), actual.length()); i < max; i++) {
      if (actual.charAt(i) != expected.charAt(i)) {
        dumpDebugInfo(
          "Incorrect " +
          operation+" processing. Expected length: " +
          expected.length()+", actual length: " +
          actual.length()+". Unmatched symbol at " +
          i+" - expected: '" +
          expected.charAt(i)+"', " +
          "actual: '" +
          actual.charAt(i)+"', expected document: '" +
          expected+"', actual document: '" +
          actual+"'"
        );
        return;
      }
    }
    dumpDebugInfo("Incorrect " + operation+" processing. Expected length: " + expected.length()+", actual length: " +
      actual.length()+", expected: '" + expected+"', actual: '" + actual+"'");
  }

  private void dumpDebugInfo(@NonNls @NotNull String problem) {
    LOG.error(
      "Incorrect CharArray processing detected: " + problem +
      ". Start: " + myStart
      + ", count: " + myCount + ", text on batch update start: " +
      myDebugTextOnBatchUpdateStart + ", deferred changes history: " +
      myDebugDeferredChanges + ", current deferred changes: " + myDeferredChangesStorage
    );
  }
}