Skip to content

Commit

Permalink
attempt 11: convert byte slice to string directly
Browse files Browse the repository at this point in the history
  • Loading branch information
shraddhaag committed Jan 30, 2024
1 parent 90f2fe1 commit c862e3a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
|7|In the city <> temperatures map, replaced the value for each key (city) to preprocessed min, max, count and sum of all temperatures instead of storing all recorded temperatures for the city.|1:39.81|-71.79|[e5213a8](https://github.com/shraddhaag/1brc/commit/e5213a836b17bec0a858474a11f07c902e724bba)|
|8|Use producer consumer pattern to read file in chunks and process the chunks in parallel.|1:43.82|+14.01|[067f2a4](https://github.com/shraddhaag/1brc/commit/067f2a44c0d6b3bb7cc073639364f733bce09e3e)|
|9|Reduce memory allocation by processing each read chunk into a map. Result channel now can collate the smaller processed chunk maps.|0:28.544|-75.286|[d4153ac](https://github.com/shraddhaag/1brc/commit/d4153ac7a841170a5ceee47d930e97738b5a19f6)|
|10|Avoid string concatenation overhead by not reading the decimal point when processing city temperature.|0:24.571|-3.973||
|10|Avoid string concatenation overhead by not reading the decimal point when processing city temperature.|0:24.571|-3.973||
|11|Convert byte slice to string directly instead of using a `strings.Builder`.|0:18.910|-5.761||
23 changes: 11 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,20 @@ func readFileLineByLineIntoAMap(filepath string) (map[string]cityTemperatureInfo
}

func processReadChunk(buf []byte, resultStream chan<- map[string]cityTemperatureInfo) {
var stringsBuilder strings.Builder
toSend := make(map[string]cityTemperatureInfo)
var city string
var start int
var city, tempString string

for _, char := range buf {
stringBuf := string(buf)
for index, char := range stringBuf {
switch char {
case ';':
city = stringsBuilder.String()
stringsBuilder.Reset()
city = stringBuf[start:index]
start = index + 1
case '\n':
if stringsBuilder.Len() != 0 && len(city) != 0 {
temp, _ := strconv.ParseInt(stringsBuilder.String(), 10, 64)
stringsBuilder.Reset()
if (index-start) > 1 && len(city) != 0 {
temp, _ := strconv.ParseInt(tempString+string(stringBuf[index-1]), 10, 64)
start = index + 1

if val, ok := toSend[city]; ok {
val.count++
Expand All @@ -214,11 +215,9 @@ func processReadChunk(buf []byte, resultStream chan<- map[string]cityTemperature
city = ""
}
case '.':
if len(city) == 0 {
stringsBuilder.WriteByte(char)
if len(city) != 0 {
tempString = stringBuf[start:index]
}
default:
stringsBuilder.WriteByte(char)
}
}
resultStream <- toSend
Expand Down
Binary file added profiles/cpu-remove-builder.prof
Binary file not shown.

0 comments on commit c862e3a

Please sign in to comment.