• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef ECMASCRIPT_PGO_PROFILER_TYPES_PGO_TYPE_GENERATOR_H
17 #define ECMASCRIPT_PGO_PROFILER_TYPES_PGO_TYPE_GENERATOR_H
18 
19 #include "ecmascript/js_tagged_value.h"
20 #include "ecmascript/js_hclass.h"
21 #include "ecmascript/mem/c_containers.h"
22 #include "ecmascript/pgo_profiler/types/pgo_profile_type.h"
23 #include "ecmascript/platform/mutex.h"
24 #include "utils/span.h"
25 
26 namespace panda::ecmascript::pgo {
27 class PGOTypeGenerator {
28 public:
GenerateProfileType(JSTaggedType child,ProfileType rootType)29     static ProfileType GenerateProfileType(JSTaggedType child, ProfileType rootType)
30     {
31         CString result = JSHClass::DumpToString(child);
32         uint32_t traceId = ComputeHashCode(result);
33         ProfileType type(rootType.GetAbcId(), traceId, rootType.GetKind());
34         return type;
35     }
36 
37 private:
38     static constexpr uint32_t INVALID_ID = 0;
39 
ComputeHashCode(const CString & string)40     static uint32_t ComputeHashCode(const CString &string)
41     {
42         uint32_t hash = INVALID_ID;
43         Span<const char> sp(string.c_str(), string.size());
44         for (auto c : sp) {
45             constexpr size_t SHIFT = 5;
46             hash = (hash << SHIFT) - hash + c;
47         }
48         return hash;
49     }
50 };
51 } // namespace panda::ecmascript::pgo
52 #endif  // ECMASCRIPT_PGO_PROFILER_TYPES_PGO_TYPE_GENERATOR_H
53