Skip to content

Commit 483d611

Browse files
committed
Reached 0.90s performance
1 parent 487a4e9 commit 483d611

16 files changed

+193
-204
lines changed

src/ast_values.cpp

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ namespace Sass {
4949

5050
bool Value::_selectorStringOrNull(Logger& logger, sass::string& rv) {
5151

52-
if (String_Constant* str = Cast<String_Constant>(this)) {
52+
if (String_Constant* str = isString()) {
5353
rv = str->value();
5454
return true;
5555
}
5656

57-
if (SassList * list = Cast<SassList>(this)) {
57+
if (SassList * list = isList()) {
5858

5959
sass::vector<ValueObj> values = list->asVector();
6060

@@ -63,8 +63,8 @@ namespace Sass {
6363
sass::vector<sass::string> result;
6464
if (list->separator() == SASS_COMMA) {
6565
for (auto complex : values) {
66-
SassList* cplxLst = Cast<SassList>(complex);
67-
String_Constant* cplxStr = Cast<String_Constant>(complex);
66+
SassList* cplxLst = complex->isList();
67+
String_Constant* cplxStr = complex->isString();
6868
if (cplxStr) {
6969
result.emplace_back(cplxStr->value());
7070
}
@@ -82,7 +82,7 @@ namespace Sass {
8282
}
8383
else {
8484
for (auto compound : values) {
85-
String_Constant* cmpdStr = Cast<String_Constant>(compound);
85+
String_Constant* cmpdStr = compound->isString();
8686
if (cmpdStr) {
8787
result.emplace_back(cmpdStr->value());
8888
}
@@ -174,7 +174,7 @@ namespace Sass {
174174
// The SassScript `+` operation.
175175
inline Value* Value::plus(
176176
Value* other, Logger& logger, const SourceSpan& pstate) const {
177-
if (String_Constant * str = Cast<String_Constant>(other)) {
177+
if (String_Constant * str = other->isString()) {
178178
sass::string text(toCssString() + str->value());
179179
return SASS_MEMORY_NEW(String_Constant,
180180
pstate, text, str->hasQuotes());
@@ -263,7 +263,7 @@ namespace Sass {
263263
// at the same key, order is not important.
264264
bool Map::operator== (const Value& rhs) const
265265
{
266-
if (const Map* r = Cast<Map>(&rhs)) {
266+
if (const Map* r = rhs.isMap()) {
267267
if (size() != r->size()) return false;
268268
for (auto kv : elements_) {
269269
auto lv = kv.second;
@@ -274,7 +274,7 @@ namespace Sass {
274274
}
275275
return true;
276276
}
277-
if (const SassList * r = Cast<SassList>(&rhs)) {
277+
if (const SassList * r = rhs.isList()) {
278278
return r->empty() && empty();
279279
}
280280
return false;
@@ -415,7 +415,7 @@ namespace Sass {
415415

416416
bool Number::operator== (const Value& rhs) const
417417
{
418-
if (const Number* n = Cast<Number>(&rhs)) {
418+
if (const Number* n = rhs.isNumber()) {
419419
return *this == *n;
420420
}
421421
return false;
@@ -574,7 +574,7 @@ namespace Sass {
574574

575575
bool Color_RGBA::operator== (const Value& rhs) const
576576
{
577-
if (auto r = Cast<Color_RGBA>(&rhs)) {
577+
if (auto r = rhs.isColorRGBA()) {
578578
return r_ == r->r() &&
579579
g_ == r->g() &&
580580
b_ == r->b() &&
@@ -659,7 +659,7 @@ namespace Sass {
659659

660660
bool Color_HSLA::operator== (const Value& rhs) const
661661
{
662-
if (auto r = Cast<Color_HSLA>(&rhs)) {
662+
if (auto r = rhs.isColorHSLA()) {
663663
return h_ == r->h() &&
664664
s_ == r->s() &&
665665
l_ == r->l() &&
@@ -726,7 +726,7 @@ namespace Sass {
726726

727727
bool Custom_Error::operator== (const Value& rhs) const
728728
{
729-
if (auto r = Cast<Custom_Error>(&rhs)) {
729+
if (auto r = rhs.isError()) {
730730
return message() == r->message();
731731
}
732732
return false;
@@ -741,7 +741,7 @@ namespace Sass {
741741

742742
bool Custom_Warning::operator== (const Value& rhs) const
743743
{
744-
if (auto r = Cast<Custom_Warning>(&rhs)) {
744+
if (auto r = rhs.isWarning()) {
745745
return message() == r->message();
746746
}
747747
return false;
@@ -765,7 +765,7 @@ namespace Sass {
765765

766766
bool Boolean::operator== (const Value& rhs) const
767767
{
768-
if (auto r = Cast<Boolean>(&rhs)) {
768+
if (auto r = rhs.isBoolean()) {
769769
return (value() == r->value());
770770
}
771771
return false;
@@ -794,10 +794,10 @@ namespace Sass {
794794

795795
const sass::string& Interpolation::getInitialPlain() const
796796
{
797-
if (StringLiteral * str = Cast<StringLiteral>(first())) {
797+
if (StringLiteral * str = Cast<StringLiteral>(first())) { // Ex
798798
return str->text();
799799
}
800-
else if (String_Constant * str = Cast<String_Constant>(first())) {
800+
else if (String_Constant * str = Cast<String_Constant>(first())) { // Ex
801801
return str->value();
802802
}
803803
return empty_string;
@@ -817,7 +817,7 @@ namespace Sass {
817817
using namespace Character;
818818
bool containsDoubleQuote = false;
819819
for (auto item : text_->elements()) {
820-
if (auto str = Cast<String_Constant>(item)) {
820+
if (auto str = Cast<String_Constant>(item)) { // Ex
821821
auto& value = str->value();
822822
for (size_t i = 0; i < value.size(); i++) {
823823
uint8_t codeUnit = value[i];
@@ -854,7 +854,7 @@ namespace Sass {
854854

855855
for (auto value : text_->elements()) {
856856
// assert(value is Expression || value is String);
857-
if (StringLiteral * str = Cast<StringLiteral>(value)) {
857+
if (StringLiteral * str = Cast<StringLiteral>(value)) { // Ex
858858
sass::string value(str->text());
859859
for (size_t i = 0; i < value.size(); i++) {
860860

@@ -885,11 +885,8 @@ namespace Sass {
885885

886886
}
887887
}
888-
else if (Expression * ex = Cast<Expression>(value)) {
889-
buffer.add(ex);
890-
}
891888
else {
892-
std::cerr << "nono item in schema\n";
889+
buffer.add(value);
893890
}
894891
}
895892

@@ -1095,7 +1092,7 @@ namespace Sass {
10951092

10961093
bool SassList::operator==(const Value& rhs) const
10971094
{
1098-
if (const SassList* r = Cast<SassList>(&rhs)) {
1095+
if (const SassList* r = rhs.isList()) {
10991096
if (length() != r->length()) return false;
11001097
if (separator() != r->separator()) return false;
11011098
if (hasBrackets() != r->hasBrackets()) return false;
@@ -1108,7 +1105,7 @@ namespace Sass {
11081105
}
11091106
return true;
11101107
}
1111-
if (const Map * r = Cast<Map>(&rhs)) {
1108+
if (const Map * r = rhs.isMap()) {
11121109
return empty() && r->empty();
11131110
}
11141111
return false;
@@ -1183,10 +1180,10 @@ namespace Sass {
11831180

11841181
bool SassArgumentList::operator==(const Value& rhs) const
11851182
{
1186-
if (const SassList * r = Cast<SassList>(&rhs)) {
1183+
if (const SassList * r = rhs.isList()) {
11871184
return SassList::operator==(*r);
11881185
}
1189-
if (const Map * r = Cast<Map>(&rhs)) {
1186+
if (const Map * r = rhs.isMap()) {
11901187
return SassList::operator==(*r);
11911188
}
11921189
return false;
@@ -1202,7 +1199,7 @@ namespace Sass {
12021199

12031200
bool SassFunction::operator== (const Value& rhs) const
12041201
{
1205-
if (const SassFunction* fn = Cast<SassFunction>(&rhs)) {
1202+
if (const SassFunction* fn = rhs.isFunction()) {
12061203
return ObjEqualityFn(callable_, fn->callable());
12071204
}
12081205
return false;

src/ast_values.hpp

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,32 @@ namespace Sass {
4343
// Return the length of this item as a list
4444
virtual size_t lengthAsList() const { return 1; }
4545

46+
virtual SassMap* isMap() { return nullptr; }
47+
virtual SassList* isList() { return nullptr; }
4648
virtual SassNumber* isNumber() { return nullptr; }
4749
virtual SassColor* isColor() { return nullptr; }
50+
virtual Color_RGBA* isColorRGBA() { return nullptr; }
51+
virtual Color_HSLA* isColorHSLA() { return nullptr; }
52+
virtual SassBoolean* isBoolean() { return nullptr; }
53+
virtual SassFunction* isFunction() { return nullptr; }
4854
virtual StringLiteral* isLiteral() { return nullptr; }
4955
virtual String_Constant* isString() { return nullptr; }
56+
virtual Custom_Error* isError() { return nullptr; }
57+
virtual Custom_Warning* isWarning() { return nullptr; }
58+
virtual SassArgumentList* isArgList() { return nullptr; }
59+
virtual const SassMap* isMap() const { return nullptr; }
60+
virtual const SassList* isList() const { return nullptr; }
5061
virtual const SassNumber* isNumber() const { return nullptr; }
5162
virtual const SassColor* isColor() const { return nullptr; }
5263
virtual const StringLiteral* isLiteral() const { return nullptr; }
5364
virtual const String_Constant* isString() const { return nullptr; }
65+
virtual const Custom_Error* isError() const { return nullptr; }
66+
virtual const Custom_Warning* isWarning() const { return nullptr; }
67+
virtual const Color_RGBA* isColorRGBA() const { return nullptr; }
68+
virtual const Color_HSLA* isColorHSLA() const { return nullptr; }
69+
virtual const SassBoolean* isBoolean() const { return nullptr; }
70+
virtual const SassFunction* isFunction() const { return nullptr; }
71+
virtual const SassArgumentList* isArgList() const { return nullptr; }
5472

5573
virtual bool isVectorEmpty() const {
5674
return list.empty();
@@ -349,7 +367,7 @@ namespace Sass {
349367
}
350368

351369
// We can give some reasonable implementations by using
352-
// inverst operators on the specialized implementations
370+
// invert operators on the specialized implementations
353371
virtual bool operator== (const Expression& rhs) const override {
354372
if (const Value * value = Cast<Value>(&rhs)) {
355373
return *value == *this;
@@ -403,6 +421,9 @@ namespace Sass {
403421
return hasBrackets_;
404422
}
405423

424+
SassList* isList() override final { return this; }
425+
const SassList* isList() const override final { return this; }
426+
406427
Map* assertMap(Logger& logger, const sass::string& name = StrEmpty) override final;
407428

408429
// Return the length of this item as a list
@@ -436,6 +457,8 @@ namespace Sass {
436457
bool is_arglist() const override final {
437458
return true;
438459
}
460+
SassArgumentList* isArgList() override final { return this; }
461+
const SassArgumentList* isArgList() const override final { return this; }
439462
SassArgumentList* assertArgumentList(Logger& logger, const sass::string& name = StrEmpty) override final {
440463
return this;
441464
}
@@ -478,6 +501,9 @@ namespace Sass {
478501
const sass::string& type() const override final { return StrTypeMap; }
479502
bool is_invisible() const override { return empty(); }
480503

504+
SassMap* isMap() override final { return this; }
505+
const SassMap* isMap() const override final { return this; }
506+
481507
Map* assertMap(Logger& logger, const sass::string& name = StrEmpty) override { return this; }
482508

483509
// Return the list separator
@@ -553,6 +579,9 @@ namespace Sass {
553579
const sass::string& type() const override final { return StrTypeFunction; }
554580
bool is_invisible() const override { return true; }
555581

582+
SassFunction* isFunction() override final { return this; }
583+
const SassFunction* isFunction() const override final { return this; }
584+
556585
bool operator== (const Value& rhs) const override;
557586

558587
union Sass_Value* toSassValue() const override final;
@@ -1024,6 +1053,9 @@ namespace Sass {
10241053
virtual const Color_RGBA* isColor() const override final { return this; }
10251054
union Sass_Value* toSassValue() const override final;
10261055

1056+
Color_RGBA* isColorRGBA() override final { return this; }
1057+
const Color_RGBA* isColorRGBA() const override final { return this; }
1058+
10271059
size_t hash() const override;
10281060

10291061
Color_RGBA* copyAsRGBA() const override;
@@ -1058,6 +1090,9 @@ namespace Sass {
10581090
Color_HSLA(const SourceSpan& pstate, double h, double s, double l, double a = 1, const sass::string& disp = StrEmpty);
10591091
union Sass_Value* toSassValue() const override final;
10601092

1093+
Color_HSLA* isColorHSLA() override final { return this; }
1094+
const Color_HSLA* isColorHSLA() const override final { return this; }
1095+
10611096
Color_RGBA* assertColor(Logger& logger, const sass::string& name = StrEmpty) override {
10621097
return toRGBA();
10631098
}
@@ -1087,6 +1122,8 @@ namespace Sass {
10871122
ADD_CONSTREF(sass::string, message)
10881123
public:
10891124
Custom_Error(const SourceSpan& pstate, const sass::string& msg);
1125+
Custom_Error* isError() override final { return this; }
1126+
const Custom_Error* isError() const override final { return this; }
10901127
union Sass_Value* toSassValue() const override final;
10911128
bool operator== (const Value& rhs) const override;
10921129
// ATTACH_CLONE_OPERATIONS(Custom_Error)
@@ -1100,6 +1137,8 @@ namespace Sass {
11001137
ADD_CONSTREF(sass::string, message)
11011138
public:
11021139
Custom_Warning(const SourceSpan& pstate, const sass::string& msg);
1140+
Custom_Warning* isWarning() override final { return this; }
1141+
const Custom_Warning* isWarning() const override final { return this; }
11031142
union Sass_Value* toSassValue() const override final;
11041143
bool operator== (const Value& rhs) const override;
11051144
// ATTACH_CLONE_OPERATIONS(Custom_Warning)
@@ -1116,6 +1155,9 @@ namespace Sass {
11161155
Boolean(const SourceSpan& pstate, bool val);
11171156
operator bool() override { return value_; }
11181157

1158+
SassBoolean* isBoolean() override final { return this; }
1159+
const SassBoolean* isBoolean() const override final { return this; }
1160+
11191161
// Return the list separator
11201162
bool isTruthy() const override final {
11211163
return value_;

0 commit comments

Comments
 (0)