pkoukk / tiktoken-go

go version of tiktoken
MIT License
649 stars 71 forks source link

Can a Tiktoken instance be shared by multiple go routines? #33

Closed WqyJh closed 1 year ago

WqyJh commented 1 year ago

It seems the GetEncoding is not cheap to call since it have to compile regex. Creating a Tiktoken instance before every token calculation is not efficient. Therefore, if Tiktoken instance can be shared by multiple go routines, then we only need to create it once, which is more efficient.

qhenkart commented 1 year ago

@WqyJh I would assume not. There are a lot of maps being passed around and modified under the hood

pkoukk commented 1 year ago

Yes. All Tiktoken methods are thread-safe to call, you can safely use them concurrently in Go routines.