-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/optimise' into develop
This PR improves performance of `FlashString` library object iteration and indexing. For example, calling `indexOf` on an `Array<uint32_t>` object results in repeated calls to the `data()` method. Further, the C++ iterator implementation does the same. A new speed test module has been added to evaulate performance. This uses some moderately large test objects and evaluates how long it takes to iterate through them using three methods: 1. regular `for(unsigned i=0; i<array.count(); ++i)` loop 2. C++ iterator `for(auto: array)` 3. Using the `indexOf` method Array<int> has 1000 elements. Vector<String> has 367 elements, referencing 3208 bytes of string data. Map<int, String> has 367 elements. These are the results, in microseconds, on an esp8266: | Operation | Current | With this PR | -------------------------------------- | ------------ | ------------ | | Array<int> for-loop | 1603 | 1611 | | Array<int> iterator | 1326 | 403 | | Array<int>.indexOf | 1303 | 425 | | Vector<String> for-loop | 800 | 721 | | Vector<String> iterator | 774 | 402 | | Vector<String> indexOf(const char*) | 6634 | 969 | | Vector<String> indexOf(String) | 849 | 476 | | Map<int, String> for-loop | 1042 | 1085 | | Map<int, String> iterator | 891 | 577 | | Map<int, String> indexOf | 189 | 189 | | Map<int, String> lookup | 168 | 168 | **Optimise iteration and `indexOf` operation** The regular `[]` operator and `valueAt` methods include range checks which are costly inside a loop. Adding `unsafe` methods skips these checks. **Improve Vector search for char* argument** The `indexOf` method has been specialised for `char*` arguments so that `strlen()` only needs to be called once. Previously it was called on every loop iteration. **Provide base `==` operator which does binary comparison** This allows easy searching by value (object content). **Rationalise string comparison methods** FlashStrings have three `equals` overloads for `char*`, `String` and `FlashString` arguments. We also require three `equalsignorecase` equivalents and appropriate `operator==` implementations. By adding an `ignoreCase` parameter to all three `equals` methods we can simplify code. **Other improvements** - Use const references instead of copy - Move copy/invalidate operations to constructors - Use constexpr where possible for method return values - Move `printElement(Print, char)` specialization into source file - Move `read` method out of header - Add github workflow - Put strings in flash
- Loading branch information
Showing
22 changed files
with
897 additions
and
199 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: CI Dispatch | ||
|
||
on: | ||
workflow_dispatch: | ||
inputs: | ||
sming_repo: | ||
description: 'Full URL for Sming repository' | ||
default: 'https://github.com/SmingHub/Sming' | ||
type: string | ||
sming_branch: | ||
description: 'Sming branch to run against' | ||
default: 'develop' | ||
type: string | ||
|
||
jobs: | ||
build: | ||
uses: SmingHub/Sming/.github/workflows/library.yml@develop | ||
with: | ||
sming_repo: ${{ inputs.sming_repo }} | ||
sming_branch: ${{ inputs.sming_branch }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: CI Push | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
uses: SmingHub/Sming/.github/workflows/library.yml@develop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/**** | ||
* ArrayPrinter.cpp - Print support for arrays | ||
* | ||
* Copyright 2019 mikee47 <mike@sillyhouse.net> | ||
* | ||
* This file is part of the FlashString Library | ||
* | ||
* This library is free software: you can redistribute it and/or modify it under the terms of the | ||
* GNU General Public License as published by the Free Software Foundation, version 3 or later. | ||
* | ||
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | ||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with this library. | ||
* If not, see <https://www.gnu.org/licenses/>. | ||
* | ||
****/ | ||
|
||
#include "include/FlashString/ArrayPrinter.hpp" | ||
|
||
namespace FSTR | ||
{ | ||
size_t printElement(Print& p, char c) | ||
{ | ||
auto escape = [](char c) -> char { | ||
switch(c) { | ||
case '\0': | ||
return '0'; | ||
case '\'': | ||
return '\''; | ||
case '\"': | ||
return '"'; | ||
case '\?': | ||
return '?'; | ||
case '\\': | ||
return '\\'; | ||
case '\a': | ||
return 'a'; | ||
case '\b': | ||
return 'b'; | ||
case '\f': | ||
return 'f'; | ||
case '\n': | ||
return 'n'; | ||
case '\r': | ||
return 'r'; | ||
case '\t': | ||
return 't'; | ||
case '\v': | ||
return 'v'; | ||
default: | ||
return '\0'; | ||
} | ||
}; | ||
|
||
char buf[8]; | ||
char* o = buf; | ||
*o++ = '\''; | ||
char esc = escape(c); | ||
if(esc) { | ||
*o++ = '\\'; | ||
*o++ = esc; | ||
} else if(isprint(c)) { | ||
*o++ = c; | ||
} else { | ||
*o++ = '\\'; | ||
*o++ = 'x'; | ||
*o++ = hexchar(uint8_t(c) >> 4); | ||
*o++ = hexchar(uint8_t(c) & 0x0f); | ||
} | ||
*o++ = '\''; | ||
return p.write(buf, o - buf); | ||
} | ||
|
||
} // namespace FSTR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.