From b320ea97b9c7b47feb61405a1bbd5ab8a7f0edc1 Mon Sep 17 00:00:00 2001 From: Andrew Woloszyn Date: Tue, 27 Feb 2018 10:05:57 -0500 Subject: [PATCH] Add an assert to the compiler for map sizes. We require that they are a power of 2, otherwise we have to fall back to a slow % operator, instead of &. --- gapil/compiler/map.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/gapil/compiler/map.go b/gapil/compiler/map.go index aa108debe5..ab715c1d5b 100644 --- a/gapil/compiler/map.go +++ b/gapil/compiler/map.go @@ -66,6 +66,12 @@ import ( // If we end up with lots of insertions/deletions, this will prevent linear search func (c *C) defineMapType(t *semantic.Map) { + + if ((minMapSize & (minMapSize - 1)) != 0) || + ((mapGrowMultiplier & (mapGrowMultiplier - 1)) != 0) { + fail("Map size must be a power of 2") + } + mapPtrTy := c.T.target[t].(codegen.Pointer) mapStrTy := mapPtrTy.Element.(*codegen.Struct) keyTy := c.T.Target(t.KeyType)