3.3 some optimization (#3765)

This commit is contained in:
江战
2021-07-27 13:40:02 +08:00
committed by GitHub
parent efe231f8aa
commit 4d2d4d82b5
12 changed files with 183 additions and 87 deletions
+1
View File
@@ -1702,6 +1702,7 @@ if(USE_SE_V8)
cocos/bindings/jswrapper/v8/Class.cpp
cocos/bindings/jswrapper/v8/Class.h
cocos/bindings/jswrapper/v8/HelperMacros.h
cocos/bindings/jswrapper/v8/HelperMacros.cpp
cocos/bindings/jswrapper/v8/Object.cpp
cocos/bindings/jswrapper/v8/Object.h
cocos/bindings/jswrapper/v8/ObjectWrap.cpp
@@ -0,0 +1,82 @@
/****************************************************************************
Copyright (c) 2021 Xiamen Yaji Software Co., Ltd.
http://www.cocos.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "HelperMacros.h"
#if defined(RECORD_JSB_INVOKING)
namespace {
bool cmp(const std::pair<const char *, std::tuple<int, uint64_t>> &a, const std::pair<const char *, std::tuple<int, uint64_t>> &b) {
return std::get<1>(a.second) > std::get<1>(b.second);
}
unsigned int __jsbInvocationCount; // NOLINT(readability-identifier-naming)
std::map<char const *, std::tuple<int, uint64_t>> __jsbFunctionInvokedRecords; // NOLINT(readability-identifier-naming)
} // namespace
JsbInvokeScopeT::JsbInvokeScopeT(const char *functionName) : _functionName(functionName) {
_start = std::chrono::high_resolution_clock::now();
__jsbInvocationCount++;
}
JsbInvokeScopeT::~JsbInvokeScopeT() {
auto &ref = __jsbFunctionInvokedRecords[_functionName];
std::get<0>(ref) += 1;
std::get<1>(ref) += std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::high_resolution_clock::now() - _start).count();
}
#endif
void printJSBInvokeAtFrame(int n) {
#if defined(RECORD_JSB_INVOKING)
static int cnt = 0;
cnt += 1;
if (cnt % n == 0) {
printJSBInvoke();
}
#endif
}
void clearRecordJSBInvoke() {
#if defined(RECORD_JSB_INVOKING)
__jsbInvocationCount = 0;
__jsbFunctionInvokedRecords.clear();
#endif
}
void printJSBInvoke() {
#if defined(RECORD_JSB_INVOKING)
static std::vector<std::pair<const char *, std::tuple<int, uint64_t>>> pairs;
for (const auto &it : __jsbFunctionInvokedRecords) {
pairs.emplace_back(it); //NOLINT
}
std::sort(pairs.begin(), pairs.end(), cmp);
cc::Log::logMessage(cc::LogType::KERNEL, cc::LogLevel::LEVEL_DEBUG, "Start print JSB function record info....... %d times", __jsbInvocationCount);
for (const auto &pair : pairs) {
cc::Log::logMessage(cc::LogType::KERNEL, cc::LogLevel::LEVEL_DEBUG, "\t%s takes %.3lf ms, invoked %u times,", pair.first, std::get<1>(pair.second) / 1000000.0, std::get<0>(pair.second));
}
pairs.clear();
cc::Log::logMessage(cc::LogType::KERNEL, cc::LogLevel::LEVEL_DEBUG, "End print JSB function record info.......\n");
#endif
}
@@ -26,19 +26,41 @@
#pragma once
#include <algorithm>
#include <chrono>
#include <map>
#include <string>
#include <tuple>
#include <vector>
#include "../config.h"
#include "base/Log.h"
//#define RECORD_JSB_INVOKING
#ifndef CC_DEBUG
#undef RECORD_JSB_INVOKING
#endif
#if SCRIPT_ENGINE_TYPE == SCRIPT_ENGINE_V8
#if defined(CC_DEBUG) & defined(RECORD_JSB_INVOKING)
extern unsigned int __jsbInvocationCount;
extern std::map<std::string, unsigned int> __jsbFunctionInvokedRecords;
#if defined(RECORD_JSB_INVOKING)
class JsbInvokeScopeT {
public:
JsbInvokeScopeT(const char *functionName);
~JsbInvokeScopeT();
private:
const char * _functionName;
std::chrono::time_point<std::chrono::high_resolution_clock> _start;
};
#define JsbInvokeScope(arg) JsbInvokeScopeT invokeScope(arg); // NOLINT(readability-identifier-naming)
#else
// NOLINTNEXTLINE(readability-identifier-naming)
#define JsbInvokeScope(arg) \
do { \
} while (0)
#endif
template <typename T, typename STATE>
@@ -56,39 +78,11 @@ constexpr typename std::enable_if<!std::is_enum<T>::value, char *>::type SE_UNDE
return typeid(T).name();
}
inline bool cmp(const std::pair<std::string, int> &a, const std::pair<std::string, int> &b) {
return a.second < b.second;
}
void clearRecordJSBInvoke();
inline void recordJSBInvoke(const std::string &funcName) {
#if defined(CC_DEBUG) & defined(RECORD_JSB_INVOKING)
++__jsbInvocationCount;
++__jsbFunctionInvokedRecords[funcName];
#endif
}
void printJSBInvoke();
inline void clearRecordJSBInvoke() {
#if defined(CC_DEBUG) & defined(RECORD_JSB_INVOKING)
__jsbInvocationCount = 0;
__jsbFunctionInvokedRecords.clear();
#endif
}
inline void printJSBInvoke() {
#if defined(CC_DEBUG) & defined(RECORD_JSB_INVOKING)
static std::vector<std::pair<std::string, int>> pairs;
for (const auto &it : __jsbFunctionInvokedRecords)
pairs.push_back(it);
std::sort(pairs.begin(), pairs.end(), cmp);
CC_LOG_DEBUG("Start print JSB function record info....... %d times", __jsbInvocationCount);
for (const auto &pair : pairs) {
CC_LOG_DEBUG("%s is invoked %u times.", pair.first.c_str(), pair.second);
}
pairs.clear();
CC_LOG_DEBUG("End print JSB function record info.......\n");
#endif
}
void printJSBInvokeAtFrame(int n);
#ifdef __GNUC__
#define SE_UNUSED __attribute__((unused))
@@ -111,11 +105,11 @@ inline void printJSBInvoke() {
#define SE_BIND_FUNC(funcName) \
void funcName##Registry(const v8::FunctionCallbackInfo<v8::Value> &_v8args) { \
recordJSBInvoke(#funcName); \
bool ret = false; \
v8::Isolate * _isolate = _v8args.GetIsolate(); \
v8::HandleScope _hs(_isolate); \
se::ValueArray args; \
JsbInvokeScope(#funcName); \
bool ret = false; \
v8::Isolate * _isolate = _v8args.GetIsolate(); \
v8::HandleScope _hs(_isolate); \
se::ValueArray args; \
args.reserve(10); \
se::internal::jsToSeArgs(_v8args, &args); \
void * nativeThisObject = se::internal::getPrivate(_isolate, _v8args.This()); \
@@ -129,7 +123,7 @@ inline void printJSBInvoke() {
#define SE_BIND_FINALIZE_FUNC(funcName) \
void funcName##Registry(void *nativeThisObject) { \
recordJSBInvoke(#funcName); \
JsbInvokeScope(#funcName); \
if (nativeThisObject == nullptr) \
return; \
auto se = se::ScriptEngine::getInstance(); \
@@ -148,7 +142,7 @@ inline void printJSBInvoke() {
// v8 doesn't need to create a new JSObject in SE_BIND_CTOR while SpiderMonkey needs.
#define SE_BIND_CTOR(funcName, cls, finalizeCb) \
void funcName##Registry(const v8::FunctionCallbackInfo<v8::Value> &_v8args) { \
recordJSBInvoke(#funcName); \
JsbInvokeScope(#funcName); \
v8::Isolate * _isolate = _v8args.GetIsolate(); \
v8::HandleScope _hs(_isolate); \
bool ret = true; \
@@ -172,7 +166,7 @@ inline void printJSBInvoke() {
#define SE_BIND_PROP_GET(funcName) \
void funcName##Registry(v8::Local<v8::Name> /*_property*/, const v8::PropertyCallbackInfo<v8::Value> &_v8args) { \
recordJSBInvoke(#funcName); \
JsbInvokeScope(#funcName); \
v8::Isolate * _isolate = _v8args.GetIsolate(); \
v8::HandleScope _hs(_isolate); \
bool ret = true; \
@@ -187,7 +181,7 @@ inline void printJSBInvoke() {
#define SE_BIND_PROP_SET(funcName) \
void funcName##Registry(v8::Local<v8::Name> /*_property*/, v8::Local<v8::Value> _value, const v8::PropertyCallbackInfo<void> &_v8args) { \
recordJSBInvoke(#funcName); \
JsbInvokeScope(#funcName); \
v8::Isolate * _isolate = _v8args.GetIsolate(); \
v8::HandleScope _hs(_isolate); \
bool ret = true; \
@@ -75,11 +75,13 @@ template <typename F>
uint64_t convertPtr(F *in) {
return static_cast<uint64_t>(reinterpret_cast<intptr_t>(in));
}
bool mqInitialized{false};
se::Object * msgQueue{nullptr};
se::Object * globalThis{nullptr};
constexpr size_t BUFFER_SIZE{1 * 1024 * 1024}; // 1MB
bool mqInitialized{false};
se::Object * msgQueue{nullptr};
se::Object * globalThis{nullptr};
constexpr size_t BUFFER_SIZE{1 * 1024 * 1024}; // 1MB;
se::Object * msgQueueInfo{nullptr};
uint32_t * msgInfoPtr{nullptr};
std::vector<uint8_t *> msgQueuePtrs;
} // namespace
/**
@@ -87,25 +89,32 @@ constexpr size_t BUFFER_SIZE{1 * 1024 * 1024}; // 1MB
* which sync data to native objects.
*/
void jsbFlushFastMQ() {
if (!mqInitialized) {
if (!mqInitialized || !msgInfoPtr || msgInfoPtr[1] == 0) {
return;
}
se::AutoHandleScope scope;
se::Value maxIdx;
se::Value currentArrayBuffer;
se::Object * arrayBufferObj;
globalThis->getProperty("__fastMQIdx__", &maxIdx);
auto maxSize = maxIdx.toUint32();
uint8_t *mqPtr{nullptr};
uint8_t * mqPtr{nullptr};
const auto minSize = msgInfoPtr[0] + 1;
if (minSize > msgQueuePtrs.size()) {
se::AutoHandleScope scope;
se::Value maxIdx;
se::Value currentArrayBuffer;
se::Object * arrayBufferObj;
auto oldSize = msgQueuePtrs.size();
msgQueuePtrs.resize(minSize);
for (auto i = oldSize; i < minSize; i++) {
msgQueue->getArrayElement(i, &currentArrayBuffer);
arrayBufferObj = currentArrayBuffer.toObject();
arrayBufferObj->getArrayBufferData(&mqPtr, nullptr);
msgQueuePtrs[i] = mqPtr;
}
}
for (auto i = 0; i <= maxSize; i++) {
msgQueue->getArrayElement(i, &currentArrayBuffer);
arrayBufferObj = currentArrayBuffer.toObject();
arrayBufferObj->getArrayBufferData(&mqPtr, nullptr);
for (auto i = 0; i < minSize; i++) {
mqPtr = msgQueuePtrs[i];
auto *u32Ptr = reinterpret_cast<uint32_t *>(mqPtr);
auto offset = *u32Ptr;
auto commands = *(u32Ptr + 1);
auto offset = u32Ptr[0];
auto commands = u32Ptr[1];
assert(offset < BUFFER_SIZE);
if (commands == 0) return;
@@ -113,18 +122,19 @@ void jsbFlushFastMQ() {
uint8_t *p = mqPtr + 8;
for (uint32_t i = 0; i < commands; i++) {
auto * base = reinterpret_cast<uint32_t *>(p);
uint32_t len = *base;
auto * fnAddr = reinterpret_cast<uint64_t *>(base + 1);
auto * fn = reinterpret_cast<FastFunction *>(fnAddr);
auto *base = reinterpret_cast<uint32_t *>(p);
auto len = base[0];
auto *fnAddr = reinterpret_cast<uint64_t *>(base + 1);
auto *fn = reinterpret_cast<FastFunction *>(fnAddr);
(*fn)(p + 12);
p += len;
}
// reset
*u32Ptr = 8;
*(u32Ptr + 1) = 0;
u32Ptr[0] = 8;
u32Ptr[1] = 0;
}
globalThis->setProperty("__fastMQIdx__", se::Value(0));
msgInfoPtr[0] = 0;
msgInfoPtr[1] = 0;
}
bool register_all_drawbatch2d_ext_manual(se::Object *obj) { //NOLINT
@@ -134,16 +144,22 @@ bool register_all_drawbatch2d_ext_manual(se::Object *obj) { //NOLINT
auto * msgArrayBuffer = se::Object::createArrayBufferObject(nullptr, BUFFER_SIZE);
globalThis = se::ScriptEngine::getInstance()->getGlobalObject();
msgQueue = se::Object::createArrayObject(1);
msgQueueInfo = se::Object::createTypedArray(se::Object::TypedArrayType::UINT32, nullptr, sizeof(uint32_t) * 2);
{
uint8_t *data{nullptr};
msgArrayBuffer->getArrayBufferData(&data, nullptr);
auto *int32Data = reinterpret_cast<uint32_t *>(data);
int32Data[0] = 8;
int32Data[1] = 0;
msgQueueInfo->getTypedArrayData(&data, nullptr);
msgInfoPtr = reinterpret_cast<uint32_t *>(data);
msgInfoPtr[0] = 0; // current queue index
msgInfoPtr[1] = 0; // total message count
}
msgQueue->setArrayElement(0, se::Value(msgArrayBuffer));
globalThis->setProperty("__fastMQ__", se::Value(msgQueue));
globalThis->setProperty("__fastMQIdx__", se::Value(0));
globalThis->setProperty("__fastMQInfo__", se::Value(msgQueueInfo));
mqInitialized = true;
@@ -148,7 +148,6 @@ void glThreadEntry() {
"flushTasksOnGameThread");
game->tick();
}
if (cc::cocosApp.destroyRequested) break;
}
@@ -76,6 +76,9 @@ void DescriptorSetAgent::doDestroy() {
}
void DescriptorSetAgent::update() {
// Avoid enqueueing unnecessary command
if (!_isDirty) return;
_isDirty = false;
ENQUEUE_MESSAGE_1(
@@ -1434,7 +1434,7 @@ void cmdFuncGLES3CreateShader(GLES3Device *device, GLES3GPUShader *gpuShader) {
GL_CHECK(glUseProgram(device->stateCache()->glProgram));
}
}
gpuShader->glSamplerTextures = glActiveSamplerTextures;
gpuShader->glSamplerTextures = std::move(glActiveSamplerTextures);
// create uniform storage images
if (!gpuShader->images.empty()) {
@@ -2810,8 +2810,8 @@ void cmdFuncGLES3CopyBuffersToTexture(GLES3Device *device, const uint8_t *const
}
CC_GLES3_API void cmdFuncGLES3CopyTextureToBuffers(GLES3Device *device, GLES3GPUTexture *gpuTexture, uint8_t *const *buffers, const BufferTextureCopy *regions, uint count) {
auto glFormat = mapGLFormat(gpuTexture->format);
auto glType = formatToGLType(gpuTexture->format);
auto glFormat = mapGLFormat(gpuTexture->format);
auto glType = formatToGLType(gpuTexture->format);
for (uint32_t i = 0; i < count; ++i) {
auto region = regions[i];
auto w = region.texExtent.width;
@@ -63,8 +63,9 @@ void DescriptorSetValidator::doDestroy() {
}
void DescriptorSetValidator::update() {
_isDirty = false;
if (!_isDirty) return;
_isDirty = false;
_actor->update();
}
@@ -51,9 +51,11 @@ bool RenderQueue::insertRenderPass(const RenderObject &renderObj, uint subModelI
return false;
}
auto passPriority = static_cast<uint32_t>(pass->getPriority());
const auto hash = (0 << 30) | (passPriority << 16) | (passPriority << 8) | passIdx;
RenderPass renderPass = {hash, renderObj.depth, passIdx, passIdx, subModel};
auto passPriority = static_cast<uint32_t>(pass->getPriority());
auto modelPriority = static_cast<uint32_t>(subModel->getPriority());
auto shaderId = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(subModel->getShader(passIdx)));
const auto hash = (0 << 30) | (passPriority << 16) | (modelPriority << 8) | passIdx;
RenderPass renderPass = {hash, renderObj.depth, shaderId, passIdx, subModel};
_queue.emplace_back(renderPass);
return true;
}
+2 -3
View File
@@ -163,10 +163,9 @@ void Node::setParent(Node *parent) {
}
}
void Node::initWithData(uint8_t *data, uint8_t *flagChunk, uint32_t offset) {
void Node::initWithData(uint8_t *data, uint8_t *flagChunk) {
_nodeLayout = reinterpret_cast<NodeLayout *>(data);
_flagChunk = flagChunk;
_flagOffest = offset;
_flagChunk = reinterpret_cast<uint32_t *>(flagChunk);
}
} // namespace scene
+4 -5
View File
@@ -57,7 +57,7 @@ public:
Node &operator=(const Node &) = delete;
Node &operator=(Node &&) = delete;
void initWithData(uint8_t *, uint8_t *, uint32_t);
void initWithData(uint8_t *, uint8_t *);
void invalidateChildren(TransformBit dirtyBit);
void updateWorldTransform();
@@ -76,7 +76,7 @@ public:
}
}
inline void setFlagsChanged(uint32_t value) { *(reinterpret_cast<uint32_t *>(_flagChunk) + _flagOffest) = value; }
inline void setFlagsChanged(uint32_t value) { *_flagChunk = value; }
inline void setDirtyFlag(uint32_t value) { _nodeLayout->dirtyFlag = value; }
inline void setLayer(uint32_t layer) { _nodeLayout->layer = layer; }
inline void setWorldMatrix(const Mat4 &matrix) { _nodeLayout->worldMatrix.set(matrix); }
@@ -90,7 +90,7 @@ public:
inline void setLocalScale(const Vec3 &scale) { _nodeLayout->localScale.set(scale); }
inline Node * getParent() const { return _parent; }
inline uint32_t getFlagsChanged() const { return *(reinterpret_cast<uint32_t *>(_flagChunk) + _flagOffest); }
inline uint32_t getFlagsChanged() const { return *_flagChunk; }
inline uint32_t getLayer() const { return _nodeLayout->layer; }
inline uint32_t getDirtyFlag() const { return _nodeLayout->dirtyFlag; }
inline const Vec3 & getPosition() const { return _nodeLayout->localPosition; }
@@ -107,8 +107,7 @@ private:
NodeLayout * _nodeLayout{nullptr};
Node * _parent{nullptr};
Mat4 _rtMat;
uint8_t * _flagChunk{nullptr};
uint32_t _flagOffest{0};
uint32_t * _flagChunk{nullptr};
std::vector<Node *> _children;
std::vector<Node *> _computeNodes;
};
+1 -1
View File
@@ -44,7 +44,7 @@ void Pass::setState(gfx::BlendState *bs, gfx::DepthStencilState *ds, gfx::Raster
_blendState = bs;
_depthStencilState = ds;
_rasterizerState = rs;
_descriptorSet = descriptorSet;
_descriptorSet = descriptorSet;
}
} // namespace scene