'Boost'에 해당되는 글. 2건

  1. 2009/04/28 영어에서 시간과 시각 구분하기 (2)
  2. 2009/04/28 Boost.Intrusive - STL Container보다 빠른 Container (1)

 우리나라 말에서 
 시간(時間)과 시각(時刻)은 다른 개념이다.

 그럼 영어에서는 어떻게 구분을 해야할까?
 사실 내가 관심 있는 부분은 영어라는 것은 큰 의미는 없고, 
 코드에서 변수명/함수명을 작명시 어떻게 구분하는 게 좋을까에 대한 것이다.

 예전부터 고민했던 부분인데
 한 가지 괜찮은 대안을 Boost.Date_Time Library 문서에서 찾을 수 있었다.

-----

Domain Concepts

The date time domain is rich in terminology and problems. The following is a brief introduction to the concepts you will find reflected in the library.

The library supports 3 basic temporal types:

  • Time Point -- Specifier for a location in the time continuum.
  • Time Duration -- A length of time unattached to any point on the time continuum.
  • Time Interval -- A duration of time attached to a specific point in the time continuum. Also known as a time period.
-----

 요약 하면
 시각: Time Point 
 시간: Time Duration
 시간 구간: Time Interval or Time Period

 여기서 시간 구간이라는 의미는 특정 시각 두 개의 값으로 이루어진 시간을 의미한다.
 즉, 10분이라는 것은 시간(Time Duration) 이고,
 23시 00분 ~ 23시 10분은 시간 구간(Time Interval) 이다.

 약간 응용하면 <Time Interval>는 <Time Point>와 <Time Duration>의 조합으로 만들 수 있다.


 오버해서 더 나아가면.. (수학에서) 구간 이라는 것은 
 개구간(Unbounded Interval)과 폐구간(Bounded Interval)이 있다.

 [1, 10]은 Endpoints인 1과 10을 포함한다는 것 이고 (폐구간)
 (1, 10)은 Endpoints인 1과 10을 포함하지 않는 다는 것이다. (개구간)

 참고로, Boost.Date_Time 라이브러리에서 구간(Interval)은..  [Begin, End) 로 사용한다.





 
Posted by U∙Seung

 제목이 약간 자극적이다. (사실, 방식 자체가 다르기 때문에 동등한 조건에서 비교하는 것은 좀 그렇다.)

 Boost.Intrusive는 Boost Library의 Container중 하나로, Performance를 높이는 방법으로 사용할 수 있다.

 Boost.Intrusive에서 제공되는 Container의 종류는 아래과 같음.
 - Singly Linked List
 - Circularly Linked List (like std::list)
 - set/multiset/rbtree
 - avl_set/avl_multiset/avltree
 - splay_set/splay_multiset/splaytree
 - sg_set/sg_multiset/sgtree
 - unordered_set/unordered_multiset

 먼저 Intrusive Container와 Non-intrusive Container의 차이점에 대해서 알아야 한다.

 쉽게 설명하면.. 기존에 우리가 쓰는 Container들은 보통 Non-intrusive Container라고 생각하면 된다.
 Linked List를 한번이라도 구현해보거나 구조에 대해서 생각해 본 사람이라면 알겠지만.

 대략 Linked List는 아래와 같이 생겼다.

    1 class SomeValue

    2 {

    3     //.. some members ..///

    4     int asdf;

    5 };

    6 

    7 template <typename T>

    8 class List<T>

    9 {

   10     class list_node

   11     {

   12        list_node *next;

   13        list_node *previous;

   14        T value;

   15     };

   16 

   17     //....

   18 };

   19 

   20 List<SomeValue> list;


 여기서 14번째 줄처럼 Non-Intrusive Container는 Value값을 복사해서 가지게 되어 있다.
 
 하지만 Intrusive Container의 Linked List는 아래와 같이 구현한다.

    1 class SomeValue

    2 {

    3     // intrusive members.

    4     SomeValue *previous;

    5     SomeValue *next;

    6 

    7     //.. some members ..///

    8     int asdf;

    9 };

   10 

   11 template <typename T>

   12 class List<T>

   13 {

   14     //....

   15 };

   16 

   17 List<SomeValue> list;


 4번째, 5번째 줄처럼 기존에 Value 구조체에 Linked List에 필요한 구조체를 포함 시키는 게 핵심이다.


 따라서, 메모리 관리는 Container 외부에서 담당하게 되는 것이고
 Container 관리를 위한 메모리를 따로 할당 하지 않기 때문에 Performance 향상을 볼 수 있다.
 아래는 비교표.

Issue

Intrusive

Non-intrusive

Memory management

External

Internal through allocator

Insertion/Erasure time

Faster

Slower

Memory locality

Better

Worse

Can hold non-copyable and non-movable objects by value

Yes

No

Exception guarantees

Better

Worse

Computation of iterator from value

Constant

Non-constant

Insertion/erasure predictability

High

Low

Memory use

Minimal

More than minimal

Insert objects by value retaining polymorphic behavior

Yes

No (slicing)

User must modify the definition of the values to insert

Yes

No

Containers are copyable

No

Yes

Inserted object's lifetime managed by

User (more complex)

Container (less complex)

Container invariants can be broken without using the container

Easier

Harder (only with containers of pointers)

Thread-safety analysis

Harder

Easier



 내가 제일 마음에 드는 부분은..
 메모리 관리를 외부에서 할 수 있다는 점과 Memory Locality가 향상 된다는 점이 아닐까 싶다.

 이를 잘 활용하면 불필요하게 Heap을 사용하는 것을 줄일 수 있다.


 개발의 편의성 보다는 성능(!!)에 초점이 맞추어진 프로젝트가 있다면 써보길 추천..


 아.. 실제 Boost.Intrusive를 쓰는 코드는 아래와 같이 생겼다.

    1 

    2 struct SomeStruct

    3 {

    4     int i;

    5     char someBuffer[1024];

    6 

    7     SomeStruct()

    8     {

    9     }

   10 };

   11 

   12 struct SomeStructI

   13     : public boost::intrusive::slist_base_hook<>

   14     , public SomeStruct

   15 {

   16 };

   17 

   18 slist<SomeStructI, cache_last<true> > ptrContainer;

   19 

   20 BOOST_FOREACH(SomeStructI& someStruct, ptrContainer)

   21 {

   22     // ...

   23 }



 그다지 못 생긴 것도 아니다.




Posted by U∙Seung