Files
sdk/pkg/analysis_server/lib
Jens Johansen 807d1cf47b [analyzer] Don't cache completion data
TL;DR: This CL is shown to reduce memory usage by ~660MB when asking
for many completions.

Before this CL the analyzer cached `_ElementCompletionData` for every
element that have ever had it calculated. This itself contains new
strings, lists etc.

This was likely (although I'm guessing) done because it was asked for
it a lot: In my benchmarks, asking for at most 250 completions for each
of the first 200 files (twice) it was asked more than 113 million times.
Likely (extrapolating) it would take ~26 minutes creating these if not
cached.

In practise we don't need to ask for >113 million of them though.
Each request only wants at most, say, 100 results and likely fewer.
In the benchmark the `build` command was only run ~31% more times than
the number of caches. Building 100 results (again just using the
average) would take ~1.4 ms so hardly matters.

This CL removes the caching (including the field on many `Element`s),
and only calculates it when needed (i.e. on `build` as it's about to be
sent to the users IDE).

When asking for at most 250 completions in the first 1000 files in flutter/flutter (80+ contexts) we save ~669MB of heap usage, making
the heap usage decrease from ~8.34GB to ~7.69GB.

Details:

```
ExtensionElementImpl (package:analyzer/src/dart/element/element.dart) (bytes):
Difference at 95.0% confidence
   -1185728.00 +/- 413.05
   -8.36% +/- 0.00%

SimpleIdentifierImpl (package:analyzer/src/dart/ast/ast.dart) (bytes):
Difference at 95.0% confidence
   -1187104.00 +/- 133935.98
   -1.14% +/- 0.13%

_List (dart:core) (instances):
Difference at 95.0% confidence
   -1204916.00 +/- 5571.96
   -10.08% +/- 0.05%

ConstTopLevelVariableElementImpl (package:analyzer/src/dart/element/element.dart) (bytes):
Difference at 95.0% confidence
   -1301800.00 +/- 12081.85
   -7.93% +/- 0.07%

_OneByteString (dart:core) (instances):
Difference at 95.0% confidence
   -1825992.50 +/- 186205.44
   -28.49% +/- 2.91%

ConstructorElementImpl (package:analyzer/src/dart/element/element.dart) (bytes):
Difference at 95.0% confidence
   -5518400.00 +/- 135361.21
   -6.29% +/- 0.15%

ClassElementImpl (package:analyzer/src/dart/element/element.dart) (bytes):
Difference at 95.0% confidence
   -6820704.00 +/- 21606.00
   -6.34% +/- 0.02%

CompletionDefaultArgumentList (package:analysis_server/src/services/completion/dart/utilities.dart) (bytes):
Difference at 95.0% confidence
   -9054304.00 +/- 6195.82
   -100.00% +/- 0.07%

InterfaceTypeImpl (package:analyzer/src/dart/element/type.dart) (bytes):
Difference at 95.0% confidence
   -9941600.00 +/- 776821.86
   -2.32% +/- 0.18%

ElementLocationImpl (package:analyzer/src/dart/element/element.dart) (bytes):
Difference at 95.0% confidence
   -10401472.00 +/- 111022.97
   -46.19% +/- 0.49%

_TwoByteString (dart:core) (bytes):
Difference at 95.0% confidence
   -11262480.00 +/- 227076.11
   -11.77% +/- 0.24%

MethodElementImpl (package:analyzer/src/dart/element/element.dart) (bytes):
Difference at 95.0% confidence
   -13877752.00 +/- 351565.94
   -7.70% +/- 0.20%

_ElementDocumentation (package:analysis_server/src/services/completion/dart/suggestion_builder.dart) (bytes):
Difference at 95.0% confidence
   -18954304.00 +/- 202259.10
   -100.00% +/- 1.07%

_GrowableList (dart:core) (bytes):
Difference at 95.0% confidence
   -23003968.00 +/- 3135.17
   -25.12% +/- 0.00%

Location (package:analyzer_plugin/protocol/protocol_common.dart) (bytes):
Difference at 95.0% confidence
   -63403200.00 +/- 444171.45
   -99.68% +/- 0.70%

_List (dart:core) (bytes):
Difference at 95.0% confidence
   -71587648.00 +/- 2062003.75
   -5.54% +/- 0.16%

Element (package:analyzer_plugin/protocol/protocol_common.dart) (bytes):
Difference at 95.0% confidence
   -79254080.00 +/- 555214.31
   -100.00% +/- 0.70%

_ElementCompletionData (package:analysis_server/src/services/completion/dart/suggestion_builder.dart) (bytes):
Difference at 95.0% confidence
   -110955712.00 +/- 777300.03
   -100.00% +/- 0.70%

_OneByteString (dart:core) (bytes):
Difference at 95.0% confidence
   -258932360.00 +/- 9586397.08
   -35.00% +/- 1.30%

maxRSS:
Difference at 95.0% confidence
   -327434240.00 +/- 121326188.82
   -3.05% +/- 1.13%

currentRSS:
Difference at 95.0% confidence
   -551153664.00 +/- 121286389.20
   -5.34% +/- 1.18%

heapCapacity:
Difference at 95.0% confidence
   -555710464.00 +/- 164879781.95
   -5.75% +/- 1.71%

heapUsage:
Difference at 95.0% confidence
   -701868280.00 +/- 8054728.47
   -7.84% +/- 0.09%
```

Change-Id: I3b3631520d56ab36dabaee913e802aa1b7ebacfe
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/326822
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Jens Johansen <jensj@google.com>
2023-09-19 18:40:30 +00:00
..