Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2)

Side by Side Diff: src/sksl/ir/SkSLSymbolTable.cpp

Issue 2131223002: SkSL performance improvements (Closed) Base URL: https://skia.googlesource.com/skia@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/sksl/ir/SkSLSymbolTable.h ('k') | src/sksl/ir/SkSLType.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkSLSymbolTable.h" 8 #include "SkSLSymbolTable.h"
9 #include "SkSLUnresolvedFunction.h"
9 10
10 namespace SkSL { 11 namespace SkSL {
11 12
12 std::vector<std::shared_ptr<FunctionDeclaration>> SymbolTable::GetFunctions( 13 std::vector<const FunctionDeclaration*> SymbolTable::GetFunctions(const Symbol& s) {
13 const std::shar ed_ptr<Symbol>& s) { 14 switch (s.fKind) {
14 switch (s->fKind) {
15 case Symbol::kFunctionDeclaration_Kind: 15 case Symbol::kFunctionDeclaration_Kind:
16 return { std::static_pointer_cast<FunctionDeclaration>(s) }; 16 return { &((FunctionDeclaration&) s) };
17 case Symbol::kUnresolvedFunction_Kind: 17 case Symbol::kUnresolvedFunction_Kind:
18 return ((UnresolvedFunction&) *s).fFunctions; 18 return ((UnresolvedFunction&) s).fFunctions;
19 default: 19 default:
20 return { }; 20 return { };
21 } 21 }
22 } 22 }
23 23
24 std::shared_ptr<Symbol> SymbolTable::operator[](const std::string& name) { 24 const Symbol* SymbolTable::operator[](const std::string& name) {
25 const auto& entry = fSymbols.find(name); 25 const auto& entry = fSymbols.find(name);
26 if (entry == fSymbols.end()) { 26 if (entry == fSymbols.end()) {
27 if (fParent) { 27 if (fParent) {
28 return (*fParent)[name]; 28 return (*fParent)[name];
29 } 29 }
30 return nullptr; 30 return nullptr;
31 } 31 }
32 if (fParent) { 32 if (fParent) {
33 auto functions = GetFunctions(entry->second); 33 auto functions = GetFunctions(*entry->second);
34 if (functions.size() > 0) { 34 if (functions.size() > 0) {
35 bool modified = false; 35 bool modified = false;
36 std::shared_ptr<Symbol> previous = (*fParent)[name]; 36 const Symbol* previous = (*fParent)[name];
37 if (previous) { 37 if (previous) {
38 auto previousFunctions = GetFunctions(previous); 38 auto previousFunctions = GetFunctions(*previous);
39 for (const std::shared_ptr<FunctionDeclaration>& prev : previous Functions) { 39 for (const FunctionDeclaration* prev : previousFunctions) {
40 bool found = false; 40 bool found = false;
41 for (const std::shared_ptr<FunctionDeclaration>& current : f unctions) { 41 for (const FunctionDeclaration* current : functions) {
42 if (current->matches(*prev)) { 42 if (current->matches(*prev)) {
43 found = true; 43 found = true;
44 break; 44 break;
45 } 45 }
46 } 46 }
47 if (!found) { 47 if (!found) {
48 functions.push_back(prev); 48 functions.push_back(prev);
49 modified = true; 49 modified = true;
50 } 50 }
51 } 51 }
52 if (modified) { 52 if (modified) {
53 ASSERT(functions.size() > 1); 53 ASSERT(functions.size() > 1);
54 return std::shared_ptr<Symbol>(new UnresolvedFunction(functi ons)); 54 return this->takeOwnership(new UnresolvedFunction(functions) );
55 } 55 }
56 } 56 }
57 } 57 }
58 } 58 }
59 return entry->second; 59 return entry->second;
60 } 60 }
61 61
62 void SymbolTable::add(const std::string& name, std::shared_ptr<Symbol> symbol) { 62 Symbol* SymbolTable::takeOwnership(Symbol* s) {
63 const auto& existing = fSymbols.find(name); 63 fOwnedPointers.push_back(std::unique_ptr<Symbol>(s));
64 if (existing == fSymbols.end()) { 64 return s;
65 fSymbols[name] = symbol; 65 }
66 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) { 66
67 const std::shared_ptr<Symbol>& oldSymbol = existing->second; 67 void SymbolTable::add(const std::string& name, std::unique_ptr<Symbol> symbol) {
68 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) { 68 this->addWithoutOwnership(name, symbol.get());
69 std::vector<std::shared_ptr<FunctionDeclaration>> functions; 69 fOwnedPointers.push_back(std::move(symbol));
70 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(oldSymbol)); 70 }
71 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(symbol)); 71
72 fSymbols[name].reset(new UnresolvedFunction(std::move(functions) )); 72 void SymbolTable::addWithoutOwnership(const std::string& name, const Symbol* sym bol) {
73 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) { 73 const auto& existing = fSymbols.find(name);
74 std::vector<std::shared_ptr<FunctionDeclaration>> functions; 74 if (existing == fSymbols.end()) {
75 for (const auto& f : ((UnresolvedFunction&) *oldSymbol).fFunctio ns) { 75 fSymbols[name] = symbol;
76 functions.push_back(f); 76 } else if (symbol->fKind == Symbol::kFunctionDeclaration_Kind) {
77 } 77 const Symbol* oldSymbol = existing->second;
78 functions.push_back(std::static_pointer_cast<FunctionDeclaration >(symbol)); 78 if (oldSymbol->fKind == Symbol::kFunctionDeclaration_Kind) {
79 fSymbols[name].reset(new UnresolvedFunction(std::move(functions) )); 79 std::vector<const FunctionDeclaration*> functions;
80 functions.push_back((const FunctionDeclaration*) oldSymbol);
81 functions.push_back((const FunctionDeclaration*) symbol);
82 UnresolvedFunction* u = new UnresolvedFunction(std::move(functions)) ;
83 fSymbols[name] = u;
84 this->takeOwnership(u);
85 } else if (oldSymbol->fKind == Symbol::kUnresolvedFunction_Kind) {
86 std::vector<const FunctionDeclaration*> functions;
87 for (const auto* f : ((UnresolvedFunction&) *oldSymbol).fFunctions) {
88 functions.push_back(f);
80 } 89 }
81 } else { 90 functions.push_back((const FunctionDeclaration*) symbol);
82 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was a lready defined"); 91 UnresolvedFunction* u = new UnresolvedFunction(std::move(functions)) ;
92 fSymbols[name] = u;
93 this->takeOwnership(u);
83 } 94 }
95 } else {
96 fErrorReporter.error(symbol->fPosition, "symbol '" + name + "' was alrea dy defined");
84 } 97 }
98 }
99
85 } // namespace 100 } // namespace
OLDNEW
« no previous file with comments | « src/sksl/ir/SkSLSymbolTable.h ('k') | src/sksl/ir/SkSLType.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698