1 /* Caching facts about regions of the buffer, for optimization.
2 Copyright (C) 1985, 1986, 1987, 1988, 1989, 1993, 1995
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
25 #include "region-cache.h"
30 /* Data structures. */
34 We want something that maps character positions in a buffer onto
35 values. The representation should deal well with long runs of
36 characters with the same value.
38 The tricky part: the representation should be very cheap to
39 maintain in the presence of many insertions and deletions. If the
40 overhead of maintaining the cache is too high, the speedups it
41 offers will be worthless.
44 We represent the region cache as a sorted array of struct
45 boundary's, each of which contains a buffer position and a value;
46 the value applies to all the characters after the buffer position,
47 until the position of the next boundary, or the end of the buffer.
49 The cache always has a boundary whose position is BUF_BEG, so
50 there's always a value associated with every character in the
51 buffer. Since the cache is sorted, this is always the first
54 To facilitate the insertion and deletion of boundaries in the
55 cache, the cache has a gap, just like Emacs's text buffers do.
57 To help boundary positions float along with insertions and
58 deletions, all boundary positions before the cache gap are stored
59 relative to BUF_BEG (buf) (thus they're >= 0), and all boundary
60 positions after the gap are stored relative to BUF_Z (buf) (thus
61 they're <= 0). Look at BOUNDARY_POS to see this in action. See
62 revalidate_region_cache to see how this helps. */
70 /* A sorted array of locations where the known-ness of the buffer
72 struct boundary
*boundaries
;
74 /* boundaries[gap_start ... gap_start + gap_len - 1] is the gap. */
75 int gap_start
, gap_len
;
77 /* The number of elements allocated to boundaries, not including the
81 /* The areas that haven't changed since the last time we cleaned out
82 invalid entries from the cache. These overlap when the buffer is
83 entirely unchanged. */
84 int beg_unchanged
, end_unchanged
;
86 /* The first and last positions in the buffer. Because boundaries
87 store their positions relative to the start (BEG) and end (Z) of
88 the buffer, knowing these positions allows us to accurately
89 interpret positions without having to pass the buffer structure
90 or its endpoints around all the time.
92 Yes, buffer_beg is always 1. It's there for symmetry with
93 buffer_end and the BEG and BUF_BEG macros. */
94 int buffer_beg
, buffer_end
;
97 /* Return the position of boundary i in cache c. */
98 #define BOUNDARY_POS(c, i) \
99 ((i) < (c)->gap_start \
100 ? (c)->buffer_beg + (c)->boundaries[(i)].pos \
101 : (c)->buffer_end + (c)->boundaries[(c)->gap_len + (i)].pos)
103 /* Return the value for text after boundary i in cache c. */
104 #define BOUNDARY_VALUE(c, i) \
105 ((i) < (c)->gap_start \
106 ? (c)->boundaries[(i)].value \
107 : (c)->boundaries[(c)->gap_len + (i)].value)
109 /* Set the value for text after boundary i in cache c to v. */
110 #define SET_BOUNDARY_VALUE(c, i, v) \
111 ((i) < (c)->gap_start \
112 ? ((c)->boundaries[(i)].value = (v))\
113 : ((c)->boundaries[(c)->gap_len + (i)].value = (v)))
116 /* How many elements to add to the gap when we resize the buffer. */
117 #define NEW_CACHE_GAP (40)
119 /* See invalidate_region_cache; if an invalidation would throw away
120 information about this many characters, call
121 revalidate_region_cache before doing the new invalidation, to
122 preserve that information, instead of throwing it away. */
123 #define PRESERVE_THRESHOLD (500)
125 static void revalidate_region_cache ();
128 /* Interface: Allocating, initializing, and disposing of region caches. */
130 struct region_cache
*
133 struct region_cache
*c
134 = (struct region_cache
*) xmalloc (sizeof (struct region_cache
));
137 c
->gap_len
= NEW_CACHE_GAP
;
140 (struct boundary
*) xmalloc ((c
->gap_len
+ c
->cache_len
)
141 * sizeof (*c
->boundaries
));
143 c
->beg_unchanged
= 0;
144 c
->end_unchanged
= 0;
148 /* Insert the boundary for the buffer start. */
152 c
->boundaries
[0].pos
= 0; /* from buffer_beg */
153 c
->boundaries
[0].value
= 0;
159 free_region_cache (c
)
160 struct region_cache
*c
;
162 xfree (c
->boundaries
);
167 /* Finding positions in the cache. */
169 /* Return the index of the last boundary in cache C at or before POS.
170 In other words, return the boundary that specifies the value for
171 the region POS..(POS + 1).
173 This operation should be logarithmic in the number of cache
174 entries. It would be nice if it took advantage of locality of
175 reference, too, by searching entries near the last entry found. */
177 find_cache_boundary (c
, pos
)
178 struct region_cache
*c
;
181 int low
= 0, high
= c
->cache_len
;
183 while (low
+ 1 < high
)
185 /* mid is always a valid index, because low < high and ">> 1"
187 int mid
= (low
+ high
) >> 1;
188 int boundary
= BOUNDARY_POS (c
, mid
);
197 if (BOUNDARY_POS (c
, low
) > pos
198 || (low
+ 1 < c
->cache_len
199 && BOUNDARY_POS (c
, low
+ 1) <= pos
))
207 /* Moving the cache gap around, inserting, and deleting. */
210 /* Move the gap of cache C to index POS, and make sure it has space
211 for at least MIN_SIZE boundaries. */
213 move_cache_gap (c
, pos
, min_size
)
214 struct region_cache
*c
;
218 /* Copy these out of the cache and into registers. */
219 int gap_start
= c
->gap_start
;
220 int gap_len
= c
->gap_len
;
221 int buffer_beg
= c
->buffer_beg
;
222 int buffer_end
= c
->buffer_end
;
225 || pos
> c
->cache_len
)
228 /* We mustn't ever try to put the gap before the dummy start
229 boundary. That must always be start-relative. */
233 /* Need we move the gap right? */
234 while (gap_start
< pos
)
236 /* Copy one boundary from after to before the gap, and
237 convert its position to start-relative. */
238 c
->boundaries
[gap_start
].pos
240 + c
->boundaries
[gap_start
+ gap_len
].pos
242 c
->boundaries
[gap_start
].value
243 = c
->boundaries
[gap_start
+ gap_len
].value
;
247 /* To enlarge the gap, we need to re-allocate the boundary array, and
248 then shift the area after the gap to the new end. Since the cost
249 is proportional to the amount of stuff after the gap, we do the
250 enlargement here, after a right shift but before a left shift,
251 when the portion after the gap is smallest. */
252 if (gap_len
< min_size
)
256 /* Always make at least NEW_CACHE_GAP elements, as long as we're
258 if (min_size
< NEW_CACHE_GAP
)
259 min_size
= NEW_CACHE_GAP
;
262 (struct boundary
*) xrealloc (c
->boundaries
,
263 ((min_size
+ c
->cache_len
)
264 * sizeof (*c
->boundaries
)));
266 /* Some systems don't provide a version of the copy routine that
267 can be trusted to shift memory upward into an overlapping
268 region. memmove isn't widely available. */
270 for (i
= c
->cache_len
- 1; i
>= gap_start
; i
--)
272 c
->boundaries
[i
+ min_size
].pos
= c
->boundaries
[i
+ gap_len
].pos
;
273 c
->boundaries
[i
+ min_size
].value
= c
->boundaries
[i
+ gap_len
].value
;
279 /* Need we move the gap left? */
280 while (pos
< gap_start
)
284 /* Copy one region from before to after the gap, and
285 convert its position to end-relative. */
286 c
->boundaries
[gap_start
+ gap_len
].pos
287 = c
->boundaries
[gap_start
].pos
+ buffer_beg
- buffer_end
;
288 c
->boundaries
[gap_start
+ gap_len
].value
289 = c
->boundaries
[gap_start
].value
;
292 /* Assign these back into the cache. */
293 c
->gap_start
= gap_start
;
294 c
->gap_len
= gap_len
;
298 /* Insert a new boundary in cache C; it will have cache index INDEX,
299 and have the specified POS and VALUE. */
301 insert_cache_boundary (c
, index
, pos
, value
)
302 struct region_cache
*c
;
306 /* index must be a valid cache index. */
307 if (index
< 0 || index
> c
->cache_len
)
310 /* We must never want to insert something before the dummy first
315 /* We must only be inserting things in order. */
316 if (! (BOUNDARY_POS (c
, index
-1) < pos
317 && (index
== c
->cache_len
318 || pos
< BOUNDARY_POS (c
, index
))))
321 /* The value must be different from the ones around it. However, we
322 temporarily create boundaries that establish the same value as
323 the subsequent boundary, so we're not going to flag that case. */
324 if (BOUNDARY_VALUE (c
, index
-1) == value
)
327 move_cache_gap (c
, index
, 1);
329 c
->boundaries
[index
].pos
= pos
- c
->buffer_beg
;
330 c
->boundaries
[index
].value
= value
;
337 /* Delete the i'th entry from cache C if START <= i < END. */
340 delete_cache_boundaries (c
, start
, end
)
341 struct region_cache
*c
;
344 int len
= end
- start
;
346 /* Gotta be in range. */
348 || end
> c
->cache_len
)
351 /* Gotta be in order. */
355 /* Can't delete the dummy entry. */
360 /* Minimize gap motion. If we're deleting nothing, do nothing. */
363 /* If the gap is before the region to delete, delete from the start
365 else if (c
->gap_start
<= start
)
367 move_cache_gap (c
, start
, 0);
370 /* If the gap is after the region to delete, delete from the end
372 else if (end
<= c
->gap_start
)
374 move_cache_gap (c
, end
, 0);
378 /* If the gap is in the region to delete, just expand it. */
381 c
->gap_start
= start
;
390 /* Set the value for a region. */
392 /* Set the value in cache C for the region START..END to VALUE. */
394 set_cache_region (c
, start
, end
, value
)
395 struct region_cache
*c
;
401 if (start
< c
->buffer_beg
402 || end
> c
->buffer_end
)
405 /* Eliminate this case; then we can assume that start and end-1 are
406 both the locations of real characters in the buffer. */
411 /* We need to make sure that there are no boundaries in the area
412 between start to end; the whole area will have the same value,
413 so those boundaries will not be necessary.
415 Let start_ix be the cache index of the boundary governing the
416 first character of start..end, and let end_ix be the cache
417 index of the earliest boundary after the last character in
418 start..end. (This tortured terminology is intended to answer
419 all the "< or <=?" sort of questions.) */
420 int start_ix
= find_cache_boundary (c
, start
);
421 int end_ix
= find_cache_boundary (c
, end
- 1) + 1;
423 /* We must remember the value established by the last boundary
424 before end; if that boundary's domain stretches beyond end,
425 we'll need to create a new boundary at end, and that boundary
426 must have that remembered value. */
427 int value_at_end
= BOUNDARY_VALUE (c
, end_ix
- 1);
429 /* Delete all boundaries strictly within start..end; this means
430 those whose indices are between start_ix (exclusive) and end_ix
432 delete_cache_boundaries (c
, start_ix
+ 1, end_ix
);
434 /* Make sure we have the right value established going in to
435 start..end from the left, and no unnecessary boundaries. */
436 if (BOUNDARY_POS (c
, start_ix
) == start
)
438 /* Is this boundary necessary? If no, remove it; if yes, set
441 && BOUNDARY_VALUE (c
, start_ix
- 1) == value
)
443 delete_cache_boundaries (c
, start_ix
, start_ix
+ 1);
447 SET_BOUNDARY_VALUE (c
, start_ix
, value
);
451 /* Do we need to add a new boundary here? */
452 if (BOUNDARY_VALUE (c
, start_ix
) != value
)
454 insert_cache_boundary (c
, start_ix
+ 1, start
, value
);
459 /* This is equivalent to letting end_ix float (like a buffer
460 marker does) with the insertions and deletions we may have
462 end_ix
= start_ix
+ 1;
464 /* Make sure we have the correct value established as we leave
465 start..end to the right. */
466 if (end
== c
->buffer_end
)
467 /* There is no text after start..end; nothing to do. */
469 else if (end_ix
>= c
->cache_len
470 || end
< BOUNDARY_POS (c
, end_ix
))
472 /* There is no boundary at end, but we may need one. */
473 if (value_at_end
!= value
)
474 insert_cache_boundary (c
, end_ix
, end
, value_at_end
);
478 /* There is a boundary at end; should it be there? */
479 if (value
== BOUNDARY_VALUE (c
, end_ix
))
480 delete_cache_boundaries (c
, end_ix
, end_ix
+ 1);
487 /* Interface: Invalidating the cache. Private: Re-validating the cache. */
489 /* Indicate that a section of BUF has changed, to invalidate CACHE.
490 HEAD is the number of chars unchanged at the beginning of the buffer.
491 TAIL is the number of chars unchanged at the end of the buffer.
492 NOTE: this is *not* the same as the ending position of modified
494 (This way of specifying regions makes more sense than absolute
495 buffer positions in the presence of insertions and deletions; the
496 args to pass are the same before and after such an operation.) */
498 invalidate_region_cache (buf
, c
, head
, tail
)
500 struct region_cache
*c
;
503 /* Let chead = c->beg_unchanged, and
504 ctail = c->end_unchanged.
505 If z-tail < beg+chead by a large amount, or
506 z-ctail < beg+head by a large amount,
508 then cutting back chead and ctail to head and tail would lose a
509 lot of information that we could preserve by revalidating the
510 cache before processing this invalidation. Losing that
511 information may be more costly than revalidating the cache now.
512 So go ahead and call revalidate_region_cache if it seems that it
513 might be worthwhile. */
514 if (((BUF_BEG (buf
) + c
->beg_unchanged
) - (BUF_Z (buf
) - tail
)
515 > PRESERVE_THRESHOLD
)
516 || ((BUF_BEG (buf
) + head
) - (BUF_Z (buf
) - c
->end_unchanged
)
517 > PRESERVE_THRESHOLD
))
518 revalidate_region_cache (buf
, c
);
521 if (head
< c
->beg_unchanged
)
522 c
->beg_unchanged
= head
;
523 if (tail
< c
->end_unchanged
)
524 c
->end_unchanged
= tail
;
526 /* We now know nothing about the region between the unchanged head
527 and the unchanged tail (call it the "modified region"), not even
530 If the modified region has shrunk in size (deletions do this),
531 then the cache may now contain boundaries originally located in
532 text that doesn't exist any more.
534 If the modified region has increased in size (insertions do
535 this), then there may now be boundaries in the modified region
536 whose positions are wrong.
538 Even calling BOUNDARY_POS on boundaries still in the unchanged
539 head or tail may well give incorrect answers now, since
540 c->buffer_beg and c->buffer_end may well be wrong now. (Well,
541 okay, c->buffer_beg never changes, so boundaries in the unchanged
542 head will still be okay. But it's the principle of the thing.)
544 So things are generally a mess.
546 But we don't clean up this mess here; that would be expensive,
547 and this function gets called every time any buffer modification
548 occurs. Rather, we can clean up everything in one swell foop,
549 accounting for all the modifications at once, by calling
550 revalidate_region_cache before we try to consult the cache the
555 /* Clean out any cache entries applying to the modified region, and
556 make the positions of the remaining entries accurate again.
558 After calling this function, the mess described in the comment in
559 invalidate_region_cache is cleaned up.
561 This function operates by simply throwing away everything it knows
562 about the modified region. It doesn't care exactly which
563 insertions and deletions took place; it just tosses it all.
565 For example, if you insert a single character at the beginning of
566 the buffer, and a single character at the end of the buffer (for
567 example), without calling this function in between the two
568 insertions, then the entire cache will be freed of useful
569 information. On the other hand, if you do manage to call this
570 function in between the two insertions, then the modified regions
571 will be small in both cases, no information will be tossed, and the
572 cache will know that it doesn't have knowledge of the first and
573 last characters any more.
575 Calling this function may be expensive; it does binary searches in
576 the cache, and causes cache gap motion. */
579 revalidate_region_cache (buf
, c
)
581 struct region_cache
*c
;
583 /* The boundaries now in the cache are expressed relative to the
584 buffer_beg and buffer_end values stored in the cache. Now,
585 buffer_beg and buffer_end may not be the same as BUF_BEG (buf)
586 and BUF_Z (buf), so we have two different "bases" to deal with
587 --- the cache's, and the buffer's. */
589 /* If the entire buffer is still valid, don't waste time. Yes, this
590 should be a >, not a >=; think about what beg_unchanged and
591 end_unchanged get set to when the only change has been an
593 if (c
->buffer_beg
+ c
->beg_unchanged
594 > c
->buffer_end
- c
->end_unchanged
)
597 /* If all the text we knew about as of the last cache revalidation
598 is still there, then all of the information in the cache is still
599 valid. Because c->buffer_beg and c->buffer_end are out-of-date,
600 the modified region appears from the cache's point of view to be
601 a null region located someplace in the buffer.
603 Now, invalidating that empty string will have no actual affect on
604 the cache; instead, we need to update the cache's basis first
605 (which will give the modified region the same size in the cache
606 as it has in the buffer), and then invalidate the modified
608 if (c
->buffer_beg
+ c
->beg_unchanged
609 == c
->buffer_end
- c
->end_unchanged
)
611 /* Move the gap so that all the boundaries in the unchanged head
612 are expressed beg-relative, and all the boundaries in the
613 unchanged tail are expressed end-relative. That done, we can
614 plug in the new buffer beg and end, and all the positions
617 The boundary which has jurisdiction over the modified region
618 should be left before the gap. */
620 (find_cache_boundary (c
, (c
->buffer_beg
625 c
->buffer_beg
= BUF_BEG (buf
);
626 c
->buffer_end
= BUF_Z (buf
);
628 /* Now that the cache's basis has been changed, the modified
629 region actually takes up some space in the cache, so we can
632 c
->buffer_beg
+ c
->beg_unchanged
,
633 c
->buffer_end
- c
->end_unchanged
,
637 /* Otherwise, there is a non-empty region in the cache which
638 corresponds to the modified region of the buffer. */
643 /* These positions are correct, relative to both the cache basis
644 and the buffer basis. */
646 c
->buffer_beg
+ c
->beg_unchanged
,
647 c
->buffer_end
- c
->end_unchanged
,
650 /* Now the cache contains only boundaries that are in the
651 unchanged head and tail; we've disposed of any boundaries
652 whose positions we can't be sure of given the information
655 If we put the cache gap between the unchanged head and the
656 unchanged tail, we can adjust all the boundary positions at
657 once, simply by setting buffer_beg and buffer_end.
659 The boundary which has jurisdiction over the modified region
660 should be left before the gap. */
662 find_cache_boundary (c
, (c
->buffer_beg
+ c
->beg_unchanged
)) + 1;
663 move_cache_gap (c
, modified_ix
, 0);
665 c
->buffer_beg
= BUF_BEG (buf
);
666 c
->buffer_end
= BUF_Z (buf
);
668 /* Now, we may have shrunk the buffer when we changed the basis,
669 and brought the boundaries we created for the start and end
670 of the modified region together, giving them the same
671 position. If that's the case, we should collapse them into
672 one boundary. Or we may even delete them both, if the values
673 before and after them are the same. */
674 if (modified_ix
< c
->cache_len
675 && (BOUNDARY_POS (c
, modified_ix
- 1)
676 == BOUNDARY_POS (c
, modified_ix
)))
678 int value_after
= BOUNDARY_VALUE (c
, modified_ix
);
680 /* Should we remove both of the boundaries? Yes, if the
681 latter boundary is now establishing the same value that
682 the former boundary's predecessor does. */
683 if (modified_ix
- 1 > 0
684 && value_after
== BOUNDARY_VALUE (c
, modified_ix
- 2))
685 delete_cache_boundaries (c
, modified_ix
- 1, modified_ix
+ 1);
688 /* We do need a boundary here; collapse the two
689 boundaries into one. */
690 SET_BOUNDARY_VALUE (c
, modified_ix
- 1, value_after
);
691 delete_cache_boundaries (c
, modified_ix
, modified_ix
+ 1);
696 /* Now the entire cache is valid. */
699 = c
->buffer_end
- c
->buffer_beg
;
703 /* Interface: Adding information to the cache. */
705 /* Assert that the region of BUF between START and END (absolute
706 buffer positions) is "known," for the purposes of CACHE (e.g. "has
707 no newlines", in the case of the line cache). */
709 know_region_cache (buf
, c
, start
, end
)
711 struct region_cache
*c
;
714 revalidate_region_cache (buf
, c
);
716 set_cache_region (c
, start
, end
, 1);
720 /* Interface: using the cache. */
722 /* Return true if the text immediately after POS in BUF is known, for
723 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
724 position after POS where the knownness changes. */
726 region_cache_forward (buf
, c
, pos
, next
)
728 struct region_cache
*c
;
732 revalidate_region_cache (buf
, c
);
735 int i
= find_cache_boundary (c
, pos
);
736 int i_value
= BOUNDARY_VALUE (c
, i
);
739 /* Beyond the end of the buffer is unknown, by definition. */
740 if (pos
>= BUF_Z (buf
))
742 if (next
) *next
= BUF_Z (buf
);
747 /* Scan forward from i to find the next differing position. */
748 for (j
= i
+ 1; j
< c
->cache_len
; j
++)
749 if (BOUNDARY_VALUE (c
, j
) != i_value
)
752 if (j
< c
->cache_len
)
753 *next
= BOUNDARY_POS (c
, j
);
762 /* Return true if the text immediately before POS in BUF is known, for
763 the purposes of CACHE. If NEXT is non-zero, set *NEXT to the nearest
764 position before POS where the knownness changes. */
765 int region_cache_backward (buf
, c
, pos
, next
)
767 struct region_cache
*c
;
771 revalidate_region_cache (buf
, c
);
773 /* Before the beginning of the buffer is unknown, by
775 if (pos
<= BUF_BEG (buf
))
777 if (next
) *next
= BUF_BEG (buf
);
782 int i
= find_cache_boundary (c
, pos
- 1);
783 int i_value
= BOUNDARY_VALUE (c
, i
);
788 /* Scan backward from i to find the next differing position. */
789 for (j
= i
- 1; j
>= 0; j
--)
790 if (BOUNDARY_VALUE (c
, j
) != i_value
)
794 *next
= BOUNDARY_POS (c
, j
+ 1);
796 *next
= BUF_BEG (buf
);
804 /* Debugging: pretty-print a cache to the standard error output. */
808 struct region_cache
*c
;
811 int beg_u
= c
->buffer_beg
+ c
->beg_unchanged
;
812 int end_u
= c
->buffer_end
- c
->end_unchanged
;
815 "basis: %d..%d modified: %d..%d\n",
816 c
->buffer_beg
, c
->buffer_end
,
819 for (i
= 0; i
< c
->cache_len
; i
++)
821 int pos
= BOUNDARY_POS (c
, i
);
823 putc (((pos
< beg_u
) ? 'v'
824 : (pos
== beg_u
) ? '-'
827 putc (((pos
> end_u
) ? '^'
828 : (pos
== end_u
) ? '-'
831 fprintf (stderr
, "%d : %d\n", pos
, BOUNDARY_VALUE (c
, i
));