Skip to content

Commit

Permalink
Performance fix - single parsing when validator is created instead of…
Browse files Browse the repository at this point in the history
… two. Version bump.
  • Loading branch information
jwaliszko committed Aug 12, 2017
1 parent b2bd4f6 commit 151612d
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 24 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -755,14 +755,16 @@ Alternatively, to enforce re-binding of already attached validation handlers, us

##### <a id="can-i-increase-web-console-verbosity-for-debug-purposes">Can I increase web console verbosity for debug purposes?</a>

If you need more insightful overview of what client-side script is doing (including warnings if detected) enable logging:
If you need more insightful overview of what client-side script is doing, enable verbose logging:

```JavaScript
<script>
ea.settings.debug = true; // output debug messages to the web console
// (should be disabled for release code not to introduce redundant overhead)
```

Warnings and errors are always flushed to the console, no matter this feature is enabled or not.

##### <a id="i-prefer-enum-values-to-be-rendered-as-numbers-is-it-allowed">I prefer enum values to be rendered as numbers, is it allowed?</a>

There is a possibility to setup of how enumeration values are internally processed: as integral numerics or string identifiers - see `enumsAsNumbers` settings flag in the script.
Expand Down
Binary file modified doc/api/api.chm
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ public void segments_collisions_are_detected()
}

[Fact]
public void naming_colissions_at_client_side_are_detected()
public void naming_collisions_at_client_side_are_detected()
{
var model = new Model();
var metadata = GetModelMetadata(model, m => m.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.5.3.0")]
[assembly: AssemblyFileVersion("2.5.3.0")]
[assembly: AssemblyVersion("2.5.4.0")]
[assembly: AssemblyFileVersion("2.5.4.0")]
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
using System.Linq.Expressions;
using System.Threading;
using System.Web.Mvc;
using ExpressiveAnnotations.Analysis;
using ExpressiveAnnotations.Attributes;
using ExpressiveAnnotations.Functions;
using ExpressiveAnnotations.MvcUnobtrusive.Caching;

namespace ExpressiveAnnotations.MvcUnobtrusive.Validators
Expand Down Expand Up @@ -48,15 +46,18 @@ protected ExpressiveValidator(ModelMetadata metadata, ControllerContext context,
{ // (by design, no reason to recompile once compiled expressions)
Debug.WriteLine($"[cache add] process: {Process.GetCurrentProcess().Id}, thread: {Thread.CurrentThread.ManagedThreadId}");

var parser = new Parser();
parser.RegisterToolchain();
parser.Parse<bool>(metadata.ContainerType, attribute.Expression);
IDictionary<string, Expression> fields = null;
attribute.Compile(metadata.ContainerType, parser =>
{
fields = parser.GetFields();
FieldsMap = fields.ToDictionary(x => x.Key, x => Helper.GetCoarseType(x.Value.Type));
ConstsMap = parser.GetConsts();
EnumsMap = parser.GetEnums();
MethodsList = parser.GetMethods();
}); // compile the expression associated with attribute (to be cached for subsequent invocations)

AssertClientSideCompatibility();

var fields = parser.GetFields();
FieldsMap = fields.ToDictionary(x => x.Key, x => Helper.GetCoarseType(x.Value.Type));
ConstsMap = parser.GetConsts();
EnumsMap = parser.GetEnums();
MethodsList = parser.GetMethods();
ParsersMap = fields
.Select(kvp => new
{
Expand All @@ -74,9 +75,6 @@ protected ExpressiveValidator(ModelMetadata metadata, ControllerContext context,
ParsersMap.Add(new KeyValuePair<string, string>(metadata.PropertyName, valueParser.ParserName));
}

AssertClientSideCompatibility();
attribute.Compile(metadata.ContainerType); // compile expressions in attributes (to be cached for subsequent invocations)

return new CacheItem
{
FieldsMap = FieldsMap,
Expand Down
9 changes: 6 additions & 3 deletions src/ExpressiveAnnotations/Attributes/ExpressiveAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ protected ExpressiveAttribute(string expression, Func<string> errorMessageAccess
protected Dictionary<Type, Func<object, bool>> CachedValidationFuncs { get; private set; }

/// <summary>
/// Gets the parser.
/// Gets the parser associated with this attribute.
/// </summary>
protected Parser Parser { get; private set; }

Expand Down Expand Up @@ -152,17 +152,20 @@ public override string ToString()
/// Parses and compiles expression provided to the attribute. Compiled lambda is then cached and used for validation purposes.
/// </summary>
/// <param name="validationContextType">The type of the object to be validated.</param>
/// <param name="force">Flag indicating whether parsing should be rerun despite the fact compiled lambda already exists.</param>
/// <param name="action">The action to be invoked after compilation is done. <see cref="Parser" /> object, containing all of the computed data, is given as an input parameter.</param>
/// <param name="force">Flag indicating whether parsing should be invoked again despite the fact compiled lambda already exists.</param>
/// <exception cref="ParseErrorException"></exception>
public void Compile(Type validationContextType, bool force = false)
public void Compile(Type validationContextType, Action<Parser> action = null, bool force = false)
{
if (force)
{
CachedValidationFuncs[validationContextType] = Parser.Parse<bool>(validationContextType, Expression);
action?.Invoke(Parser);
return;
}
if (!CachedValidationFuncs.ContainsKey(validationContextType))
CachedValidationFuncs[validationContextType] = Parser.Parse<bool>(validationContextType, Expression);
action?.Invoke(Parser);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/ExpressiveAnnotations/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("2.7.2.0")]
[assembly: AssemblyFileVersion("2.7.2.0")]
[assembly: AssemblyVersion("2.7.3.0")]
[assembly: AssemblyFileVersion("2.7.3.0")]
2 changes: 1 addition & 1 deletion src/expressive.annotations.validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ var
string: {
format: function(text, params) {
function makeParam(value) {
var replacer = function (key, value) {
var replacer = function(key, value) {
return typeof value === 'function' ? 'function(...) {...}' : value;
}
value = typeHelper.isObject(value) ? JSON.stringify(value, replacer, 4): value;
Expand Down
2 changes: 1 addition & 1 deletion src/expressive.annotations.validate.min.js

Large diffs are not rendered by default.

0 comments on commit 151612d

Please sign in to comment.