From 27121f904a1f358b01e4660ab098ac087a5a1933 Mon Sep 17 00:00:00 2001 From: Simon Let Date: Tue, 10 Dec 2019 20:33:36 +0100 Subject: [PATCH] use slim record for /recall instead of regular one we went from about 0.15 ms of unmarshalling time to about 0.05 ms that takes simple recalls from 0.2 ms to 0.1 ms this all seems like useless optimizing but this improvement made recalling usable in zsh and probably tolerable in bash --- cmd/collect/main.go | 135 +++++++++++++++++++++-------------------- cmd/daemon/recall.go | 2 +- pkg/collect/collect.go | 2 +- pkg/records/records.go | 15 +++++ 4 files changed, 87 insertions(+), 67 deletions(-) diff --git a/cmd/collect/main.go b/cmd/collect/main.go index 5699ac6..49ccbde 100644 --- a/cmd/collect/main.go +++ b/cmd/collect/main.go @@ -169,74 +169,79 @@ func main() { // *osReleasePrettyName = "Linux" // } - rec := records.Record{ - // posix - Cols: *cols, - Lines: *lines, - // core - BaseRecord: records.BaseRecord{ - RecallHistno: *recallHistno, - - CmdLine: *cmdLine, - ExitCode: *exitCode, - Shell: *shell, - Uname: *uname, - SessionID: *sessionID, - - // posix - Home: *home, - Lang: *lang, - LcAll: *lcAll, - Login: *login, - // Path: *path, - Pwd: *pwd, - ShellEnv: *shellEnv, - Term: *term, - - // non-posix - RealPwd: realPwd, - Pid: *pid, - SessionPID: *sessionPid, - Host: *host, - Hosttype: *hosttype, - Ostype: *ostype, - Machtype: *machtype, - Shlvl: *shlvl, - - // before after - TimezoneBefore: *timezoneBefore, - - RealtimeBefore: realtimeBefore, - RealtimeBeforeLocal: realtimeBeforeLocal, - - RealtimeSinceSessionStart: realtimeSinceSessionStart, - RealtimeSinceBoot: realtimeSinceBoot, - - GitDir: gitDir, - GitRealDir: gitRealDir, - GitOriginRemote: *gitRemote, - MachineID: collect.ReadFileContent(machineIDPath), - - OsReleaseID: *osReleaseID, - OsReleaseVersionID: *osReleaseVersionID, - OsReleaseIDLike: *osReleaseIDLike, - OsReleaseName: *osReleaseName, - OsReleasePrettyName: *osReleasePrettyName, - - PartOne: true, - - ReshUUID: collect.ReadFileContent(reshUUIDPath), - ReshVersion: Version, - ReshRevision: Revision, - - RecallActionsRaw: *recallActions, - RecallPrefix: *recallPrefix, - RecallStrategy: *recallStrategy, - }, - } if *recall { + rec := records.SlimRecord{ + SessionID: *sessionID, + RecallHistno: *recallHistno, + RecallPrefix: *recallPrefix, + } fmt.Print(collect.SendRecallRequest(rec, strconv.Itoa(config.Port))) } else { + rec := records.Record{ + // posix + Cols: *cols, + Lines: *lines, + // core + BaseRecord: records.BaseRecord{ + RecallHistno: *recallHistno, + + CmdLine: *cmdLine, + ExitCode: *exitCode, + Shell: *shell, + Uname: *uname, + SessionID: *sessionID, + + // posix + Home: *home, + Lang: *lang, + LcAll: *lcAll, + Login: *login, + // Path: *path, + Pwd: *pwd, + ShellEnv: *shellEnv, + Term: *term, + + // non-posix + RealPwd: realPwd, + Pid: *pid, + SessionPID: *sessionPid, + Host: *host, + Hosttype: *hosttype, + Ostype: *ostype, + Machtype: *machtype, + Shlvl: *shlvl, + + // before after + TimezoneBefore: *timezoneBefore, + + RealtimeBefore: realtimeBefore, + RealtimeBeforeLocal: realtimeBeforeLocal, + + RealtimeSinceSessionStart: realtimeSinceSessionStart, + RealtimeSinceBoot: realtimeSinceBoot, + + GitDir: gitDir, + GitRealDir: gitRealDir, + GitOriginRemote: *gitRemote, + MachineID: collect.ReadFileContent(machineIDPath), + + OsReleaseID: *osReleaseID, + OsReleaseVersionID: *osReleaseVersionID, + OsReleaseIDLike: *osReleaseIDLike, + OsReleaseName: *osReleaseName, + OsReleasePrettyName: *osReleasePrettyName, + + PartOne: true, + + ReshUUID: collect.ReadFileContent(reshUUIDPath), + ReshVersion: Version, + ReshRevision: Revision, + + RecallActionsRaw: *recallActions, + RecallPrefix: *recallPrefix, + RecallStrategy: *recallStrategy, + }, + } collect.SendRecord(rec, strconv.Itoa(config.Port), "/record") } } diff --git a/cmd/daemon/recall.go b/cmd/daemon/recall.go index 0f0ad83..d5f831b 100644 --- a/cmd/daemon/recall.go +++ b/cmd/daemon/recall.go @@ -24,7 +24,7 @@ func (h *recallHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - rec := records.Record{} + rec := records.SlimRecord{} log.Println("/recall unmarshaling record ...") err = json.Unmarshal(jsn, &rec) if err != nil { diff --git a/pkg/collect/collect.go b/pkg/collect/collect.go index 7f26b4e..56ee374 100644 --- a/pkg/collect/collect.go +++ b/pkg/collect/collect.go @@ -19,7 +19,7 @@ type SingleResponse struct { } // SendRecallRequest to daemon -func SendRecallRequest(r records.Record, port string) string { +func SendRecallRequest(r records.SlimRecord, port string) string { recJSON, err := json.Marshal(r) if err != nil { log.Fatal("send err 1", err) diff --git a/pkg/records/records.go b/pkg/records/records.go index 3b4170a..319f0a0 100644 --- a/pkg/records/records.go +++ b/pkg/records/records.go @@ -129,6 +129,21 @@ type FallbackRecord struct { Lines int `json:"lines"` // notice the int type } +// SlimRecord used for recalling because unmarshalling record w/ 50+ fields is too slow +type SlimRecord struct { + SessionID string `json:"sessionId"` + RecallHistno int `json:"recallHistno,omitempty"` + RecallPrefix string `json:"recallPrefix,omitempty"` + + // extra recall - we might use these in the future + // Pwd string `json:"pwd"` + // RealPwd string `json:"realPwd"` + // GitDir string `json:"gitDir"` + // GitRealDir string `json:"gitRealDir"` + // GitOriginRemote string `json:"gitOriginRemote"` + +} + // Convert from FallbackRecord to Record func Convert(r *FallbackRecord) Record { return Record{