2011-11-10 14:26:57 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2011-11-10 14:26:57 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/code-stubs.h"
|
|
|
|
#include "src/factory.h"
|
|
|
|
#include "src/macro-assembler.h"
|
|
|
|
#include "src/objects.h"
|
|
|
|
#include "test/cctest/cctest.h"
|
2011-11-10 14:26:57 +00:00
|
|
|
|
|
|
|
#ifdef USE_SIMULATOR
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/simulator.h"
|
2011-11-10 14:26:57 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
|
|
|
|
|
|
|
typedef uint32_t (*HASH_FUNCTION)();
|
|
|
|
|
2012-01-06 11:33:20 +00:00
|
|
|
#define __ masm->
|
2011-11-10 14:26:57 +00:00
|
|
|
|
|
|
|
|
2012-01-10 12:58:41 +00:00
|
|
|
void generate(MacroAssembler* masm, uint32_t key) {
|
2014-05-23 16:37:27 +00:00
|
|
|
#if V8_TARGET_ARCH_IA32 || V8_TARGET_ARCH_X87
|
2012-01-10 12:58:41 +00:00
|
|
|
__ push(ebx);
|
|
|
|
__ mov(eax, Immediate(key));
|
|
|
|
__ GetNumberHash(eax, ebx);
|
|
|
|
__ pop(ebx);
|
|
|
|
__ Ret();
|
|
|
|
#elif V8_TARGET_ARCH_X64
|
2014-03-19 08:59:04 +00:00
|
|
|
__ pushq(kRootRegister);
|
2012-01-10 12:58:41 +00:00
|
|
|
__ InitializeRootRegister();
|
2014-03-19 08:59:04 +00:00
|
|
|
__ pushq(rbx);
|
|
|
|
__ movp(rax, Immediate(key));
|
2012-01-10 12:58:41 +00:00
|
|
|
__ GetNumberHash(rax, rbx);
|
2014-03-19 08:59:04 +00:00
|
|
|
__ popq(rbx);
|
|
|
|
__ popq(kRootRegister);
|
2012-01-10 12:58:41 +00:00
|
|
|
__ Ret();
|
|
|
|
#elif V8_TARGET_ARCH_ARM
|
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
__ mov(r0, Operand(key));
|
|
|
|
__ GetNumberHash(r0, ip);
|
|
|
|
__ pop(kRootRegister);
|
|
|
|
__ mov(pc, Operand(lr));
|
2014-03-21 09:28:26 +00:00
|
|
|
#elif V8_TARGET_ARCH_ARM64
|
|
|
|
// The ARM64 assembler usually uses jssp (x28) as a stack pointer, but only
|
|
|
|
// csp is initialized by the calling (C++) code.
|
2014-02-12 09:19:30 +00:00
|
|
|
Register old_stack_pointer = __ StackPointer();
|
|
|
|
__ SetStackPointer(csp);
|
|
|
|
__ Push(root, xzr);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
__ Mov(x0, key);
|
|
|
|
__ GetNumberHash(x0, x10);
|
|
|
|
__ Pop(xzr, root);
|
|
|
|
__ Ret();
|
|
|
|
__ SetStackPointer(old_stack_pointer);
|
2014-07-09 11:08:26 +00:00
|
|
|
#elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
|
2012-01-10 12:58:41 +00:00
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
__ li(v0, Operand(key));
|
|
|
|
__ GetNumberHash(v0, t1);
|
|
|
|
__ pop(kRootRegister);
|
|
|
|
__ jr(ra);
|
|
|
|
__ nop();
|
2015-01-16 07:42:00 +00:00
|
|
|
#elif V8_TARGET_ARCH_PPC
|
|
|
|
__ function_descriptor();
|
|
|
|
__ push(kRootRegister);
|
|
|
|
__ InitializeRootRegister();
|
|
|
|
__ li(r3, Operand(key));
|
|
|
|
__ GetNumberHash(r3, ip);
|
|
|
|
__ pop(kRootRegister);
|
|
|
|
__ blr();
|
2014-02-12 09:19:30 +00:00
|
|
|
#else
|
|
|
|
#error Unsupported architecture.
|
2012-01-10 12:58:41 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void check(uint32_t key) {
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-01-09 15:47:53 +00:00
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
|
2012-01-10 12:58:41 +00:00
|
|
|
v8::internal::byte buffer[2048];
|
2015-11-25 14:23:37 +00:00
|
|
|
MacroAssembler masm(CcTest::i_isolate(), buffer, sizeof(buffer),
|
|
|
|
v8::internal::CodeObjectRequired::kYes);
|
2012-01-10 12:58:41 +00:00
|
|
|
|
|
|
|
generate(&masm, key);
|
|
|
|
|
|
|
|
CodeDesc desc;
|
|
|
|
masm.GetCode(&desc);
|
2013-01-09 15:47:53 +00:00
|
|
|
Handle<Object> undefined(isolate->heap()->undefined_value(), isolate);
|
|
|
|
Handle<Code> code = factory->NewCode(desc,
|
|
|
|
Code::ComputeFlags(Code::STUB),
|
|
|
|
undefined);
|
2012-01-10 12:58:41 +00:00
|
|
|
CHECK(code->IsCode());
|
|
|
|
|
|
|
|
HASH_FUNCTION hash = FUNCTION_CAST<HASH_FUNCTION>(code->entry());
|
|
|
|
#ifdef USE_SIMULATOR
|
2015-11-23 08:09:34 +00:00
|
|
|
uint32_t codegen_hash = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(
|
|
|
|
CALL_GENERATED_CODE(isolate, hash, 0, 0, 0, 0, 0)));
|
2012-01-10 12:58:41 +00:00
|
|
|
#else
|
|
|
|
uint32_t codegen_hash = hash();
|
|
|
|
#endif
|
|
|
|
|
2013-01-09 15:47:53 +00:00
|
|
|
uint32_t runtime_hash = ComputeIntegerHash(key, isolate->heap()->HashSeed());
|
Move hash code from hidden string to a private symbol
* Hash code is now just done with a private own symbol instead of the hidden string, which predates symbols.
* In the long run we should do all hidden properties this way and get rid of the
hidden magic 0-length string with the zero hash code. The advantages include
less complexity and being able to do things from JS in a natural way.
* Initially, the performance of weak set regressed, because it's a little harder
to do the lookup in C++. Instead of heroics in C++ to make things faster I
moved some functionality into JS and got the performance back. JS is supposed to be good at looking up named properties on objects.
* This also changes hash codes of Smis so that they are always Smis.
Performance figures are in the comments to the code review. Summary: Most of js-perf-test/Collections is neutral. Set and Map with object keys are 40-50% better. WeakMap is -5% and WeakSet is +9%. After the measurements, I fixed global proxies, which cost 1% on most tests and 5% on the weak ones :-(.
In the code review comments is a patch with an example of the heroics we could do in C++ to make lookup faster (I hope we don't have to do this. Instead of checking for the property, then doing a new lookup to insert it, we could do one lookup and handle the addition immediately). With the current benchmarks above this buys us nothing, but if we go back to doing more lookups in C++ instead of in stubs and JS then it's a win.
In a similar vein we could give the magic zero hash code to the hash code
symbol. Then when we look up the hash code we would sometimes see the table
with all the hidden properties. This dual use of the field for either the hash
code or the table with all hidden properties and the hash code is rather ugly,
and this CL gets rid of it. I'd be loath to bring it back. On the benchmarks quoted above it's slightly slower than moving the hash code lookup to JS like in this CL.
One worry is that the benchmark results above are more monomorphic than real
world code, so may be overstating the performance benefits of moving to JS. I
think this is part of a general issue we have with handling polymorphic code in
JS and any solutions there will benefit this solution, which boils down to
regular property access. Any improvement there will lift all boats.
R=adamk@chromium.org, verwaest@chromium.org
BUG=
Review URL: https://codereview.chromium.org/1149863005
Cr-Commit-Position: refs/heads/master@{#28622}
2015-05-26 11:26:26 +00:00
|
|
|
CHECK_EQ(runtime_hash, codegen_hash);
|
2012-01-10 12:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t PseudoRandom(uint32_t i, uint32_t j) {
|
|
|
|
return ~(~((i * 781) ^ (j * 329)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
TEST(NumberHash) {
|
2013-09-19 08:54:58 +00:00
|
|
|
v8::Isolate* isolate = CcTest::isolate();
|
2013-05-08 07:45:16 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Context::Scope context_scope(v8::Context::New(isolate));
|
2012-01-10 12:58:41 +00:00
|
|
|
|
|
|
|
// Some specific numbers
|
|
|
|
for (uint32_t key = 0; key < 42; key += 7) {
|
|
|
|
check(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some pseudo-random numbers
|
|
|
|
static const uint32_t kLimit = 1000;
|
|
|
|
for (uint32_t i = 0; i < 5; i++) {
|
|
|
|
for (uint32_t j = 0; j < 5; j++) {
|
|
|
|
check(PseudoRandom(i, j) % kLimit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-10 14:26:57 +00:00
|
|
|
#undef __
|