Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: third_party/WebKit/Source/core/editing/VisibleUnits.cpp

Issue 1687813002: Optimize Boundary Finding with Progressive Text Accumulation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@text_accumulator_push
Patch Set: Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/editing/iterators/TextBufferBase.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed. 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv ed.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 if (static_cast<unsigned>(i) < characters.size()) 683 if (static_cast<unsigned>(i) < characters.size())
684 break; 684 break;
685 forwardsIterator.advance(); 685 forwardsIterator.advance();
686 } 686 }
687 } 687 }
688 688
689 BackwardsTextBuffer string; 689 BackwardsTextBuffer string;
690 string.pushRange(suffixString.data(), suffixString.size()); 690 string.pushRange(suffixString.data(), suffixString.size());
691 691
692 SimplifiedBackwardsTextIteratorAlgorithm<Strategy> it(start, end); 692 SimplifiedBackwardsTextIteratorAlgorithm<Strategy> it(start, end);
693 int itRemain = 0;
yosin_UTC9 2016/02/16 07:55:13 |itRemain| is confusing name, e.g. it is an iterat
693 unsigned next = 0; 694 unsigned next = 0;
694 bool needMoreContext = false; 695 bool needMoreContext = false;
695 while (!it.atEnd()) { 696 while (!it.atEnd()) {
696 bool inTextSecurityMode = it.isInTextSecurityMode(); 697 bool inTextSecurityMode = it.isInTextSecurityMode();
697 // iterate to get chunks until the searchFunction returns a non-zero 698 // iterate to get chunks until the searchFunction returns a non-zero
698 // value. 699 // value.
699 if (!inTextSecurityMode) { 700 if (!inTextSecurityMode) {
700 it.copyTextTo(&string); 701 int runOffset = 0;
702 do {
703 runOffset += it.copyTextTo(&string, runOffset, string.capacity() );
704 // TODO(xiaochengh): The following line takes O(string.size()) t ime,
705 // which makes quadratic overall running time in the worst case.
706 // Should improve it in some way.
707 next = searchFunction(string.data(), string.size(), string.size( ) - suffixLength, MayHaveMoreContext, needMoreContext);
708 } while (!next && runOffset < it.length());
709 if (next) {
710 itRemain = it.length() - runOffset;
711 break;
712 }
701 } else { 713 } else {
702 // Treat bullets used in the text security mode as regular 714 // Treat bullets used in the text security mode as regular
703 // characters when looking for boundaries 715 // characters when looking for boundaries
704 string.pushCharacters('x', it.length()); 716 string.pushCharacters('x', it.length());
717 next = 0;
705 } 718 }
706 // TODO(xiaochengh): The following line takes O(string.size()) time,
707 // which makes the while loop take quadratic time in the worst case.
708 // Should improve it in some way.
709 next = searchFunction(string.data(), string.size(), string.size() - suff ixLength, MayHaveMoreContext, needMoreContext);
710 if (next)
711 break;
712 it.advance(); 719 it.advance();
713 } 720 }
714 if (needMoreContext) { 721 if (needMoreContext) {
715 // The last search returned the beginning of the buffer and asked for 722 // The last search returned the beginning of the buffer and asked for
716 // more context, but there is no earlier text. Force a search with 723 // more context, but there is no earlier text. Force a search with
717 // what's available. 724 // what's available.
718 // TODO(xiaochengh): Do we have to search the whole string? 725 // TODO(xiaochengh): Do we have to search the whole string?
719 next = searchFunction(string.data(), string.size(), string.size() - suff ixLength, DontHaveMoreContext, needMoreContext); 726 next = searchFunction(string.data(), string.size(), string.size() - suff ixLength, DontHaveMoreContext, needMoreContext);
720 ASSERT(!needMoreContext); 727 ASSERT(!needMoreContext);
721 } 728 }
722 729
723 if (!next) 730 if (!next)
724 return createVisiblePosition(it.atEnd() ? it.startPosition() : pos); 731 return createVisiblePosition(it.atEnd() ? it.startPosition() : pos);
725 732
726 Node* node = it.startContainer(); 733 Node* node = it.startContainer();
727 if (node->isTextNode() && static_cast<int>(next) <= node->maxCharacterOffset ()) { 734 int boundaryOffset = itRemain + next;
735 if (node->isTextNode() && boundaryOffset <= node->maxCharacterOffset()) {
728 // The next variable contains a usable index into a text node 736 // The next variable contains a usable index into a text node
729 return createVisiblePosition(PositionTemplate<Strategy>(node, next)); 737 return createVisiblePosition(PositionTemplate<Strategy>(node, boundaryOf fset));
730 } 738 }
731 739
732 // Use the character iterator to translate the next value into a DOM 740 // Use the character iterator to translate the next value into a DOM
733 // position. 741 // position.
734 BackwardsCharacterIteratorAlgorithm<Strategy> charIt(start, end); 742 BackwardsCharacterIteratorAlgorithm<Strategy> charIt(start, end);
735 charIt.advance(string.size() - suffixLength - next); 743 charIt.advance(string.size() - suffixLength - next);
736 // TODO(yosin) charIt can get out of shadow host. 744 // TODO(yosin) charIt can get out of shadow host.
737 return createVisiblePosition(charIt.endPosition()); 745 return createVisiblePosition(charIt.endPosition());
738 } 746 }
739 747
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
776 const unsigned invalidOffset = static_cast<unsigned>(-1); 784 const unsigned invalidOffset = static_cast<unsigned>(-1);
777 unsigned next = invalidOffset; 785 unsigned next = invalidOffset;
778 unsigned offset = prefixLength; 786 unsigned offset = prefixLength;
779 bool needMoreContext = false; 787 bool needMoreContext = false;
780 while (!it.atEnd()) { 788 while (!it.atEnd()) {
781 // Keep asking the iterator for chunks until the search function 789 // Keep asking the iterator for chunks until the search function
782 // returns an end value not equal to the length of the string passed to 790 // returns an end value not equal to the length of the string passed to
783 // it. 791 // it.
784 bool inTextSecurityMode = it.isInTextSecurityMode(); 792 bool inTextSecurityMode = it.isInTextSecurityMode();
785 if (!inTextSecurityMode) { 793 if (!inTextSecurityMode) {
786 it.copyTextTo(&string); 794 int runOffset = 0;
795 do {
796 runOffset += it.copyTextTo(&string, runOffset, string.capacity() );
797 next = searchFunction(string.data(), string.size(), offset, MayH aveMoreContext, needMoreContext);
798 if (!needMoreContext) {
799 // When the search does not need more context, skip all exam ined
800 // characters except the last one, in case it is a boundary.
801 offset = string.size();
802 U16_BACK_1(string.data(), 0, offset);
803 }
804 } while (next == string.size() && runOffset < it.length());
805 if (next != string.size())
806 break;
787 } else { 807 } else {
788 // Treat bullets used in the text security mode as regular 808 // Treat bullets used in the text security mode as regular
789 // characters when looking for boundaries 809 // characters when looking for boundaries
790 string.pushCharacters('x', it.length()); 810 string.pushCharacters('x', it.length());
811 next = string.size();
791 } 812 }
792 next = searchFunction(string.data(), string.size(), offset, MayHaveMoreC ontext, needMoreContext);
793 if (next != string.size())
794 break;
795 it.advance(); 813 it.advance();
796 if (!needMoreContext) {
797 // When the search does not need more context, skip all examined
798 // characters except the last one, in case it is a boundary.
799 offset = string.size();
800 U16_BACK_1(string.data(), 0, offset);
801 }
802 } 814 }
803 if (needMoreContext) { 815 if (needMoreContext) {
804 // The last search returned the end of the buffer and asked for more 816 // The last search returned the end of the buffer and asked for more
805 // context, but there is no further text. Force a search with what's 817 // context, but there is no further text. Force a search with what's
806 // available. 818 // available.
807 // TODO(xiaochengh): Do we still have to search the whole string? 819 // TODO(xiaochengh): Do we still have to search the whole string?
808 next = searchFunction(string.data(), string.size(), prefixLength, DontHa veMoreContext, needMoreContext); 820 next = searchFunction(string.data(), string.size(), prefixLength, DontHa veMoreContext, needMoreContext);
809 ASSERT(!needMoreContext); 821 ASSERT(!needMoreContext);
810 } 822 }
811 823
(...skipping 2540 matching lines...) Expand 10 before | Expand all | Expand 10 after
3352 { 3364 {
3353 return previousPositionOfAlgorithm<EditingStrategy>(visiblePosition, rule); 3365 return previousPositionOfAlgorithm<EditingStrategy>(visiblePosition, rule);
3354 } 3366 }
3355 3367
3356 VisiblePositionInFlatTree previousPositionOf(const VisiblePositionInFlatTree& vi siblePosition, EditingBoundaryCrossingRule rule) 3368 VisiblePositionInFlatTree previousPositionOf(const VisiblePositionInFlatTree& vi siblePosition, EditingBoundaryCrossingRule rule)
3357 { 3369 {
3358 return previousPositionOfAlgorithm<EditingInFlatTreeStrategy>(visiblePositio n, rule); 3370 return previousPositionOfAlgorithm<EditingInFlatTreeStrategy>(visiblePositio n, rule);
3359 } 3371 }
3360 3372
3361 } // namespace blink 3373 } // namespace blink
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/editing/iterators/TextBufferBase.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698