Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache mapping #1132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

# 8.3.0

- Serialization performance: if `toString` is used to map types, cache the
result to improve performance.

# 8.2.1

- Fix deps: allow `built_value_generator` to use `built_value 8.2.0`.
Expand Down
9 changes: 8 additions & 1 deletion built_value/lib/src/built_json_serializers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,15 @@ class BuiltJsonSerializersBuilder implements SerializersBuilder {
}
}

/// Raw name uses `toString` which is not particularly fast, so cache the
/// results.
Map<Type?, String> _rawNameMap = <Type?, String>{};

String _getRawName(Type? type) {
var maybeResult = _rawNameMap[type];
if (maybeResult != null) return maybeResult;
var name = type.toString();
var genericsStart = name.indexOf('<');
return genericsStart == -1 ? name : name.substring(0, genericsStart);
var result = genericsStart == -1 ? name : name.substring(0, genericsStart);
return _rawNameMap[type] = result;
}