Skip to content

Commit

Permalink
syntax: fast-path for did:plc parsing
Browse files Browse the repository at this point in the history
Goal is to avoid using full regex parsing for this common case.
profiling was showing that ParseDID regex has been burning a lot of CPU
in prod.
  • Loading branch information
bnewbold committed Nov 29, 2024
1 parent 2b814e8 commit 1e2f969
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions atproto/syntax/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,30 @@ import (
type DID string

var didRegex = regexp.MustCompile(`^did:[a-z]+:[a-zA-Z0-9._:%-]*[a-zA-Z0-9._-]$`)
var plcChars = ""

func isASCIIAlphaNum(c rune) bool {
if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') {
return true
}
return false
}

func ParseDID(raw string) (DID, error) {
// fast-path for did:plc, avoiding regex
if len(raw) == 32 && strings.HasPrefix(raw, "did:plc:") {
// NOTE: this doesn't really check base32, just broader alphanumberic. might pass invalid PLC DIDs, but they still have overall valid DID syntax
isPlc := true
for _, c := range raw[8:32] {
if !isASCIIAlphaNum(c) {
isPlc = false
break
}
}
if isPlc {
return DID(raw), nil
}
}
if raw == "" {
return "", errors.New("expected DID, got empty string")
}
Expand Down

0 comments on commit 1e2f969

Please sign in to comment.