분석과탐구
-
CRTP는 어떻게 가능할까?분석과탐구 2021. 10. 13. 03:36
링크 : CRTP(Curiously recurring template pattern) Curiously recurring template pattern - Wikipedia The curiously recurring template pattern (CRTP) is an idiom in C++ in which a class X derives from a class template instantiation using X itself as a template argument.[1] More generally it is known as F-bound polymorphism, and it is a form of F-bounded qu en.wikipedia.org CRTP라는 코드 구성이 어떻게 가능한지 살펴볼 ..
-
캐시 친화적인 코드를 찾으려 했으나 실패...분석과탐구 2021. 10. 12. 11:39
아래처럼 다차원 반복문에서 방문순서를 정하는 것 외의 다른 예제를 찾아보는데 쉽지가 않다. //i, j 순으로 순회하느냐 j , i순으로 순회하느냐 for(int i = 0; i < size; i++) { for(int j= 0; j < size; j++) { arr[i][j] 혹은 arr[j][i] } } 컴퓨터에 캐시 라인 크기가 64바이트이길래 쓰기, 읽기를 섞으면서 구체적인 성능 차이를 도출하려고 하는데 영 결과가 시원치 않다... 그나마 찾다 보니, 상속이냐 vs 포함이냐 할 때, 상속을 하면 부모 클래스의 데이터가 하위 클래스의 데이터랑 같은 선에서 쭉 나열되고 이러한 속성을 이용해서 성능 향상을 꾀하는 게 정말 있는 거구나 정도는 찾아서 좋았지만… 이것도 또 c++이 아닌 다른 언어로 가면 ..
-
OverlappedIO에서 소켓 버퍼 사이즈를 바꾸는 것은 대부분 성능에 영향이 없다.분석과탐구 2021. 6. 5. 23:59
윈도우즈 소켓 함수인 send()는 데이터의 전송을 보장하지 않는다. 정확히는 유저 버퍼에 있는 데이터를 소켓의 송신 버퍼에 복사를 해줄 뿐이다. 데이터 전송은 운영체제에서 알아서 해준다. 그럴 수밖에 없는 것이, TCP 같은 경우는 congestion control, flow control 같은 전송량 제어 프로토콜을 따라야 하기 때문이다. OverlappedIO을 이용하여 콜백 함수(OverlappedIO Completion Routine)나 IOCP 큐로 통지를 받은 것은 유저 버퍼의 데이터가 소켓의 송신 버퍼에 복사된 것을 의미한다. 이 경우엔 All or Nothing으로 요청한 데이터 사이즈만큼 복사가 완료될 때까지 성공 이벤트가 뜨지 않는다. 그런데, 예외적인 동작이 하나 있다. 그것은 바로..
-
소켓의 수신 버퍼 크기를 0으로 할 때 주의할 점.분석과탐구 2021. 5. 30. 20:56
IOCP 및 overlapped IO를 다루는 글에서 소켓의 수신 버퍼 사이즈를 0으로 만들어서, zero copy의 이점을 살리라고 되어 있다. setsockopt 함수와 SO_RCVBUF를 이용하여 소켓의 수신 버퍼 사이즈를 0으로 만들 수 있다. zero copy는 도착한 패킷이 커널의 소켓 수신 버퍼를 거치지 않고, 유저가 제공한 버퍼에 바로 복사되는 것을 의미한다. 그러나, SO_RCVBUF의 사이즈를 0으로 만든다고 zero copy가 되는 것은 아니며, 수신 버퍼 사이즈를 0으로 만드는 것은 주의해야할 점이 있다. zero copy와 SO_RCVBUF의 관계는 이 링크에 있다. https://crmerry.tistory.com/29?category=1031711 아래 인용글에 커널 소켓의 수..
-
switch-case optimization분석과탐구 2021. 5. 23. 18:17
switch-case 어셈블리어 컴파일러가 switch-case문을 최적화시킨다는 것은 잘 알려진 사실이다. 최적화 옵션을 끄더라도, 최적화에 대한 힌트를 잘 주면 디버그 모드에서도 switch-case문에 점프 테이블을 만들어서 비교 횟수를 줄여준다. 비교를 줄임으로써, CPU의 파이프라인 기법에 좋은 영향을 줄 수 있다. 디스어셈블리를 쉽게 확인할 수 있는 비주얼 스튜디오를 이용하여 msvc 컴파일러를 이용하여 직접 확인해 보자. 확인을 위해 다음과 같은 코드를 사용하였다. switch (val) { case 2: val += 2; printf("val : %d\n", val); break; case 5: val += 5; printf("val : %d\n", val); break; case 7: v..
-
멀티 스레드 프로그래밍의 문제 : 데이터 오염분석과탐구 2021. 5. 20. 14:23
문제상황 서버로부터 설정(폴리시)이나 플래그 값을 받아오는 스레드(폴리시 스레드)가 있고, 이 값을 바탕으로 작업을 하는 스레드(워킹 스레드)가 있다. 폴리시 스레드에서 기존의 폴리시를 업데이트 할 것이고, 워킹 스레드에서 윗 줄의 코드에서는 이전 폴리시를 사용했는데 아랫 줄의 코드에서는 최신의 폴리시를 써서 생기는 데이터 오염을 해결하고 싶어한다. 작성자는 그 해결 방안으로 글로벌 변수로 되어있는 폴리시를 스레드 내에서 지역변수로 복사하고, 이 지역변수를 폴리시로 인식하고 작업을 풀어갔다. 해결방법 질문자의 해결법도 괜찮은 것 같은데, 하나 문제가 있다. 글로벌 변수에서 지역변수로 복사할 때, 명시적인 락이 없다면, 복사하는 과정에서도 데이터가 오염될 수 있다. 복사라는 게 결국 4바이트나 8바이트 혹..