Path: blob/master/core/object/callable_method_pointer.cpp
9903 views
/**************************************************************************/1/* callable_method_pointer.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "callable_method_pointer.h"3132bool CallableCustomMethodPointerBase::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) {33const CallableCustomMethodPointerBase *a = static_cast<const CallableCustomMethodPointerBase *>(p_a);34const CallableCustomMethodPointerBase *b = static_cast<const CallableCustomMethodPointerBase *>(p_b);3536if (a->comp_size != b->comp_size) {37return false;38}3940// Avoid sorting by memory address proximity, which leads to unpredictable performance over time41// due to the reuse of old addresses for newer objects. Use byte-wise comparison to leverage the42// backwards encoding of little-endian systems as a way to decouple spatiality and time.43return memcmp(a->comp_ptr, b->comp_ptr, a->comp_size * 4) == 0;44}4546bool CallableCustomMethodPointerBase::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) {47const CallableCustomMethodPointerBase *a = static_cast<const CallableCustomMethodPointerBase *>(p_a);48const CallableCustomMethodPointerBase *b = static_cast<const CallableCustomMethodPointerBase *>(p_b);4950if (a->comp_size != b->comp_size) {51return a->comp_size < b->comp_size;52}5354// See note in compare_equal().55return memcmp(a->comp_ptr, b->comp_ptr, a->comp_size * 4) < 0;56}5758CallableCustom::CompareEqualFunc CallableCustomMethodPointerBase::get_compare_equal_func() const {59return compare_equal;60}6162CallableCustom::CompareLessFunc CallableCustomMethodPointerBase::get_compare_less_func() const {63return compare_less;64}6566uint32_t CallableCustomMethodPointerBase::hash() const {67return h;68}6970void CallableCustomMethodPointerBase::_setup(uint32_t *p_base_ptr, uint32_t p_ptr_size) {71comp_ptr = p_base_ptr;72comp_size = p_ptr_size / 4;7374// Precompute hash.75for (uint32_t i = 0; i < comp_size; i++) {76if (i == 0) {77h = hash_murmur3_one_32(comp_ptr[i]);78} else {79h = hash_murmur3_one_32(comp_ptr[i], h);80}81}82}838485