이번 주 목표
하루에 TIL 1개
오늘 목표
하루에 TIL 1개
오늘 한 것
하루에 TIL 1개
오늘 스케줄
하루에 TIL 1개
1. go의 Clean 메서드
상위 참조를 제거하여 보안 이슈를 방지한다.
Clean 메서드는 파일 경로를 간단하고 일관되게 유지하는 데 매우 유용합니다.
Go 언어의 filepath 패키지에서 제공하는 Clean 메서드는 파일 경로를 정규화하는 데 사용됩니다. 이 메서드는 슬래시로 구분된 경로를 해석하고, ., .., 중복된 슬래시를 제거하여 간단한 경로로 변환합니다.
Clean 메서드를 사용하면, 입력된 경로 문자열을 다음과 같이 처리할 수 있습니다:
- 슬래시로 시작하지 않으면 현재 디렉터리로 간주합니다.
- 여러 개의 슬래시를 하나의 슬래시로 축소합니다.
- 경로에 포함된 . 요소를 제거합니다.
- .. 요소와 그 이전의 디렉토리를 제거합니다.
package util
import "path/filepath"
// 주어진 두 경로에서 상위 참조를 제거하고 병합한 뒤 이를 반환합니다.
func JoinPath(p1, p2 string) string {
cp1 := filepath.Clean(p1)
cp2 := filepath.Clean(p2)
return filepath.Join(cp1, cp2)
}
// Clean returns the shortest path name equivalent to path
// by purely lexical processing. It applies the following rules
// iteratively until no further processing can be done:
//
// 1. Replace multiple [Separator] elements with a single one.
// 2. Eliminate each . path name element (the current directory).
// 3. Eliminate each inner .. path name element (the parent directory)
// along with the non-.. element that precedes it.
// 4. Eliminate .. elements that begin a rooted path:
// that is, replace "/.." by "/" at the beginning of a path,
// assuming Separator is '/'.
//
// The returned path ends in a slash only if it represents a root directory,
// such as "/" on Unix or `C:\` on Windows.
//
// Finally, any occurrences of slash are replaced by Separator.
//
// If the result of this process is an empty string, Clean
// returns the string ".".
//
// On Windows, Clean does not modify the volume name other than to replace
// occurrences of "/" with `\`.
// For example, Clean("//host/share/../x") returns `\\host\share\x`.
//
// See also Rob Pike, “Lexical File Names in Plan 9 or
// Getting Dot-Dot Right,”
// https://9p.io/sys/doc/lexnames.html
func Clean(path string) string {
originalPath := path
volLen := volumeNameLen(path)
path = path[volLen:]
if path == "" {
if volLen > 1 && os.IsPathSeparator(originalPath[0]) && os.IsPathSeparator(originalPath[1]) {
// should be UNC
return FromSlash(originalPath)
}
return originalPath + "."
}
rooted := os.IsPathSeparator(path[0])
// Invariants:
// reading from path; r is index of next byte to process.
// writing to buf; w is index of next byte to write.
// dotdot is index in buf where .. must stop, either because
// it is the leading slash or it is a leading ../../.. prefix.
n := len(path)
out := lazybuf{path: path, volAndPath: originalPath, volLen: volLen}
r, dotdot := 0, 0
if rooted {
out.append(Separator)
r, dotdot = 1, 1
}
for r < n {
switch {
case os.IsPathSeparator(path[r]):
// empty path element
r++
case path[r] == '.' && (r+1 == n || os.IsPathSeparator(path[r+1])):
// . element
r++
case path[r] == '.' && path[r+1] == '.' && (r+2 == n || os.IsPathSeparator(path[r+2])):
// .. element: remove to last separator
r += 2
switch {
case out.w > dotdot:
// can backtrack
out.w--
for out.w > dotdot && !os.IsPathSeparator(out.index(out.w)) {
out.w--
}
case !rooted:
// cannot backtrack, but not rooted, so append .. element.
if out.w > 0 {
out.append(Separator)
}
out.append('.')
out.append('.')
dotdot = out.w
}
default:
// real path element.
// add slash if needed
if rooted && out.w != 1 || !rooted && out.w != 0 {
out.append(Separator)
}
// copy element
for ; r < n && !os.IsPathSeparator(path[r]); r++ {
out.append(path[r])
}
}
}
// Turn empty string into "."
if out.w == 0 {
out.append('.')
}
postClean(&out) // avoid creating absolute paths on Windows
return FromSlash(out.string())
}
2. 캐시의 지역성
캐시 메모리 : 속도가 빠른 장치와 느린 장치간의 속도차에 따른 병목 현상을 줄이기 위한 범용 메모리
매핑 : 캐시 기억장치와 주기억장치 사이에서 정보를 옮기는 것
캐시 히트 : 캐시 메모리에 찾는 데이터가 존재하였을 때
캐시 미스 : 캐시 메모리에 찾는 데이터가 존재하지 않았을 때
히트 레이트 : 적중률
미스 레이트 : 없을 확률
캐싱라인(Caching Line) :캐시에 데이터를 저장할 때 특정 자료구조를 사용하여 '묶음'으로 저장하는 것
시간적 지역성(Temporal Locality)
시간적 지역성은 최근에 참조된 주소의 내용은 곧 다음에 다시 참조되는 특성이다.
메모리 상의 같은 주소에 여러 차례 읽기 쓰기를 수행할 경우, 상대적으로 작은 크기의 캐시를 사용해도 효율성을 꾀할 수 있다.
공간적 지역성(Spatial Locality)
공간적 지역성은 기억장치 내에 서로 인접하여 저장되어 있는 데이터들이 연속적으로 액세스 될 가능성이 높아지는 특성이다.
이때 메모리 주소를 오름차순이나 내림차순으로 접근한다면, 캐시에 이미 저장된 같은 블록의 데이터를 접근하게 되므로 캐시의 효율성이 크게 향상된다.
정리
[ 캐시의 지역성 ]
시간적 지역성(Temporal Locality)
시간적 지역성은 최근에 참조된 주소의 내용은 곧 다음에 다시 참조되는 특성이다.
메모리 상의 같은 주소에 여러 차례 읽기 쓰기를 수행할 경우, 상대적으로 작은 크기의 캐시를 사용해도 효율성을 꾀할 수 있다.
공간적 지역성(Spatial Locality)
공간적 지역성은 기억장치 내에 서로 인접하여 저장되어 있는 데이터들이 연속적으로 액세스 될 가능성이 높아지는 특성이다.
이때 메모리 주소를 오름차순이나 내림차순으로 접근한다면, 캐시에 이미 저장된 같은 블록의 데이터를 접근하게 되므로 캐시의 효율성이 크게 향상된다.
KPT
Keep
읽으려고 하는 것
Problem
마음이 급한 것
Try
시간이 걸리더라도 천천히 읽기
소감
펍 카페 프로젝트에 재합류 하였다.
Go를 오랜만에 보니 감회가 새롭다.
열심히 하자.
'verdantjuly > Today I Learned' 카테고리의 다른 글
TIL 20240619 (0) | 2024.06.19 |
---|---|
TIL 20240610 (2) | 2024.06.10 |
TIL 20240604 (0) | 2024.06.04 |
TIL 20240603 (0) | 2024.06.03 |
TIL 20240512 (0) | 2024.05.13 |