1
0
Fork 0
mirror of https://github.com/anyproto/anytype-heart.git synced 2025-06-07 21:37:04 +09:00

GO-3010 Turn on fast building

This commit is contained in:
Mikhail Iudin 2024-03-04 15:00:11 +01:00
parent 0bafa47fec
commit d581888ae0
No known key found for this signature in database
GPG key ID: FAAAA8BAABDFF1C0
6 changed files with 41 additions and 8 deletions

View file

@ -143,8 +143,14 @@ func (s *State) Merge(s2 *State) *State {
return s
}
// ApplyChange used in tests only
func (s *State) ApplyChange(changes ...*pb.ChangeContent) (err error) {
defer s.resetParentIdsCache()
alreadyEnabled := s.EnableParentIdsCache()
defer func() {
if !alreadyEnabled {
s.ResetParentIdsCache()
}
}()
for _, ch := range changes {
if err = s.applyChange(ch); err != nil {
return
@ -175,7 +181,12 @@ func (s *State) GetAndUnsetFileKeys() (keys []pb.ChangeFileKeys) {
// ApplyChangeIgnoreErr should be called with changes from the single pb.Change
func (s *State) ApplyChangeIgnoreErr(changes ...*pb.ChangeContent) {
defer s.resetParentIdsCache()
alreadyEnabled := s.EnableParentIdsCache()
defer func() {
if !alreadyEnabled {
s.ResetParentIdsCache()
}
}()
for _, ch := range changes {
if err := s.applyChange(ch); err != nil {
log.With("objectID", s.RootId()).Warnf("error while applying change %T: %v; ignore", ch.Value, err)

View file

@ -340,11 +340,19 @@ func (s *State) PickParentOf(id string) (res simple.Block) {
return
}
func (s *State) resetParentIdsCache() {
func (s *State) ResetParentIdsCache() {
s.parentIdsCache = nil
s.isParentIdsCacheEnabled = false
}
func (s *State) EnableParentIdsCache() bool {
if s.isParentIdsCacheEnabled {
return true
}
s.isParentIdsCacheEnabled = true
return false
}
func (s *State) getParentIdsCache() map[string]string {
if s.parentIdsCache == nil {
s.parentIdsCache = make(map[string]string)
@ -462,7 +470,12 @@ func ApplyStateFastOne(s *State) (msgs []simple.EventMessage, action undo.Action
}
func (s *State) apply(fast, one, withLayouts bool) (msgs []simple.EventMessage, action undo.Action, err error) {
defer s.resetParentIdsCache()
alreadyEnabled := s.EnableParentIdsCache()
defer func() {
if !alreadyEnabled {
s.ResetParentIdsCache()
}
}()
if s.parent != nil && (s.parent.parent != nil || fast) {
s.intermediateApply()
if one {

View file

@ -207,6 +207,7 @@ func (s *source) Update(ot objecttree.ObjectTree) {
// todo: check this one
err := s.receiver.StateAppend(func(d state.Doc) (st *state.State, changes []*pb.ChangeContent, err error) {
st, changes, sinceSnapshot, err := BuildStateFull(s.spaceID, d.(*state.State), ot, "")
defer st.ResetParentIdsCache()
if prevSnapshot != s.lastSnapshotId {
s.changesSinceSnapshot = sinceSnapshot
} else {
@ -270,6 +271,7 @@ func (s *source) readDoc(receiver ChangeReceiver) (doc state.Doc, err error) {
func (s *source) buildState() (doc state.Doc, err error) {
st, _, changesAppliedSinceSnapshot, err := BuildState(s.spaceID, nil, s.ObjectTree)
defer st.ResetParentIdsCache()
if err != nil {
return
}
@ -540,6 +542,7 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb
startId = ot.Root().Id
} else {
st = initState
st.EnableParentIdsCache()
startId = st.ChangeId()
}
@ -561,6 +564,7 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb
st = state.NewDoc(ot.Id(), nil).(*state.State)
}
st.SetChangeId(change.Id)
st.EnableParentIdsCache()
return true
}
@ -572,10 +576,10 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb
if st == nil {
changesAppliedSinceSnapshot = 0
st = state.NewDocFromSnapshot(ot.Id(), model.Snapshot, state.WithChangeId(startId), state.WithInternalKey(uniqueKeyInternalKey)).(*state.State)
return true
} else {
st = st.NewState()
}
st.EnableParentIdsCache()
return true
}
if model.Snapshot != nil {
@ -605,7 +609,6 @@ func BuildState(spaceId string, initState *state.State, ot objecttree.ReadableOb
return
}
// BuildStateFull is deprecated, used in tests only, use BuildState instead
func BuildStateFull(spaceId string, initState *state.State, ot objecttree.ReadableObjectTree, profileId string) (st *state.State, appliedContent []*pb.ChangeContent, changesAppliedSinceSnapshot int, err error) {
var (
startId string
@ -618,6 +621,7 @@ func BuildStateFull(spaceId string, initState *state.State, ot objecttree.Readab
} else {
st = initState
startId = st.ChangeId()
st.EnableParentIdsCache()
}
var lastMigrationVersion uint32
@ -628,6 +632,7 @@ func BuildStateFull(spaceId string, initState *state.State, ot objecttree.Readab
if change.Id == ot.Id() {
st = state.NewDoc(ot.Id(), nil).(*state.State)
st.SetChangeId(change.Id)
st.EnableParentIdsCache()
return true
}
@ -643,6 +648,7 @@ func BuildStateFull(spaceId string, initState *state.State, ot objecttree.Readab
} else {
st = st.NewState()
}
st.EnableParentIdsCache()
return true
}
if model.Snapshot != nil {

View file

@ -83,6 +83,7 @@ func (t *treeImporter) State(fullStateChain bool) (*state.State, error) {
return nil, err
}
}
defer st.ResetParentIdsCache()
if _, _, err = state.ApplyStateFast(st); err != nil {
return nil, err

View file

@ -214,6 +214,7 @@ func (h *history) buildState(id domain.FullID, versionId string) (st *state.Stat
}
st, _, _, err = source.BuildState(id.SpaceID, nil, tree)
defer st.ResetParentIdsCache()
if err != nil {
return
}

View file

@ -25,7 +25,7 @@ You can find all endpoints in `/debug` page. For example: http://localhost:6061/
In order to log mw gRPC requests/responses use `ANYTYPE_GRPC_LOG` env var:
- `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=1` - log only method names
- `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=2` - log method names + payloads for commands
- `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=2` - log method names + payloads for commands&events
- `ANYTYPE_LOG_LEVEL="grpc=DEBUG" ANYTYPE_GRPC_LOG=3` - log method names + payloads for commands&events
### gRPC tracing
1. Run jaeger UI on the local machine:
@ -33,8 +33,9 @@ In order to log mw gRPC requests/responses use `ANYTYPE_GRPC_LOG` env var:
2. Run mw with `ANYTYPE_GRPC_TRACE` env var:
- `ANYTYPE_GRPC_TRACE=1` - log only method names/times
- `ANYTYPE_GRPC_TRACE=2` - log method names + payloads for commands
- `ANYTYPE_GRPC_TRACE=2` - log method names + payloads for commands&events
- `ANYTYPE_GRPC_TRACE=3` - log method names + payloads for commands&events
3. Open Jaeger UI at http://localhost:16686
4. If you can't see anything use JAEGER_SAMPLER_TYPE="const" and JAEGER_SAMPLER_PARAM=1 env vars to force sampling
### Debug tree
1. You can use `cmd/debugtree.go` to perform different operations with tree exported in zip archive (`rpc DebugTree`)