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

Side by Side Diff: Source/wtf/Vector.h

Issue 23775006: Remove redundant branches and add a little LIKELY / UNLIKELY. (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 7 years, 3 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
3 * 3 *
4 * This library is free software; you can redistribute it and/or 4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public 5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either 6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version. 7 * version 2 of the License, or (at your option) any later version.
8 * 8 *
9 * This library is distributed in the hope that it will be useful, 9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 if (newCapacity > inlineCapacity) 397 if (newCapacity > inlineCapacity)
398 Base::allocateBuffer(newCapacity); 398 Base::allocateBuffer(newCapacity);
399 else { 399 else {
400 m_buffer = inlineBuffer(); 400 m_buffer = inlineBuffer();
401 m_capacity = inlineCapacity; 401 m_capacity = inlineCapacity;
402 } 402 }
403 } 403 }
404 404
405 void deallocateBuffer(T* bufferToDeallocate) 405 void deallocateBuffer(T* bufferToDeallocate)
406 { 406 {
407 if (bufferToDeallocate == inlineBuffer()) 407 if (LIKELY(bufferToDeallocate == inlineBuffer()))
408 return; 408 return;
409 Base::deallocateBuffer(bufferToDeallocate); 409 Base::deallocateBuffer(bufferToDeallocate);
410 } 410 }
411 411
412 bool shouldReallocateBuffer(size_t newCapacity) const 412 bool shouldReallocateBuffer(size_t newCapacity) const
413 { 413 {
414 // We cannot reallocate the inline buffer. 414 // We cannot reallocate the inline buffer.
415 return Base::shouldReallocateBuffer(newCapacity) && std::min(static_ cast<size_t>(m_capacity), newCapacity) > inlineCapacity; 415 return Base::shouldReallocateBuffer(newCapacity) && std::min(static_ cast<size_t>(m_capacity), newCapacity) > inlineCapacity;
416 } 416 }
417 417
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
484 484
485 Vector() 485 Vector()
486 { 486 {
487 m_size = 0; 487 m_size = 0;
488 } 488 }
489 489
490 explicit Vector(size_t size) 490 explicit Vector(size_t size)
491 : Base(size) 491 : Base(size)
492 { 492 {
493 m_size = size; 493 m_size = size;
494 if (begin()) 494 TypeOperations::initialize(begin(), end());
495 TypeOperations::initialize(begin(), end());
496 } 495 }
497 496
498 ~Vector() 497 ~Vector()
499 { 498 {
500 if (m_size) 499 if (m_size)
501 shrink(0); 500 shrink(0);
502 } 501 }
503 502
504 Vector(const Vector&); 503 Vector(const Vector&);
505 template<size_t otherCapacity> 504 template<size_t otherCapacity>
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 void removeLast() 583 void removeLast()
585 { 584 {
586 ASSERT(!isEmpty()); 585 ASSERT(!isEmpty());
587 shrink(size() - 1); 586 shrink(size() - 1);
588 } 587 }
589 588
590 Vector(size_t size, const T& val) 589 Vector(size_t size, const T& val)
591 : Base(size) 590 : Base(size)
592 { 591 {
593 m_size = size; 592 m_size = size;
594 if (begin()) 593 TypeOperations::uninitializedFill(begin(), end(), val);
595 TypeOperations::uninitializedFill(begin(), end(), val);
596 } 594 }
597 595
598 void fill(const T&, size_t); 596 void fill(const T&, size_t);
599 void fill(const T& val) { fill(val, size()); } 597 void fill(const T& val) { fill(val, size()); }
600 598
601 template<typename Iterator> void appendRange(Iterator start, Iterator en d); 599 template<typename Iterator> void appendRange(Iterator start, Iterator en d);
602 600
603 void swap(Vector<T, inlineCapacity>& other) 601 void swap(Vector<T, inlineCapacity>& other)
604 { 602 {
605 std::swap(m_size, other.m_size); 603 std::swap(m_size, other.m_size);
(...skipping 17 matching lines...) Expand all
623 using Base::shouldReallocateBuffer; 621 using Base::shouldReallocateBuffer;
624 using Base::reallocateBuffer; 622 using Base::reallocateBuffer;
625 using Base::restoreInlineBufferIfNeeded; 623 using Base::restoreInlineBufferIfNeeded;
626 }; 624 };
627 625
628 template<typename T, size_t inlineCapacity> 626 template<typename T, size_t inlineCapacity>
629 Vector<T, inlineCapacity>::Vector(const Vector& other) 627 Vector<T, inlineCapacity>::Vector(const Vector& other)
630 : Base(other.capacity()) 628 : Base(other.capacity())
631 { 629 {
632 m_size = other.size(); 630 m_size = other.size();
633 if (begin()) 631 TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
634 TypeOperations::uninitializedCopy(other.begin(), other.end(), begin( ));
635 } 632 }
636 633
637 template<typename T, size_t inlineCapacity> 634 template<typename T, size_t inlineCapacity>
638 template<size_t otherCapacity> 635 template<size_t otherCapacity>
639 Vector<T, inlineCapacity>::Vector(const Vector<T, otherCapacity>& other) 636 Vector<T, inlineCapacity>::Vector(const Vector<T, otherCapacity>& other)
640 : Base(other.capacity()) 637 : Base(other.capacity())
641 { 638 {
642 m_size = other.size(); 639 m_size = other.size();
643 if (begin()) 640 TypeOperations::uninitializedCopy(other.begin(), other.end(), begin());
644 TypeOperations::uninitializedCopy(other.begin(), other.end(), begin( ));
645 } 641 }
646 642
647 template<typename T, size_t inlineCapacity> 643 template<typename T, size_t inlineCapacity>
648 Vector<T, inlineCapacity>& Vector<T, inlineCapacity>::operator=(const Vector <T, inlineCapacity>& other) 644 Vector<T, inlineCapacity>& Vector<T, inlineCapacity>::operator=(const Vector <T, inlineCapacity>& other)
649 { 645 {
650 if (&other == this) 646 if (UNLIKELY(&other == this))
651 return *this; 647 return *this;
652 648
653 if (size() > other.size()) 649 if (size() > other.size())
654 shrink(other.size()); 650 shrink(other.size());
655 else if (other.size() > capacity()) { 651 else if (other.size() > capacity()) {
656 clear(); 652 clear();
657 reserveCapacity(other.size()); 653 reserveCapacity(other.size());
658 ASSERT(begin()); 654 ASSERT(begin());
659 } 655 }
660 656
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
805 } 801 }
806 802
807 template<typename T, size_t inlineCapacity> 803 template<typename T, size_t inlineCapacity>
808 inline void Vector<T, inlineCapacity>::resize(size_t size) 804 inline void Vector<T, inlineCapacity>::resize(size_t size)
809 { 805 {
810 if (size <= m_size) 806 if (size <= m_size)
811 TypeOperations::destruct(begin() + size, end()); 807 TypeOperations::destruct(begin() + size, end());
812 else { 808 else {
813 if (size > capacity()) 809 if (size > capacity())
814 expandCapacity(size); 810 expandCapacity(size);
815 if (begin()) 811 TypeOperations::initialize(end(), begin() + size);
816 TypeOperations::initialize(end(), begin() + size);
817 } 812 }
818 813
819 m_size = size; 814 m_size = size;
820 } 815 }
821 816
822 template<typename T, size_t inlineCapacity> 817 template<typename T, size_t inlineCapacity>
823 void Vector<T, inlineCapacity>::shrink(size_t size) 818 void Vector<T, inlineCapacity>::shrink(size_t size)
824 { 819 {
825 ASSERT(size <= m_size); 820 ASSERT(size <= m_size);
826 TypeOperations::destruct(begin() + size, end()); 821 TypeOperations::destruct(begin() + size, end());
827 m_size = size; 822 m_size = size;
828 } 823 }
829 824
830 template<typename T, size_t inlineCapacity> 825 template<typename T, size_t inlineCapacity>
831 void Vector<T, inlineCapacity>::grow(size_t size) 826 void Vector<T, inlineCapacity>::grow(size_t size)
832 { 827 {
833 ASSERT(size >= m_size); 828 ASSERT(size >= m_size);
834 if (size > capacity()) 829 if (size > capacity())
835 expandCapacity(size); 830 expandCapacity(size);
836 if (begin()) 831 TypeOperations::initialize(end(), begin() + size);
837 TypeOperations::initialize(end(), begin() + size);
838 m_size = size; 832 m_size = size;
839 } 833 }
840 834
841 template<typename T, size_t inlineCapacity> 835 template<typename T, size_t inlineCapacity>
842 void Vector<T, inlineCapacity>::reserveCapacity(size_t newCapacity) 836 void Vector<T, inlineCapacity>::reserveCapacity(size_t newCapacity)
843 { 837 {
844 if (newCapacity <= capacity()) 838 if (newCapacity <= capacity())
845 return; 839 return;
846 T* oldBuffer = begin(); 840 T* oldBuffer = begin();
847 T* oldEnd = end(); 841 T* oldEnd = end();
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after
1070 inline bool operator!=(const Vector<T, inlineCapacity>& a, const Vector<T, i nlineCapacity>& b) 1064 inline bool operator!=(const Vector<T, inlineCapacity>& a, const Vector<T, i nlineCapacity>& b)
1071 { 1065 {
1072 return !(a == b); 1066 return !(a == b);
1073 } 1067 }
1074 1068
1075 } // namespace WTF 1069 } // namespace WTF
1076 1070
1077 using WTF::Vector; 1071 using WTF::Vector;
1078 1072
1079 #endif // WTF_Vector_h 1073 #endif // WTF_Vector_h
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698