Opened on 06/07/2015 at 02:44:28 PM
Closed on 06/08/2015 at 12:33:22 PM
Last modified on 06/08/2015 at 09:17:50 PM
#2658 closed change (fixed)
Get rid of try..catch for filter validation
Reported by: | sebastian | Assignee: | sebastian |
---|---|---|---|
Priority: | P4 | Milestone: | Adblock-Plus-1.9-for-Chrome-Opera-Safari |
Module: | Platform | Keywords: | |
Cc: | Blocked By: | ||
Blocking: | Platform: | Unknown | |
Ready: | yes | Confidential: | no |
Tester: | Verified working: | no | |
Review URL(s): |
Description (last modified by sebastian)
Background
The parseFilter() and parseFilters() functions parse filters and raise an exception when encountering invalid filters. Hence calls needs to be wrapped in try..catch blocks. However, the V8 JavaScript engine cannot optimize functions using try statements.
While validating filters isn't really a hotspot, the onMessage handler which currently isn't optimized because of this is one. Removing the try statement there speeds up the time spend in that function by 30%, which corresponds to ~10ms when loading websites like nytimes.com on my notebook.
As a sight effect, returning the error instead throwing it, also speeds up filter validation by ~20%.
Plus it slightly simplifies the calling code, by not having to catch an exception.
What to change
Make parseFilter() and parseFilters() not throw an exception but return an object holding the parsed filter(s) in one property or the error message in another property, and adjust the calling code.
So parseFilters() will either return an object like {filters: [..]} or {error: ..}. And the calling code would look like:
var result = parseFilters(text); if (result.error) ... else { for (var filter of result.filters) ... }
Hints for testers
Make sure that filter validation works as it did before:
- Go to the options page
-
- Add a valid blocking filter (e.g. ||example.com)
- Attempt to add an invalid blocking filter (e.g. ||example.com$bad)
- Add a valid element hiding filter (e.g. ##.foobar)
- Attempt to add an invalid element hiding filter (e.g. ##[foo)
-
- Click "Edit filters as raw text"
- Click "Apply changes"
- Click "Edit filters as raw text" again
- Add some invalid filters (see above for examples)
- Click "Apply changes"
-
- Go to any website
- Click the ABP icon
- Choose "Block Element"
- Select any element on the page
- Click "Add filters"
- Repeat 2.1 through 2.3
- Modify the suggested filters producing one or more invalid filters
- Click "Add filters"
When you did enter any invalid filters you should see an alert() dialog with an error message. Otherwise the filter(s) should be saved.
Attachments (0)
Change History (8)
comment:1 Changed on 06/07/2015 at 02:47:31 PM by sebastian
- Review URL(s) modified (diff)
- Status changed from new to reviewing
comment:4 Changed on 06/08/2015 at 12:33:22 PM by sebastian
- Milestone set to Adblock-Plus-for-Chrome-Opera-Safari-next
- Resolution set to fixed
- Status changed from reviewing to closed
https://hg.adblockplus.org/adblockpluschrome/rev/60c0d0cc374f